#!/bin/bash

# Debug info: uncomment when debugging
DEBUG="set -x"
$DEBUG

##
## Usage: make_db -c <config_file> -s <source> -[0|1|2] -f <file>
##  
## -t <topdir> of whoisd
## -p svconfig port
## -h whoishost
## <config_file> is the name of the whois_rip configuration file
## <source> is the source (SOURSE) to be loaded
## -[0|1|2] numebwr of load passes
## 0 - just to create an empty database; if the database already exists it will be emptied
## 1 - one pass. This is a recommended option. May be a little slower than 2-pass loading
##     but more reliable.
## 2 - for loading huge databases (more than 1,000,000 objects). High requirements on 
##     the consuistency of the flat files: duplicate objects (objects with the same primary key)
##     will prevent loading.
## <file> is the name of the file containing objects to be loaded (plain text)

##
## Functions
##

DATECMD="/bin/date '+%Y-%m-%d %H:%M:%S'"
TIMECMD="/bin/date '+%H:%M:%S'"

# log a message, with appropriate timestamp
log()
{
 echo 
 echo "=" `eval $TIMECMD` $*
 echo
}

# log an error message, with appropriate timestamp
error()
{
 echo "************** ERROR ***************"
 echo "*** " `eval $TIMECMD` $*
 echo "************************************"
 echo
}

# exit with an error message
error_exit()
{
 error $*
 exit 1
}

USAGE="make_db -t topdir -h host -p confport -c <config_file> -s <source> -[0|1|2] -f file \n"

######################
# Parse arguments

while getopts t:h:p:f:l:c:s:012 a
do
case $a in
h)       WHOISHOST=$OPTARG;;
p)       CONFPORT=$OPTARG;;
t)       TOPDIR=$OPTARG;;
c)       CARG=$OPTARG;;
s)       SARG=$OPTARG;;
l)       LOAD_FILE=$OPTARG;;
f)	     LIST=$OPTARG;;
0)       NPASSES=0;;
1)       NPASSES=1;;
2)       NPASSES=2;;
\?)      echo $USAGE
         exit 2;;
esac
done
         
shift `expr $OPTIND - 1`

SOURCE=${SARG:?"<source> is not specified. Usage: $USAGE"}
PROPERTIES=${CARG:?"<config_file> is not specified. Usage: $USAGE"}
NPASSES=${NPASSES:?" number of passes is not specified. Usage: $USAGE"}

# check whether the configuration file exists
ls $PROPERTIES >/dev/null 2>&1 || error_exit "cannot find config file $PROPERTIES"

##################### DIRECTORY LAYOUT #############################################
# Directory where the load_file.sh script is located
SCRIPTDIR=$TOPDIR/bin; export SCRIPTDIR
# Location of the loader binary
LOADER=$TOPDIR/bin/loader
# Update log. Use /dev/null for huge databases.
UPDLOG=/dev/tty

#################### LOCATION OF THE UTILITIES #####################################

# Location of the utility that fetches config variables
GETVAR="$TOPDIR/bin/getvar -s $SOURCE -c $PROPERTIES"

# Filter that uses to convert files to RPSL format
FILTER=cat

# Binary to unzip object files (snapshots)
# UNZIP1='gzip -cd'

# MySQL setup is taken from the configuration file
# from the section SOURCE <source_name>
# where <source_name> is <source>

SQLHOST=`$GETVAR -p dbhost` || error_exit "unknown source $SOURCE"
SQLPORT=`$GETVAR -p dbport` || error_exit "unknown source $SOURCE"
SQLUSER=`$GETVAR -p dbuser` || error_exit "unknown source $SOURCE"
SQLPSWD=`$GETVAR -p dbpswd` || error_exit "unknown source $SOURCE"
SQLDB=`$GETVAR -p dbname` || error_exit "unknown source $SOURCE"

