#!/bin/sh
#
# cgmkdb
# ------
# Utility script to generate an empty cego database with one tableset.
#
# (C)opyright 2000-2024 by Bjoern Lemke
#
# The database and tableset parameters defined below
# should be edited and customized to appropriate values.
#
# Thanks to Keve Nagy, this script has been expanded and improved very much
#

DEFPAGESIZE=16384
DEFHOSTNAME=`hostname`
DEFADMPORT=2000
DEFDBPORT=2200
DEFLOGPORT=3000
DEFPROTOCOL=fastserial
DEFCSMODE=ID
DEFQESCMODE=ON

ECHO=/bin/echo

printUsage() {
  echo "Usage : $0 [ -a ]"
}

ADVANCED=N

while getopts "ha" OPTLET; do
  case $OPTLET in
     h) printUsage; exit 0;;
     a) ADVANCED=Y;;
     ?) printUsage; exit 1;;
  esac
done


$ECHO "==========================="
$ECHO "Cego Database Configuration"
$ECHO "==========================="


if [ $ADVANCED = "Y" ]
then
    $ECHO "Advanced mode used for extended parameter setting"    
else
    $ECHO "Basic mode used, use -a for advanced parameter setting"
fi

$ECHO ""

$ECHO "Database Part"
$ECHO "-------------"


