#!/bin/bash
#run this after dbupdateAll.js, so backup is done
#./dbUpdateView.sh -hanaInst of1 -schemaList BEGINNER100,YATALTEST -cons
#./dbUpdateView.sh -hanaInst others -hanaAddr xxx:xxx -schemaList BEGINNER100,YATALTEST -cons -sysPass xxxx -userPass xxxx
echo "start hana db update view"

sysPass='Wang_2017'
userPass="EPM_user_2017"
GIT_DIR="../"
TMP_DIR='./tmp/'
while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in
    -hanaInst)
      hanaInst=$2
      shift 2
      ;;
    -hanaAddr)
      hanaAddr=$2
      shift 2
      ;;
    -sysPass)
      sysPass=$2
      shift 2
      ;;
    -userPass)
      userPass=$2
      shift 2
      ;;
    -schemaList)
      schemaList=$2
      shift 2
      ;;
    -cons)
      cons=true
      shift
      ;;
    *)
      break
      ;;
  esac
done

case $hanaInst in
prod1)
    hanaAddr='hana1.epmprod.cn:30115'
    ;;
prod2)
    hanaAddr='hana2.epmprod.cn:30115'
    ;;
poc1)
    hanaAddr='hanapoc1.epmtest.cn:30115'
    ;;
poc2)
    hanaAddr='hanapoc2.epmtest.cn:30115'
    ;;
poc3)
    hanaAddr='hanapoc3.epmtest.cn:30115'
    ;;
of1)
    hanaAddr='www.epmsquare.cn:30515'
    ;;
of2)
    hanaAddr='www.epmsquare.cn:30415'
    ;;
others)
    echo 'use self defined hana address'
    ;;
*)
    echo unknown option >&2
    exit 1
esac

echo "input information: $*"
#echo "input information: HANA_ADDR: $HANA_ADDR, EXECUTE:$EXECUTE,OUT_LOG_PATH:$OUT_LOG_PATH,CONS:$CONS,GIT_CHANGE:$GIT_CHANGE,SCHEMA_LIST: $SCHEMA_LIST"

CREATE_SP_SCRIPTS1="createProcedure.sql"
CREATE_SP_SCRIPTS="${GIT_DIR}createProcedure.sql"
if [ "$cons" = true ]; then	
    CREATE_VIEW_SCRIPTS1="consolidationView.sql"
    CREATE_VIEW_SCRIPTS="${GIT_DIR}consolidationView.sql"
else
    CREATE_VIEW_SCRIPTS1="createView.sql"
    CREATE_VIEW_SCRIPTS="${GIT_DIR}createView.sql"
fi
#str="i,like,you,csdn"
arr=(${schemaList//,/ })
echo $arr
for USER_NAME in ${arr[@]}
do
    echo $USER_NAME	
    
    EXEC_SP_SCRIPTS="${TMP_DIR}execProcedure.sql"
    EXEC_VIEW_SCRIPTS="${TMP_DIR}execView.sql"

    #mkdir ${TMP_DIR}
    echo "2nd update procedure"   
    sed "s|EPM|$USER_NAME|g" $CREATE_SP_SCRIPTS > $EXEC_SP_SCRIPTS
    echo "hdbsql -u $USER_NAME -p $userPass -n $hanaAddr -I $EXEC_SP_SCRIPTS"
    hdbsql -u $USER_NAME -p $userPass -n $hanaAddr -I $EXEC_SP_SCRIPTS	
    echo "update procedure done for ${USER_NAME}" 

    echo "3rd update view"
    sed "s|EPM|$USER_NAME|g" $CREATE_VIEW_SCRIPTS > $EXEC_VIEW_SCRIPTS
    echo "hdbsql -u $USER_NAME -p $userPass -n $hanaAddr -I $EXEC_VIEW_SCRIPTS"
    hdbsql -u $USER_NAME -p $userPass -n $hanaAddr -I $EXEC_VIEW_SCRIPTS	
    echo "update view done  for ${USER_NAME}" 

done

echo "db view update done"