#!/bin/bash
echo "start hana db initialization"

#private
#./system_init.sh -USER_PASSWORD EPM_user_2017 -SYS_PASSWORD Wang_2017 -HANA_ADDR "127.0.0.1:30115" -CONS true -USER_NAME EPM -GIT_DIR db

# short/private
#./system_init.sh


USER_PASSWORD='EPM_user_2022'
SYS_PASSWORD='EPM_user_2022'
REPLACE='true'
SKIP_INIT='false'
hdbsql='hdbsql'
GIT_DIR='db' #private
HANA_ADDR="127.0.0.1:30115"
CONS='true'
USER_NAME='EPM'
while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in
    -HANA_ADDR)
      HANA_ADDR=$2
      shift 2
      ;;
    -CONS)
      CONS=$2
      shift 2
      ;;
    -USER_NAME)
      USER_NAME=$2
      shift 2
      ;;
    -SYS_PASSWORD)
      SYS_PASSWORD=$2
      shift 2
      ;;
    -USER_PASSWORD)
      USER_PASSWORD=$2
      shift 2
      ;;
    -REPLACE) #if schema exists, whether replace it or not
      REPLACE=$2
      shift 2
      ;;
    -SKIP_INIT)
      SKIP_INIT=$2
      shift 2
      ;;
    -hdbsql)
      hdbsql=$2
      shift 2
      ;;
    -GIT_DIR)
      GIT_DIR=$2
      shift 2
      ;;
    *)
      break
      ;;
  esac
done

DB_DIR="$GIT_DIR/"
TMP_DIR="$GIT_DIR/tmp/"

#REPLACE USER
if [ "$REPLACE" = true ]; then
    echo "drop user/schema if exist,$USER_NAME"
    echo "hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR drop user $USER_NAME cascade"
    $hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR drop user $USER_NAME cascade
    $hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR drop user READONLY_$USER_NAME cascade
fi

echo "CREATE USER"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR CREATE USER $USER_NAME PASSWORD $USER_PASSWORD NO FORCE_FIRST_PASSWORD_CHANGE
if [ $? != "0" ]; then
    echo "user may exist, CREATE user fails, skip the rest actions"
    exit 0
fi
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR ALTER USER $USER_NAME DISABLE PASSWORD LIFETIME
echo "GRANT IMPORT/export"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR GRANT IMPORT TO $USER_NAME
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR GRANT EXPORT TO $USER_NAME
#readonly account
echo "CREATE readonly USER"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR CREATE USER READONLY_$USER_NAME PASSWORD $USER_PASSWORD NO FORCE_FIRST_PASSWORD_CHANGE
if [ $? != "0" ]; then
    echo "user may exist, CREATE user fails, skip the rest actions"
    exit 0
fi
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR ALTER USER READONLY_$USER_NAME DISABLE PASSWORD LIFETIME
$hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR GRANT SELECT ON SCHEMA $USER_NAME TO READONLY_$USER_NAME

echo "sys config password policy"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "ALTER SYSTEM ALTER CONFIGURATION ('indexserver.ini', 'SYSTEM') SET ('password policy', 'detailed_error_on_connect') = 'TRUE' WITH reconfigure"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "ALTER SYSTEM ALTER CONFIGURATION ('indexserver.ini', 'SYSTEM') SET ('password policy', 'maximum_password_lifetime') = '65536' WITH reconfigure"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "ALTER SYSTEM ALTER CONFIGURATION ('indexserver.ini', 'SYSTEM') SET ('password policy', 'password_lock_for_system_user') = 'FALSE' WITH reconfigure"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "ALTER SYSTEM ALTER CONFIGURATION ('indexserver.ini', 'SYSTEM') SET ('password policy', 'force_first_password_change') = 'FALSE' WITH reconfigure"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "ALTER SYSTEM ALTER CONFIGURATION ('indexserver.ini','SYSTEM') set ('password policy','last_used_passwords')='0' with reconfigure"
echo "sys config mem"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "alter system alter configuration('indexserver.ini','SYSTEM') set('sql', 'plan_cache_enabled') = 'FALSE' with reconfigure"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "alter system alter configuration('global.ini','SYSTEM') set('resource_tracking', 'enable_tracking') = 'FALSE' with reconfigure"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "alter system alter configuration('indexserver.ini','SYSTEM') set('sqlscript', 'disable_sqlscript_rule_based_checker') = 'true' with reconfigure"
$hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR "ALTER SYSTEM ALTER configuration ('global.ini', 'SYSTEM') SET ('memorymanager', 'statement_memory_limit') = '0' WITH reconfigure"

if [ "$SKIP_INIT" = true ]; then
    echo "db user create done"
    exit 0
fi

EXEC_SP_SCRIPTS="${TMP_DIR}execProcedure.sql"
EXEC_VIEW_SCRIPTS="${TMP_DIR}execView.sql"

if [[ -e $TMP_DIR ]]; then
  echo 'tmp exist'
else
  echo 'tmp does not exist, create it'
  mkdir $TMP_DIR
  chmod -R a+x $TMP_DIR
fi

if [ "$CONS" = true ]; then
        echo "For Consolidation SYSTEM"
        CREATE_TABLE_SCRIPTS="${DB_DIR}consolidation.sql"
        CREATE_SP_SCRIPTS="${DB_DIR}consolidationFunction.sql"
        CREATE_VIEW_SCRIPTS="${DB_DIR}consolidationView.sql"
        INIT_TABLE_SCRIPTS="${DB_DIR}initialTables_cons.sql"
else
        echo "For Planning SYSTEM"
        CREATE_TABLE_SCRIPTS="${DB_DIR}createTable.sql"
        CREATE_SP_SCRIPTS="${DB_DIR}createProcedure.sql"
        INIT_TABLE_SCRIPTS="${DB_DIR}initialTables.sql"
        CREATE_VIEW_SCRIPTS="${DB_DIR}createView.sql"
fi


echo "create Table"
$hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR -I $CREATE_TABLE_SCRIPTS

echo "create procedure"
sed "s|EPM|$USER_NAME|g" $CREATE_SP_SCRIPTS > $EXEC_SP_SCRIPTS
$hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR -I $EXEC_SP_SCRIPTS

echo "initialize table"
sed "s|KEYFUNC|$USER_NAME|g" $INIT_TABLE_SCRIPTS > $EXEC_VIEW_SCRIPTS
$hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR -I $EXEC_VIEW_SCRIPTS

echo "create view"
sed "s|EPM|$USER_NAME|g" $CREATE_VIEW_SCRIPTS > $EXEC_VIEW_SCRIPTS
$hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR -I $EXEC_VIEW_SCRIPTS

echo "db initialization done"