if [ $ADVANCED = "Y" ]
then

    doAsk=true
    while $doAsk
    do
    read -p "Enter page size [$DEFPAGESIZE]: "  PAGESIZE
    PAGESIZE=${PAGESIZE:-$DEFPAGESIZE}
    if  [ -z "${PAGESIZE##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter admin port [$DEFADMPORT]: "  ADMPORT
    ADMPORT=${ADMPORT:-$DEFADMPORT}
    if  [ -z "${ADMPORT##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter db port [$DEFDBPORT]: "  DBPORT
    DBPORT=${DBPORT:-$DEFDBPORT}
    if  [ -z "${DBPORT##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter log port [$DEFLOGPORT]: "  LOGPORT
    LOGPORT=${LOGPORT:-$DEFLOGPORT}

    if  [ -z "${LOGPORT##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    read -p "Enter hostname [$DEFHOSTNAME]: "  HOSTNAME
    HOSTNAME=${HOSTNAME:-$DEFHOSTNAME}

    doAsk=true
    while $doAsk
    do
    read -p "Enter protocol (xml,serial,fastserial) [$DEFPROTOCOL]: "  PROTOCOL
    PROTOCOL=${PROTOCOL:-$DEFPROTOCOL}

    if [ "$PROTOCOL" = "serial" ] \
        || [ "$PROTOCOL" = "xml" ] \
        || [ "$PROTOCOL" = "fastserial" ]
    then
        doAsk=false
    else
        $ECHO "Invalid protocol $PROTOCOL"
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter csmode (ID,STR,NONE) [$DEFCSMODE]: "  CSMODE
    CSMODE=${CSMODE:-$DEFCSMODE}

    if [ "$CSMODE" = "ID" ] \
        || [ "$CSMODE" = "STR" ] \
        || [ "$CSMODE" = "NONE" ]
    then
        doAsk=false
    else
        $ECHO "Invalid csmode $CSMODE"
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter qescmode (ON,OFF) [$DEFQESCMODE]: "  QESCMODE
    QESCMODE=${QESCMODE:-$DEFQESCMODE}

    if [ "$QESCMODE" = "ON" ] \
        || [ "$QESCMODE" = "OFF" ]
    then
        doAsk=false
    else
        $ECHO "Invalid qescmode $QESCMODE"
    fi
    done

else
    PAGESIZE=$DEFPAGESIZE
    HOSTNAME=$DEFHOSTNAME
    ADMPORT=$DEFADMPORT
    DBPORT=$DEFDBPORT
    LOGPORT=$DEFLOGPORT
    PROTOCOL=$DEFPROTOCOL
    CSMODE=$DEFCSMODE
    QESCMODE=$DEFQESCMODE
fi

DEFDBNAME=cgdb
DEFDBUSER=cego
DEFTABLESET=TS1

read -p "Enter database name [$DEFDBNAME]: "  DBNAME
DBNAME=${DBNAME:-$DEFDBNAME}

DEFROOTPATH=/usr/local/$DBNAME


doAsk=true
while $doAsk
do
    read -p "Enter root path [$DEFROOTPATH]: "  ROOTPATH
    ROOTPATH=${ROOTPATH:-$DEFROOTPATH}

    if [ -d $ROOTPATH ]
    then
    doAsk=false
    else
    $ECHO "Database root directory $ROOTPATH must exist."

    fi
done


read -p "Enter tableset name  [$DEFTABLESET]: "  TABLESET
TABLESET=${TABLESET:-$DEFTABLESET}

TSROOT=${ROOTPATH}/${DBNAME}_data

CEGO=`which cego`;               ## path to the cego executable
DBXML=${ROOTPATH}/${DBNAME}.xml;   ## name of the database xml file
LOGFILE=${ROOTPATH}/${DBNAME}.log
LOCKFILE=${ROOTPATH}/${DBNAME}.lock
MAINARGS="--dbxml=${DBXML} --logfile=${LOGFILE} --lockfile=${LOCKFILE}"

if [ -f $DBXML ]
then
    $ECHO ""
    $ECHO "Detected existing database configuration file ${DBXML}"
    $ECHO "Tableset will be just added to it."
    $ECHO ""
    # $ECHO "Please note : Additional tableset still has to be added manually to the existing startup script"

else

    read -p "Enter database user name [$DEFDBUSER]: "  DBUSER
    DBUSER=${DBUSER:-$DEFDBUSER}

    read -p "Enter database user password [$DBUSER]: "  DBPWD
    DBPWD=${DBPWD:-$DBUSER}

    $ECHO "Creating database $DBNAME in directory $ROOTPATH ..."

    PIDFILE=${ROOTPATH}/${DBNAME}.pid
    ADMINUSER=cgadm
    ADMINPWD=cgadm
    DEBUGLEVEL=NOTICE
    DBSTARTSCRIPT=${ROOTPATH}/${DBNAME}_start
    DBSTOPSCRIPT=${ROOTPATH}/${DBNAME}_stop

    $ECHO -n " * Generating database configuration xml ... "
    ${CEGO} --mode=init --dbname=${DBNAME} --hostname=${HOSTNAME} --pgsize=${PAGESIZE} --dbport=${DBPORT} --admport=${ADMPORT} --logport=${LOGPORT} --pidfile=${PIDFILE} --csmode=${CSMODE} --qescmode=${QESCMODE} --loglevel=${DEBUGLEVEL} ${MAINARGS}
    test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }

    $ECHO -n " * Creating admin user for the database ... "
    ${CEGO} --mode=adduser --user=${ADMINUSER}/${ADMINPWD} --role=admin ${MAINARGS}
    test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }

    $ECHO -n " * Creating role ALL ... "
    ${CEGO} --mode=addrole --role=ALL ${MAINARGS}
    test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }

    $ECHO -n "   + Creating role permission ... "
    ${CEGO} --mode=addperm --permid=${TABLESET}_P --role=ALL --tableset=${TABLESET} --filter="ALL" --perm="ALL"  ${MAINARGS}
    test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }

    $ECHO -n "   + Creating user:${DBUSER} ... "
    ${CEGO} --mode=adduser --user=${DBUSER}/${DBPWD} --role=ALL ${MAINARGS}
    test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }

    mkdir ${TSROOT}
    test $? != 0 && { $ECHO "Cannot create tableset root directory."; exit 1; }

    $ECHO -n " * Creating database start script ... "
    cat << _EOF_ > ${DBSTARTSCRIPT}
#!/bin/sh
$ECHO "Starting cego database-instance:${DBNAME} with tableset:${TABLESET} ..."
cego --mode=daemon ${MAINARGS} --tableset=${TABLESET} --pidfile=${PIDFILE} --protocol=${PROTOCOL}
sleep 2
$ECHO ""
_EOF_
    test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }
    chmod 755 ${DBSTARTSCRIPT}

    $ECHO -n " * Creating database stop script ... "
    cat << _EOF_ > ${DBSTOPSCRIPT}
