#!/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 client, hostname, user and password of the server
MYSQL=/usr/local/bin/mysql
HOST=myhost.mydb.net
USER=myuser
PASSWORD=mypasswd
LOG=DBmake.$$.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` 

