( cd opt/postgresql-8.4.2/lib ; rm -rf libpgtypes.so )
( cd opt/postgresql-8.4.2/lib ; ln -sf libpgtypes.so.3.1 libpgtypes.so )
( cd opt/postgresql-8.4.2/lib ; rm -rf libpq.so.5 )
( cd opt/postgresql-8.4.2/lib ; ln -sf libpq.so.5.2 libpq.so.5 )
( cd opt/postgresql-8.4.2/lib ; rm -rf libpgtypes.so.3 )
( cd opt/postgresql-8.4.2/lib ; ln -sf libpgtypes.so.3.1 libpgtypes.so.3 )
( cd opt/postgresql-8.4.2/lib ; rm -rf libecpg.so.6 )
( cd opt/postgresql-8.4.2/lib ; ln -sf libecpg.so.6.1 libecpg.so.6 )
( cd opt/postgresql-8.4.2/lib ; rm -rf libecpg_compat.so )
( cd opt/postgresql-8.4.2/lib ; ln -sf libecpg_compat.so.3.1 libecpg_compat.so )
( cd opt/postgresql-8.4.2/lib ; rm -rf libpq.so )
( cd opt/postgresql-8.4.2/lib ; ln -sf libpq.so.5.2 libpq.so )
( cd opt/postgresql-8.4.2/lib ; rm -rf libecpg.so )
( cd opt/postgresql-8.4.2/lib ; ln -sf libecpg.so.6.1 libecpg.so )
( cd opt/postgresql-8.4.2/lib ; rm -rf libecpg_compat.so.3 )
( cd opt/postgresql-8.4.2/lib ; ln -sf libecpg_compat.so.3.1 libecpg_compat.so.3 )
( cd opt/postgresql-8.4.2/bin ; rm -rf postmaster )
( cd opt/postgresql-8.4.2/bin ; ln -sf postgres postmaster )
( cd usr/share/doc/postgresql-8.4.2 ; rm -rf html )
( cd usr/share/doc/postgresql-8.4.2 ; ln -sf ../../../../opt/pgsql/doc/html html )
if [ -x  opt/pgsql/bin/pgsqld ]; then
   opt/pgsql/bin/pgsqld stop
   mv opt/pgsql/data opt/postgresql-8.4.2/
fi
( cd opt ; rm -rf pgsql )
( cd opt ; ln -sf postgresql-8.4.2 pgsql )
#%% begin initialize aaa_postgresql-8.4.2-i586-P1
#!/bin/sh
# This should be done at initial booting after installation.
#
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
export LANG=C

if [ -r /tmp/SeTT_PX ]; then
  ROOT="`cat /tmp/SeTT_PX`"
fi

cd $ROOT/

#if [ -d $ROOT/opt/pgsql ] ; then
#  mv $ROOT/opt/pgsql $ROOT/opt/pgsql.bak
#fi
#( cd opt ; rm -f pgsql ; ln -sf postgresql-8.4.2 pgsql )
# rm -f $ROOT/opt/pgsql
# ln -sf $ROOT/opt/postgresql-8.4.2 $ROOT/opt/pgsql
#
#(cd $ROOT/opt/postgresql-8.4.2/bin; ln -sf postgres postmaster)

#(cd $ROOT/usr/share/doc/postgresql-8.4.2 && \
# rm -f doc && \
# ln -s $ROOT/opt/postgresql-8.4.2/doc doc )

#
# Create Environment
#
PG_ROOT=$ROOT/opt/postgresql-8.4.2; export PG_ROOT

(cd $PG_ROOT/lib && \
	ln -sf libecpg.so.6.1 libecpg.so.6
	ln -sf libecpg.so.6.1 libecpg.so
	ln -sf libecpg_compat.so.3.1 libecpg_compat.so
	ln -sf libecpg_compat.so.3.1 libecpg_compat.so.3
	ln -sf libpgtypes.so.3.1 libpgtypes.so
	ln -sf libpgtypes.so.3.1 libpgtypes.so.3
	ln -sf libpq.so.5.2 libpq.so
	ln -sf libpq.so.5.2 libpq.so.5
)


