#!/bin/sh
#
# Script: enl_load.sh
#
# This script loads data from flat files created with enl_dump.sh into
# an existing enlighten database.
# To use this script:
# 1. It must be run by user "dbenl"
# 2. There must not be any open connections to the database; therefore, the EMD
#    should not be running. To ensure that no events data is lost, PEP
#    should not be running either. The events daemons will cache data until
#    PEP is reachable again. PEP does not cache data.
#
# (c) 1996 ENlighten Software Solutions, Inc.
# All Rights Reserved.
#

curr_dir=`pwd`
command_file=ENL_CMDS.TXT

echo "This script will load data from flat files created with 'ENL_DUMP.SH'"
echo "into an existing ENlighten database."
echo ""
echo "Enter the full path name of the directory which contains"
echo "the 'enlighten.exp' sub-directory created with 'ENL_DUMP.SH' or enter"
echo "q to quit:"

while true
do
	read indir

	if [ "$indir" = "q" -o "$indir" = "Q" ] ; then
		exit 1
	fi

	if [ ! "$indir" ] ; then
		echo "Enter the full path name of the directory which contains"
		echo "the 'enlighten.exp' sub-directory created with 'ENL_DUMP.SH'"
		echo "or enter q to quit:"
	elif [ ! -d "$indir" ] ; then
		echo "Directory $indir does not exist."
		echo "Please re-enter or type 'q' to exit:"
	elif [ ! -d "$indir"/enlighten.exp ] ; then
		echo "$indir/enlighten.exp does not exist."
		echo "Are you \"$indir\" is correct?"
		echo "Re-enter or type 'q' to exit:"
	else
		break
	fi
done

if [ ! -f $curr_dir/$command_file ] ; then
	echo "Unable to find $curr_dir/$command_file"
	echo "Please copy the file \"$command_file\" to your current working"
	echo "directory and try again."
	exit 1
fi

echo "Copying file \"$command_file\" to $indir/enlighten.exp..."
cp $curr_dir/$command_file $indir/enlighten.exp

if [ ! -f $indir/enlighten.exp/$command_file ] ; then
	echo "Unable to copy \"$command_file\""
	echo "Aborting..."
	exit 1
fi

echo "delete from pool_tbl;delete from pool_entry_tbl;delete from prefer_tbl;delete from auth_tbl;delete from template_tbl;delete from expire_tbl;commit;" > tmp.sql
dbaccess enlighten tmp 2> $curr_dir/enl_load.log
rm tmp.sql

echo "Extracting data from $indir/enlighten.exp..."

cd $indir/enlighten.exp
dbload -c $command_file -d enlighten -l $curr_dir/enl_load.log -e 100 1> /dev/null
echo "Done loading data. Errors, if any, will be found in $curr_dir/enl_load.log
			
