#!/bin/sh
DEFPROP=$1
DEFDB=$2
DEFSRC=$3
DEFSERIAL=$4

# Specify where the database resides:
# MySQL client, hostname, user and password of the server
SOURCE=${DEFSRC:=RIPE}
MYSQL=/usr/package/mysql/bin/mysql
HOST=myhost.mydb.net
USER=user
PASSWORD=password
# the database name
DB=${DEFDB:?"Database name is not set. Usage make_db properties db_name [source [serial]]"}
LOG=DBmake7.$$.log


# Directory where the object files reside
OBJDIR=/export/dbfs2/dbwork/RIP/snapshot
UNZIP1='gzip -cd'

# CURRENTSERIAL
DEFCURRENTSERIAL=`cat ${OBJDIR}/${SOURCE}.CURRENTSERIAL`
DEFCURRENTSERIAL=${DEFCURRENTSERIAL:=1}
CURRENTSERIAL=${DEFSERIAL:=$DEFCURRENTSERIAL}

# Name of the properties file
PROPERTIES=${DEFPROP:?"Properties file is not set. Usage make_db properties db_name [source [serial]]"}

echo
echo "Starting to make a database"
echo "    source: $SOURCE"
echo "***************************"
echo 
echo "Database: ${DB}@${HOST}"
echo "User: $USER"
echo "Password: $PASSWORD"
echo ""
echo "Gzip=" ${UNZIP1}
echo "Objects in " ${OBJDIR}
echo "Properties=" ${PROPERTIES}
echo "CURRENTSERIAL=" ${CURRENTSERIAL}
echo "Logging results in $LOG"
echo "***************************"
echo
echo "Accept and continue(Y/N)? (Default No)"

read ANS dummy

if [ $ANS != 'Y' ] 
then
	exit;
fi

echo "OK"
export MYSQL HOST USER PASSWORD DB LOG OBJDIR UNZIP1 PROPERTIES CURRENTSERIAL SOURCE


echo "*******************"`date`"******************" >$LOG
echo "Creating tables\n" >>$LOG
echo "Creating tables\n"

cd SQL; ./create.first-stage >>../$LOG; cd ..

echo `date`"\n" >>$LOG
echo "Loading tables - I pass\n" >>$LOG
echo "Loading tables - I pass\n"

./load1.sh 

echo `date`"\n" >>$LOG
echo "Making indexes\n" >>$LOG

cd SQL; ./create.second-stage >>../$LOG; cd ..

echo `date`"\n" >>$LOG
echo "Loading tables - II pass\n" >>$LOG

./load2.sh

echo `date`"\n" >>$LOG
echo "Making more indexes\n" >>$LOG

cd SQL; ./create.third-stage >>../$LOG; cd ..

echo "Finished "`date` >>$LOG

