#!/bin/sh

# empty_db - Script to empty an SQL database
# used by the RIPE Whoisd software v3
# Only the current serial will remain.
# Use with care!

# Usage: empty_db db_name [source [serial]]
DEFDB=$1
DEFSRC=$2
DEFSERIAL=$3

#####################
# Customizable variables

# Specify where the database resides
MYSQL=/usr/package/mysql/bin/mysql
#MYSQL=/usr/local/mysql/bin/mysql
HOST=yucca
USER=dbint
PASSWORD=reimp
# the databse name
LOG=DBempty.$$.log

#####################

# The database name
DB=${DEFDB:?"Database name is not set Usage: $0 db_name [source [serial]]"}

# The source
SOURCE=${DEFSRC:=RIPE}


# The current serial can be set to any number
CURRENTSERIAL=${DEFSERIAL:=1}

echo
echo "You are about to empty a database."
echo "    source: $SOURCE"
echo "***************************"
echo 
echo "Database: ${DB}@${HOST}"
echo "User: $USER"
echo "Password: $PASSWORD"
echo ""
echo "CURRENTSERIAL=" ${CURRENTSERIAL}
echo "Logging results in $LOG"
echo "***************************"
echo
echo "WARNING!!! By accepting, you will remove"
echo "all the information contained in the database."
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 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 "Making indexes\n" >>$LOG
echo "Making indexes\n" 

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

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

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

echo "Finished "`date` >>$LOG
echo "Finished "`date` 