## Add user and group
#groupadd pgsql
#useradd -g pgsql -d $ROOT/opt/pgsql -c "Postgres Admin." -s /bin/tcsh postgres
chown -R postgres.pgsql $ROOT/opt/postgresql-8.4.2

# This is required for Kerberos authentication
if ! fgrep "postgres" $ROOT/etc/services 1> /dev/null 2>&1; then
    echo 'postgres	5432/tcp			# Reserve for PostgreSQL' >> $ROOT/etc/services
else
	echo "Entry postgres is already in /etc/services."
fi

if [ ! -f $ROOT/etc/rc.d/init.d/postgresql ]; then
  cp $ROOT/opt/postgresql-8.4.2/etc/init.d/postgresql $ROOT/etc/rc.d/init.d/
  chmod +x $ROOT/etc/rc.d/init.d/postgresql
fi

if ! fgrep "/opt/pgsql/lib" $ROOT/etc/ld.so.conf 1> /dev/null 2>&1; then
  echo "/opt/pgsql/lib" >> $ROOT/etc/ld.so.conf
  $ROOT/sbin/ldconfig
else
  echo "Entry /opt/pgsql/lib is already in $ROOT/etc/ld.so.conf."
fi

# fix startup shell
sed -e "s%/opt/pgsql%/opt/postgresql-8.4.2%" -e "s%5432%5432%" \
	$ROOT/opt/postgresql-8.4.2/apps/utils/pgsqld \
	> $ROOT/opt/postgresql-8.4.2/bin/pgsqld
chmod +x $ROOT/opt/postgresql-8.4.2/bin/pgsqld
chown postgres.pgsql $ROOT/opt/postgresql-8.4.2/bin/pgsqld

# add LOG_LOCAL0 for syslogd report
if ! fgrep "/var/log/pgsqlog" $ROOT/etc/syslog.conf 1> /dev/null 2>&1; then
    echo "local0.*		/var/log/pgsqlog" >> $ROOT/etc/syslog.conf
    if [ ! -f $ROOT/var/log/pgsqlog ]; then
	touch $ROOT/var/log/pgsqlog
    fi
    kill -HUP `pidof syslogd`
else
	echo "Entry pgsqlog is already in /etc/syslog.conf."
fi

if [ ! -d $ROOT/etc/logrotate.d ]; then
  if [ -f $ROOT/etc/logrotate.conf ]; then
    if ! fgrep "/var/log/pgsqlog" $ROOT/etc/logrotate.conf 1> /dev/null 2>&1; then
        cat $ROOT/opt/postgresql-8.4.2/etc/logrotate.pgsql >> $ROOT/etc/logrotate.conf
    fi
  fi
else
  if [ ! -f $ROOT/etc/logrotate.d/postgresql ]; then
    cp $ROOT/opt/postgresql-8.4.2/etc/logrotate.pgsql $ROOT/etc/logrotate.d/postgresql
  fi
    #if ! fgrep "/var/log/pgsqlog" $ROOT/etc/logrotate.d/syslog 1> /dev/null 2>&1; then
    #  cp -p $ROOT/etc/logrotate.d/syslog $ROOT/tmp/syslog.old
    #  sed -e 's% {% /var/log/pgsqlog {%' $ROOT/tmp/syslog.old > $ROOT/etc/logrotate.d/syslog
    #fi
fi
if ! fgrep "Add by Plamo Linux 4" $ROOT/opt/postgresql-8.4.2/share/postgresql.conf.sample 1> /dev/null 2>&1; then
  cat >> $ROOT/opt/postgresql-8.4.2/share/postgresql.conf.sample <<ENF

#
# Add by Plamo Linux 4.x installer for Postgres8
listen_addresses = '*'
ssl = true

#log_destination = 'syslog'
#syslog_facility = 'LOCAL0'
#syslog_ident = 'postgres'

# logging to file on 8.4
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
log_line_prefix = '%t [%p] %u '

