#!/bin/sh
echo "Welcome to the SysAid initialization script!";
echo "This script will configure the database connection and initialize the database.";
if [ $# -ne 1 ]; then
  echo "Usage: init_db.sh <root sysaid directory> "
  exit 1;
fi
SYSAID_HOME=$1
if [ ! -s "$SYSAID_HOME/WEB-INF/conf/sysaid.ver" ]; then
  echo "Root SysAid directory not found at $SYSAID_HOME. Please deploy the SysAid web application into the Tomcat server before running this script.";
  exit 1;
fi
JAVA_BIN=`which java`;
if [ "$?" -ne 0 ]; then
  # Complain and quit
  echo "Could not find java executable. Please make sure the java command is in your path."
  exit 1;
fi

CP=
for i in ${SYSAID_HOME}/WEB-INF/lib/*.jar ; do
    CP=$i:${CP}
done
export CP

DBHOST="localhost";
DBNAME="ilient";
DBUSER="mysql";
DBPASSWORD="mysql";
UNICODE="Y";

CONNSTATUS="1";


while ["" eq ""]; do
  echo "Please enter the host name or IP address of the MySQL server [$DBHOST]:";
  read answer
  if [ "$answer" != "" ]; then
    DBHOST="$answer";
  fi
  echo "Please enter the database name that should contain the SysAid data (please create an empty database with this name) [$DBNAME]:";
  read answer
  if [ "$answer" != "" ]; then
    DBNAME="$answer";
  fi
  echo "Please enter the database login user name [$DBUSER]:";
  read answer
  if [ "$answer" != "" ]; then
    DBUSER="$answer";
  fi
  echo "Please enter the database login password [$DBPASSWORD]:";
  read answer
  if [ "$answer" != "" ]; then
    DBPASSWORD="$answer";
  fi


  echo "Please confirm your input:";
  echo "Host name: $DBHOST";
  echo "Database name: $DBNAME";
  echo "Database user name: $DBUSER";
  echo "Database password: $DBPASSWORD";
  echo "Would you like to proceed (y/n) ?";
  read cont
  if [ "$cont" != "Y" ] && [ "$cont" != "y" ]; then
    continue
  fi  
  echo "Would you like to check connection (y/n) ?"
  read ischeck
  if [ "$ischeck" != "Y" ] && [ "$ischeck" != "y" ]; then
    $JAVA_BIN -cp ${CP} com.ilient.server.CheckConf "$SYSAID_HOME" "$SYSAID_HOME/WEB-INF/logs/initdb.log" "org.gjt.mm.mysql.Driver" "jdbc:mysql://$DBHOST/$DBNAME" "$DBUSER" "$DBPASSWORD" "mysql" $UNICODE
    break;  
  fi
  echo "Checking connection....";
  $JAVA_BIN -cp ${CP} com.ilient.server.CheckConf "$SYSAID_HOME" "$SYSAID_HOME/WEB-INF/logs/initdb.log" "org.gjt.mm.mysql.Driver" "jdbc:mysql://$DBHOST/$DBNAME" "$DBUSER" "$DBPASSWORD" "mysql" $UNICODE
  CONNSTATUS="$?";
  if [ "$CONNSTATUS" -ne 0 ]; then
    # Complain and quit
    echo "Connection verified."
    break;
  fi
  CHECKOUT=`cat $SYSAID_HOME/WEB-INF/logs/initdb.log`
  echo "Error while checking connection: $CHECKOUT"
done

echo "Validating license ....."; 
$JAVA_BIN -cp ${CP} com.ilient.server.CheckLicense "$SYSAID_HOME" "$SYSAID_HOME/WEB-INF/logs/checklic.log"
if [ "$?" -eq 0 ]; then
    # Complain and quit
    CHECKOUT=`cat $SYSAID_HOME/WEB-INF/logs/checklic.log`
    echo "FATAL: $CHECKOUT"
    exit 1;
fi
CHECKOUT=`cat $SYSAID_HOME/WEB-INF/logs/checklic.log | sed -e 's/accountID=//' | sed -e 's/serial=//'`
#echo "$CHECKOUT";
set -- $CHECKOUT;

ACCOUNT="$1";
SERIAL="$2";

echo "License verified. Account ID is $ACCOUNT. Serial is $SERIAL"

if [ $CONNSTATUS -eq 1 ]; then

  USER="sysaid";
  echo "Please choose a main user name. Use this login for your first login into SysAid [$USER]:"
  read answer
  if [ "$answer" != "" ]; then
    USER="$answer";
  fi

  PASSWORD="changeit";
  echo "Please choose the password of the $USER login [$PASSWORD]:"
  read answer
  if [ "$answer" != "" ]; then
    PASSWORD="$answer";
  fi

  echo "Initializing database...."
  $JAVA_BIN -cp ${CP} com.ilient.server.InitAccount "$SYSAID_HOME" "$ACCOUNT" "$SERIAL" "$USER" "$PASSWORD" "$CONNSTATUS"
  if [ "$?" -eq 0 ]; then
      # Complain and quit    
      echo "FATAL error while initializing database. Please consult the SysAid log at $SYSAID_HOME/WEB-INF/logs/sysaid.log for more information. "
      exit 1;
  fi

fi

echo "Initialization complete! Please restart the web application server (Tomcat). Welcome to SysAid!";

