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

#usage: of2.leondev:epm, in dir cicd/hanascript
#./system_init.sh hana_sys hana_addr cons USER_NAME(upper) SYS_PASSWORD USER_PASSWORD REPLACE SKIP_INIT hdbsql_path
#初始化系统,生产环境
#./system_init.sh -SYS of2_hana1 -CONS true -USER_NAME DEMOCONS  -GIT_DIR '/home/epm/git/db'

#清空培训系统schema,初始化 schema
#./system_init.sh -SYS of2_hana1 -CONS true -USER_NAME DEMOCONS -REPLACE true -SKIP_INIT false  -GIT_DIR '/home/epm/git_build/db'

#create user, import from other schema
#./system_init.sh -SYS poc1 -CONS true -USER_NAME RESTORE_SCHEMA_TEST -REPLACE false -SKIP_INIT true  -GIT_DIR '/home/epm/git_build/db'

#private
#./system_init.sh -SYS others -HANA_ADDR '192.168.124.187:30115' -CONS true -USER_NAME DEMOCONS -REPLACE true

USER_PASSWORD='EPM_user_2017'
SYS_PASSWORD='Wang_2017'
REPLACE='false'
SKIP_INIT='false'
hdbsql='hdbsql'
GIT_DIR='.' #private

while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in    
    -SYS)
      SYS=$2
      shift 2
      ;;
    -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/"

#cd $DB_DIR
#git pull

case $SYS in
test1)
    HANA_ADDR='hanatest1.epmtest.cn:30115'
    ;;
test2)
    HANA_ADDR='hanatest2.epmtest.cn:30115'
    ;;
test3)
    HANA_ADDR='hanatest3.epmtest.cn:30115'
    ;;
prod2)
    HANA_ADDR='hana2.epmprod.cn:30115'
    ;;
poc1)
    HANA_ADDR='hanapoc1.epmtest.cn:30115'
    ;;
poc2)
    HANA_ADDR='hanapoc2.epmtest.cn:30115'
    ;;
poc3)
    HANA_ADDR='hanapoc3.epmtest.cn:30115'
    ;;
poc4)
    HANA_ADDR='hanapoc4.epmtest.cn:30115'
    ;;
poc5)
    HANA_ADDR='hanapoc5.epmtest.cn:30115'
    ;;
of2_hana1)
    HANA_ADDR='192.168.124.204:30115'
    ;;
others)
    echo 'use self defined hana address'
    ;;
*)
    echo unknown option >&2
    exit 1
esac

#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 $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR GRANT SELECT ON SCHEMA $USER_NAME TO READONLY_$USER_NAME

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"