ENF
#  $ROOT/opt/postgresql-8.4.2/apps/utils/get_memory_parameters -n 64 \
#	>>$ROOT/opt/postgresql-8.4.2/share/postgresql.conf.sample
fi

# set environment variables when non default installation 
if [ "$ROOT" != "" ] ; then
  (cd $ROOT/opt/postgresql-8.4.2/apps/utils;
   mv pgsqld pgsqld.org;
   sed -e "s%PGROOT=%PGROOT=$ROOT%" pgsqld.org > pgsqld;
   chown postgres.pgsql pgsqld;
   chmod +x pgsqld)

  if [ ! -f $ROOT/opt/postgresql-8.4.2/.cshrc.mine ]; then
  sed -e "s%/opt/pgsql%/opt/postgresql-8.4.2%" -e "s%5432%5432%" \
	$ROOT/opt/postgresql-8.4.2/apps/_cshrc.pgsqlj > $ROOT/opt/postgresql-8.4.2/.cshrc.mine
  fi
  (cd $ROOT/opt/postgresql-8.4.2/;
   mv .cshrc.mine .cshrc.mine.org;
   sed -e "s%PG_DIR	%PG_DIR	$ROOT%" .cshrc.mine.org >> .cshrc.mine;
   chown postgres.pgsql .cshrc.mine;
   )
fi


#
# Initializing DATABASE
#

# set access control file(open for your network)
NADDR=`ifconfig eth0 | fgrep "inet " | sed -e 's/.*addr:\(.*\)  .*Bcast:\(.*\)  .*Mask:\(.*\)/\1/'`
NMASK=`ifconfig eth0 | fgrep "inet " | sed -e 's/.*addr:\(.*\)  .*Bcast:\(.*\)  .*Mask:\(.*\)/\3/'`
if [ "$NADDR" != "" ] ; then
	sed -e "s/192.168.2.0/$NADDR/" -e "s/255.255.255.128/$NMASK/" \
 $ROOT/opt/postgresql-8.4.2/share/pg_hba.conf.template > $ROOT/opt/postgresql-8.4.2/share/pg_hba.conf.sample
fi

# Initialize database
sed -e "s%/opt/pgsql%/opt/postgresql-8.4.2%" -e "s%5432%5432%" \
	$ROOT/opt/postgresql-8.4.2/apps/utils/initialize_default_database \
	> $ROOT/opt/postgresql-8.4.2/apps/utils/initialize_default_database.sh
/bin/su - postgres -c "sh $ROOT/opt/postgresql-8.4.2/apps/utils/initialize_default_database.sh"

# Start backend
$ROOT/etc/rc.d/init.d/postgresql start
sleep 5

# Create database users
sed -e "s%/opt/pgsql%/opt/postgresql-8.4.2%" -e "s%5432%5432%" \
	$ROOT/opt/postgresql-8.4.2/apps/utils/create_default_users-8.x \
	> $ROOT/opt/postgresql-8.4.2/apps/utils/create_default_users.sh
/bin/su - postgres -c "sh $ROOT/opt/postgresql-8.4.2/apps/utils/create_default_users.sh"

if [ ! -f $ROOT/opt/postgresql-8.4.2/.cshrc ]; then
  sed -e "s%/opt/pgsql%/opt/postgresql-8.4.2%" -e "s%5432%5432%" \
	$ROOT/opt/postgresql-8.4.2/apps/_cshrc.pgsql > $ROOT/opt/postgresql-8.4.2/.cshrc
fi
if [ ! -f $ROOT/opt/postgresql-8.4.2/.bashrc ]; then
  sed -e "s%/opt/pgsql%/opt/postgresql-8.4.2%" -e "s%5432%5432%" \
	$ROOT/opt/postgresql-8.4.2/apps/_bashrc.pgsql > $ROOT/opt/postgresql-8.4.2/.bashrc
fi

rm  -f $ROOT/opt/htdocs/postgresql-8.4.2
ln -sf /usr/share/doc/postgresql-8.4.2 $ROOT/opt/htdocs/postgresql-8.4.2

cd $ROOT/
#%% end
#%% end