#!/bin/sh
$ECHO "Stopping cego database-instance:${DBNAME} ..."
if [ -e ${PIDFILE} ]
then
   myPID=\`cat ${PIDFILE}\`
   /bin/$ECHO -n " * Signaling PID:\${myPID} ... "
   kill -INT \${myPID} || { $ECHO "Failed to signal PID:\${myPID}."; exit 1; }
   $ECHO "Done."
   $ECHO " * Waiting for lockfile to clear ..."
   $ECHO '   This takes a few seconds. Please be patient!'
   myTimeout=1;
   while test -e ${LOCKFILE}
   do
      test \${myTimeout} -ge 60 && { $ECHO "Timeout reached. Exiting."; $ECHO "Please check for existing cego daemon."; exit 1; }
      sleep 1
      myTimeout=\`expr \${myTimeout} + 1\`;
   done
   $ECHO "   Lockfile cleared in \${myTimeout} seconds."
   /bin/$ECHO -n " * Housekeeping ... "
   test -e ${PIDFILE} && rm ${PIDFILE}
   test \$? -eq 0 && $ECHO "Done." || { $ECHO FAILED; exit 1; }
   $ECHO "Database shutdown complete."
   $ECHO ""
else
   $ECHO "No pidfile ${PIDFILE} found."
fi
_EOF_
    test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }
    chmod 755 ${DBSTOPSCRIPT}
    $ECHO "Done."
    $ECHO ""

fi

$ECHO "Tableset Part"
$ECHO "-------------"

TSTICKET=${ROOTPATH}/tsticket.xml
DEFSYSSIZE=100
DEFTMPSIZE=3000
DEFAPPSIZE=3000
DEFLOGFILESIZE=1000000
DEFLOGFILENUM=3
DEFSORTAREASIZE=10000000


if [ $ADVANCED = "Y" ]
then

    doAsk=true
    while $doAsk
    do
    read -p "Enter system size [$DEFSYSSIZE]: "  SYSSIZE
    SYSSIZE=${SYSSIZE:-$DEFSYSSIZE}

    if  [ -z "${SYSSIZE##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter temp size [$DEFTMPSIZE]: "  TMPSIZE
    TMPSIZE=${TMPSIZE:-$DEFTMPSIZE}

    if  [ -z "${TMPSIZE##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter application size [$DEFAPPSIZE]: "  APPSIZE
    APPSIZE=${APPSIZE:-$DEFAPPSIZE}

    if  [ -z "${APPSIZE##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter sort area size [$DEFSORTAREASIZE]: " SORTAREASIZE
    SORTAREASIZE=${SORTAREASIZE:-$DEFSORTAREASIZE}

    if  [ -z "${SORTAREASIZE##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter log file size [$DEFLOGFILESIZE]: "  LOGFILESIZE
    LOGFILESIZE=${LOGFILESIZE:-$DEFLOGFILESIZE}

    if  [ -z "${LOGFILESIZE##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

    doAsk=true
    while $doAsk
    do
    read -p "Enter log file num [$DEFLOGFILENUM]: "  LOGFILENUM
    LOGFILENUM=${LOGFILENUM:-$DEFLOGFILENUM}

    if  [ -z "${LOGFILENUM##*[!0-9]*}" ];
    then
        $ECHO "Sorry integers only"
    else
        doAsk=false
    fi
    done

else
    SYSSIZE=$DEFSYSSIZE
    TMPSIZE=$DEFTMPSIZE
    SORTAREASIZE=$DEFSORTAREASIZE
    LOGFILESIZE=$DEFLOGFILESIZE
    LOGFILENUM=$DEFLOGFILENUM
fi

$ECHO " * Creating a tableset ... "
$ECHO -n "   + Defining tableset:${TABLESET} ... "
${CEGO} --mode=define ${MAINARGS} --tableset=${TABLESET} --tsdef="tsroot=${TSROOT},tsticket=${TSTICKET},syssize=${SYSSIZE},tmpsize=${TMPSIZE},logfilesize=${LOGFILESIZE},logfilenum=${LOGFILENUM},appsize=${APPSIZE},sortareasize=${SORTAREASIZE}"
test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }

$ECHO -n "   + Creating tableset:$TABLESET ... "
${CEGO} --mode=create ${MAINARGS} --tableset=${TABLESET}
test $? -eq 0 && $ECHO OK || { $ECHO FAILED; exit 1; }

$ECHO "Done."
$ECHO ""
