#!/bin/bash

# default values
dbrestore=false
prepare_dir=false
prep_cfg="bmsdev-merged.txt"
which_db="default"

i=0
i=$((i+1)); usage[$i]="Usage: $0 [-cd] [-r [report_testing]] [-p<cfg>]"
i=$((i+1)); usage[$i]="Where:"
i=$((i+1)); usage[$i]="\t c = Clear log files"
i=$((i+1)); usage[$i]="\t r = Restore database. Specify -r report_testing to restore that DB or else the default"
i=$((i+1)); usage[$i]="\t d = Run server with debug enabled"
i=$((i+1)); usage[$i]="\t p = Perform directory preparation using <cfg> (eg. specify -p$prep_cfg for cfgs/$prep_cfg)"

show_usage()
{
   for i in "${usage[@]}"; do
      echo -e $i
   done
}

# Sanity check to see if server is already running
./server.sh -s | grep STATUS_RUNNING > /dev/null 2>&1
rc=$?
if [ $rc -eq 0 ]; then
   echo "Server is *already* running. If you want to restart, use ./stop_server.sh first."
   exit 0
fi

# process options
while getopts "h?crdp:" opt; do
   case $opt in
      c  ) rm -f ./*.log* /usr/c-act/*.log* /usr/c-act/logs/*.log*
           ;;
      r  ) dbrestore=true
           # Check next positional parameter
           eval nextopt=\${$OPTIND}
           # existing or starting with dash?
           if [[ -n $nextopt && $nextopt != -* ]]; then
              OPTIND=$((OPTIND + 1))
              which_db=$nextopt
           fi
           ;;
      d  ) dbug="-d"
           ;;
      p  ) prepare_dir=true
           if [ -n $OPTARG ]; then prep_cfg=$OPTARG; fi
           ;;
      h | \
      \? ) show_usage && exit 1
           ;;
   esac
done
shift $(($OPTIND - 1))

if [ "$dbrestore" = true ]; then
   echo "** Restoring $which_db DB ..."
   pushd ../.. > /dev/null
   if [[ "$which_db" = "report_testing" ]] && [[ -d "data/dump_report_testing" ]]; then # We need to swap it
      swapem=true
      echo "** Swapping default and report_testing DB ..."
      mv data/dump data/dump.ori
      mv data/dump_report_testing data/dump
   fi
   ant import.db
   if [ $? -ne 0 ]; then
      rval=$?
      echo "** Master DB restore has failed!"
      impfail=true
   fi
   if [ "$swapem" = true ]; then
      echo "** Un-swapping default and report_testing DB ..."
      mv data/dump data/dump_report_testing
      mv data/dump.ori data/dump
   fi
   popd > /dev/null
   if [ "$impfail" = true ]; then
      exit rval
   fi
else
   echo "** Not performing master DB restore."
fi

if [ "$prepare_dir" = true ]; then
   # Setup templates if we are restoring a fresh database, otherwise just leave the configuration alone
   # This means it is up to the user to configure their directory.xml files prior to running.
   if [ -f "cfgs/$prep_cfg" ]; then
      ./prepare_dir.sh <cfgs/$prep_cfg
   else
      echo "** Cannot locate cfgs/$prep_cfg for directory prepare!"
      exit 1
   fi
   if [ $? -ne 0 ]; then
      echo "** Directory prepare has failed!"
      exit $?
   fi
else   
   echo "** Bypassing directory preparation."
fi   

echo "** Starting server..."
./server.sh $dbug &

echo "** Waiting for server to start..."
./server.sh -w
server_ready=$?

if [ $server_ready -ne 0 ]; then
   echo "Server could not be started!"
   exit 1
else
   # Secondary wait for appservers. Messages are:
   echo "** Initial server startup complete. Awaiting app servers..."
   apps_cact=" Appserver 'cactAS1' was started successfully ("
   apps_mob=" Appserver 'mobAS1' was started successfully ("
   logfile=/usr/c-act/logs/server_0.log
   cnt=0
   tail -f $logfile |
   while IFS= read line; 
   do
      if [[ "$line" == *"$apps_cact"* ]] || [[ "$line" == *"$apps_mob"* ]]; then
         cnt=$((cnt+1))
         echo "...Appserver $cnt found"
      fi
      if [ $cnt -eq 2 ]; then
         pkill tail;
      fi;
   done

   echo "** Server started successfully!"
fi

exit 0
