#! /bin/sh
# create the postgres user ID:

PG_UID=42

res=`awk 'BEGIN {FS=":";res="undefined"}
$1 == "postgres" && $3 == '"$PG_UID"' {res= "exists";exit}
$3 == '"$PG_UID"' {res= "in_use";exit}
$1 == "postgres" {res= "other_id";exit}
END {print res}' /etc/passwd`

group=`awk 'BEGIN {FS=":";res="0"}
$1 == "other" && res == "0" {res=$2}
$1 == "user" {res=$2}
END {print res}' /etc/group`

case $res in
    undefined) if [ -x /bin/tcsh ] ; then shell=/bin/tcsh ; else shell=/bin/sh ; fi
	echo "postgres:*:$PG_UID:$group:Postgres Database Admin:/usr/local/postgres:$shell" >>/etc/passwd
	echo "Please type the password, you want to use for the Postgres Database Admin:"
	passwd postgres
	# Stupid tar does has probably done it all wrong.  Make postgres be
	# the owner of his files:
	chown postgres `find /usr/local/postgres/ -uid 0 -print`
	;;
    exists) echo "A postgres user already exists and has the correct ID.  Doing nothing."
	;;
    in_use) echo "User ID $PG_UID is already in use.  Please try giving that user"
	echo "an other ID, as otherwise, some of the postgres demos may not work."
	echo "Then re-run this script!"
	;;
    other_id) echo "A postgres user already exists, but has the wrong user ID.  Please"
	echo "change it to $PG_UID by hand, so that all demos will work correctly!"
	;;
esac