# Given the $PROPERTIES, let's get RIPADMIN db connection data
for i in `egrep '^RIPADMIN ' $PROPERTIES | cut -d" " -f2 | tr "," " "`; do
	if [ -z $RIPADMIN_DBHOST ]; then export RIPADMIN_DBHOST=$i; continue; fi
	if [ -z $RIPADMIN_DBPORT ]; then export RIPADMIN_DBPORT=$i; continue; fi
	if [ -z $RIPADMIN_DBUSER ]; then export RIPADMIN_DBUSER=$i; continue; fi
	if [ -z $RIPADMIN_DBPASS ]; then export RIPADMIN_DBPASS=$i; continue; fi
	if [ -z $RIPADMIN_DBNAME ]; then export RIPADMIN_DBNAME=$i; continue; fi
done

# MySQL commands
# note: the "CMD" versions include appropriate starup options
MYSQLBIN=/usr/bin
MYSQL=$MYSQLBIN/mysql
MYSQLCMD="$MYSQL -h$SQLHOST -P$SQLPORT -u$SQLUSER -p$SQLPSWD"
MYSQLADMIN=$MYSQLBIN/mysqladmin
MYSQLADMINCMD="$MYSQLADMIN -h$SQLHOST -P$SQLPORT -u$SQLUSER -p$SQLPSWD"

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

echo  sqlhost=$SQLHOST sqlport=$SQLPORT sqluser=$SQLUSER sqlpswd=$SQLPSWD sqldbname=$SQLDB
echo  npasses=$NPASSES


log "Loading database [$SQLDB] (source [$SOURCE]) started"

HOST=$SQLHOST
USER=$SQLUSER
PASSWORD=$SQLPSWD
DB=$SQLDB
PORT=$SQLPORT


######################### DATABASE CREATION AND LOADING ###############################

log "Loading the database [$DB] [see list in ${UPDLOG}] "

log "Flush tables"
$MYSQLADMINCMD flush-tables

export MYSQL HOST USER PASSWORD DB PORT
export UNZIP1 FILTER LOADER
export LIST CURRENTSERIAL SOURCE
export PROPERTIES DEBUG

#cd ${SCRIPTDIR} && sh ./load_all.sh $NPASSES  2>${UPDLOG}
#sh $LOAD_FILE $NPASSES  2>&1 2> /dev/tty
sh $LOAD_FILE $NPASSES  2>&1 

if [ $? -ne 0 ]; then
  error_exit "Error loading database=$DB for source=${SOURCE}, $0 exiting"
fi

REMADMINCMD="$TOPDIR/bin/remadmin.pl -h $WHOISHOST -p $CONFPORT"
PAUSEQUERIES="$REMADMINCMD set queries pause"
RESUMEQUERIES="$REMADMINCMD set queries resume"
RESUMEUPDATES="$REMADMINCMD set updates resume"
INITRX="$REMADMINCMD set initrx $SOURCE"

# enable updates
DYNAMICCHECK="$REMADMINCMD show dynamic"
DYNAMICSTART="$REMADMINCMD set dynamic start"
log "checking dynamic updates"
DYNAMIC=`$DYNAMICCHECK`
log "dynamic = $DYNAMIC"

if [ $DYNAMIC -ne 1 ]; then
  log "enabling dynamic updates"
  $DYNAMICSTART
  if [ $? -ne 0 ]; then
    error "Error setting dynamic to start"
    error_exit "$0 now exiting"
  fi
fi

$PAUSEQUERIES
if [ $? -ne 0 ]; then
   error "Error stopping queries, attempting to resume queries"
   $RESUMEQUERIES
    if [ $? -ne 0 ]; then
      error "Error resuming queries"
    fi
    error_exit "$0 now exiting"
fi

$MYSQLADMINCMD flush-tables

ERROR=0
$INITRX
if [ $? -ne 0 ]; then
    error "Error reloading radix tree, $0 scheduling exit"
    ERROR=1
fi

# we should reset these, too
$REMADMINCMD set acl_tree
$REMADMINCMD set access_tree
$REMADMINCMD set aaa_cache

# at one point, initrx would stop updates - resume them here just in case
$RESUMEUPDATES
if [ $? -ne 0 ]; then
    error "Error resuming updates, $0 scheduling exit"
    ERROR=1
fi
$RESUMEQUERIES
if [ $? -ne 0 ]; then
    error "Error resuming queries, $0 exiting"
    ERROR=1
fi
if [ $ERROR -eq 1 ]; then
    exit 1
fi

log "Loading database [$SQLDB] (source [$SOURCE]) finished without errors"

