#!/bin/sh

# This script restores enstoredb from latest backup

enstore_dir=${ENSTORE_DIR}
database_area=/diskb/enstore-db
database_scripts=$enstore_dir/databases/scripts
database_owner=products
db_name=enstoredb
db_owner=enstore
db_port=8888
db_startup_script=$database_scripts/enstore-db
db_schema=$enstore_dir/databases/schemas/enstoredb.schema
tmp=/tmp
restore_tmp=/diskb/DBRESTORE
journal_dir=/diska/enstore-journal
db_dump_file=enstoredb.dmp

# find the host
host=$1
if [ "$host" == "" ]
then
	host=`uname -n | cut -d. -f1`
fi

echo "host = $host"

case $host in
	stken*)
		hba_file=$enstore_dir/databases/control_files/pg_hba.conf-stkensrv0
		;;
	cdfen*)
		hba_file=$enstore_dir/databases/control_files/pg_hba.conf-cdfensrv0
		;;
	d0en*)
		hba_file=$enstore_dir/databases/control_files/pg_hba.conf-d0ensrv0
		;;	
	*)
		hba_file=$enstore_dir/databases/control_files/pg_hba.conf-cdfensrv0
		;;
esac

case $host in
	stken*)
		backup_host=stkensrv3.fnal.gov
		;;
	cdfen*)
		backup_host=cdfensrv3.fnal.gov
		;;
	d0en*)
		backup_host=d0ensrv3.fnal.gov
		;;
	*)
		backup_host=cdfensrv3.fnal.gov
		;;
esac

backup_dir=/diska/enstore-backup

# need to setup enstore first
if [ "$enstore_dir" == "" ]
then
	echo "need to set up enstore first"
	exit
fi

# must be root
if [ "`whoami`" != "root" ]
then
	echo "must run as root"
	exit 0
fi

# retriving backup

archive=`rsh $backup_host "ls -dt $backup_dir/dbase.* | head -1"`
echo archive=$backup_host:$archive

# check if $restore_tmp exist. If so, rename it

if [ -d $restore_tmp ]
then
	echo $restore_tmp exists, rename it
	dest=$restore_tmp.`date +%F.%T`
	echo mv $restore_tmp $dest
	mv $restore_tmp $dest
fi

# create restore_tmp
echo mkdir -p $restore_tmp
mkdir -p $restore_tmp

# retrieve backup
echo retrieving backup
echo rcp "$backup_host:$archive/*" $restore_tmp
rcp "$backup_host:$archive/*" $restore_tmp

# untar journals
echo "untaring journal files ..."
for i in $restore_tmp/*.gz
do
	echo "gunzip -c $i | tar xvfC - $restore_tmp -"
	gunzip -c $i | tar xvfC - $restore_tmp
	rm $i
done

# copy anything from $journal_dir
echo "copying current journal files ..."
for i in $journal_dir/*
do
	echo cp $i $restore_tmp
	cp $i $restore_tmp
done

# check if $database_area exist. If so, rename it

echo "creating new database area"

if [ -d $database_area ]
then
	echo $database_area exists, rename it
	dest=$database_area.`date +%F.%T`
	echo mv $database_area $dest
	mv $database_area $dest
fi

# create initial database area
echo create database area $database_area
echo mkdir -p $database_area
mkdir -p $database_area
echo chown $database_owner.$database_owner $database_area
chown $database_owner.$database_owner $database_area
echo chmod go-rwx $database_area
chmod go-rwx $database_area
echo su $database_owner -c \"initdb -D $database_area\"
su $database_owner -c "initdb -D $database_area"

#  modify create pg_hba.conf
echo su $database_owner cp \"$hba_file $database_area/pg_hba.conf\"
su $database_owner -c "cp $hba_file $database_area/pg_hba.conf"

# bring up database server
echo su $database_owner -c \"postmaster -D $database_area -p $db_port \&\"
su $database_owner -c "postmaster -D $database_area -p $db_port &"
# echo su $database_owner -c "$db_startup_script start"
# su $database_owner -c "$db_startup_script start"

echo waiting for database server to start up
sleep 10

# create user enstore
#
# This is a little bit different in batch mode
echo su $database_owner createuser -p $db_port -d -S -R -P enstore 
su $database_owner -c "psql template1 -p $db_port -c \"create user enstore password 'enstore_user' createdb;\""

# create user enstore_reader
#
# This is a little bit different in batch mode
echo su $database_owner createuser -p $db_port -d -S -R -P enstore_reader 
su $database_owner -c "psql template1 -p $db_port -c \"create user enstore_reader password 'readonly_enstore_user';\""

# create database enstoredb
echo su $db_owner createdb -p $db_port $db_name
su $db_owner -c "createdb -p $db_port $db_name"

# restore database from backup
echo "restoring database from backup ..."
echo su $database_owner -c \"pg_restore -p $db_port -d $db_name $restore_tmp/$db_dump_file\"
su $database_owner -c "pg_restore -p $db_port -d $db_name $restore_tmp/$db_dump_file"

# synchronize database using the journal
echo su $db_owner -c \"$ENSTORE_DIR/src/restoredb.py $restore_tmp\"
su $db_owner -c "$ENSTORE_DIR/src/restoredb.py $restore_tmp"

# shutdown database server
echo shuting down database server ...
echo kill \`head -1 $database_area/postmaster.pid\`
kill `head -1 $database_area/postmaster.pid`
