#!/bin/sh
#
# pg_dumpall [pg_dump parameters]
# dumps all databases to standard output
# It also dumps the pg_user table
#
# to adapt to System V vs. BSD 'echo'
#set -x
if echo '\\' | grep '\\\\' >/dev/null 2>&1
then	
	BS='\'			# BSD
else
	BS='\\'			# System V
fi
#
# Dump everyone but the postgres user
# initdb creates him
#
# get the postgres user id
#
POSTGRES_SUPER_USER_ID="`psql -A -q -t template1 <<END
select datdba
from pg_database
where datname = 'template1';
END`"
echo "${BS}connect template1"
#
# delete all users in case they run this twice
#
echo "delete from pg_user"
echo "where usesysid <> $POSTGRES_SUPER_USER_ID;"
#
# load all the non-postgres users
#
echo "copy pg_user from stdin;"
psql -q template1 <<END
select pg_user.* into table tmp_pg_user
from pg_user
where usesysid <> $POSTGRES_SUPER_USER_ID;
copy tmp_pg_user to stdout;
drop table tmp_pg_user;
END
echo "${BS}."
psql -l -A -q -t| tr '|' ' ' | grep -v '^template1 ' | \
while read DATABASE PGUSERID DATAPATH
do
	POSTGRES_USER="`psql -A -q -t template1 <<END
select usename
from pg_user
where usesysid = $PGUSERID;
END`"

	echo "${BS}connect template1 $POSTGRES_USER"
	echo "create database $DATABASE;"
	echo "${BS}connect $DATABASE $POSTGRES_USER"
	pg_dump "$@" $DATABASE
done
# done
