#!/bin/bash
echo "start hana db update view"

#main process
# check git changed file
# 1. create tmp schema using latest sql init file
# 2. compare tmp schema with target schema, do the update
HANA_ADDR=$2
#INST_ID=$3
SYS_PASSWORD=$3
USER_PASSWORD=$4
SCHEMA_LIST=$5
EXECUTE=$6
CONS=$7
GIT_CHANGE=$8

#OUT_LOG_PATH=$10
if [[ $OUT_LOG_PATH ]]; then
  echo "env OUT_LOG_PATH: $OUT_LOG_PATH"
else
  OUT_LOG_PATH='./logs/'
fi

FILE_CHANGED=""
if [ "$GIT_CHANGE" = true ]; then
    # file changed in last commit, should not use this in weekly update 
	# as in a week there could be many commits
	# should be used for per push commit
	FILE_CHANGED=`git diff --name-only HEAD~ HEAD`
	echo "FILE_CHANGED: $FILE_CHANGED"
fi


#SYS_PASSWORD='Wang_2017'

GIT_DIR="../"
#USER_PASSWORD="EPM_user_2017"

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

if [ "$CONS" = true ]; then
	TMP_NAME="EPM_TMP_CONS"
	echo "For Consolidation SYSTEM"
	CREATE_TABLE_SCRIPTS1="consolidation.sql"
    CREATE_TABLE_SCRIPTS="${GIT_DIR}consolidation.sql"
else
	TMP_NAME="EPM_TMP"
	echo "For Planning SYSTEM"
	CREATE_TABLE_SCRIPTS1="createTable.sql"
    CREATE_TABLE_SCRIPTS="${GIT_DIR}createTable.sql"	
fi

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"

if [[ "$GIT_CHANGE" = false || $FILE_CHANGED =~ $CREATE_TABLE_SCRIPTS1 ]]
then   
	echo "1st update table"
	echo "drop tmp and recreate tmp:$TMP_NAME"
	hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR drop user $TMP_NAME cascade

	echo "CREATE USER $TMP_NAME"
	hdbsql -u SYSTEM -p $SYS_PASSWORD -n $HANA_ADDR CREATE USER $TMP_NAME PASSWORD ${USER_PASSWORD} NO FORCE_FIRST_PASSWORD_CHANGE;

	echo "create TMP Table"
	hdbsql -u $TMP_NAME -p ${USER_PASSWORD} -n $HANA_ADDR -I $CREATE_TABLE_SCRIPTS

	echo "table update start"
	#usage node xx.js exeute sql_file hanaInst schemaList
	node $NODE_FILE $EXECUTE $CREATE_TABLE_SCRIPTS $HANA_ADDR $SCHEMA_LIST $OUT_LOG_PATH $CONS
	echo "table update done"
else
	echo 'skip update table as no valid git chanage'
fi


if [[ "$VIEW_RECREATE" = true ]]
then
    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=(${SCHEMA_LIST//,/ })
	echo $arr
	for USER_NAME in ${arr[@]}
	do
		echo $USER_NAME	
		
		TMP_DIR="${OUT_LOG_PATH}tmp/"
		EXEC_SP_SCRIPTS="${TMP_DIR}execProcedure.sql"
		EXEC_VIEW_SCRIPTS="${TMP_DIR}execView.sql"

		mkdir ${TMP_DIR}

		if [[ "$GIT_CHANGE" = false || $FILE_CHANGED =~ $CREATE_SP_SCRIPTS1 ]]
		then
			echo "2nd update procedure"   
			sed "s|EPM|$USER_NAME|g" $CREATE_SP_SCRIPTS > $EXEC_SP_SCRIPTS
			echo "hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR -I $EXEC_SP_SCRIPTS"
			hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR -I $EXEC_SP_SCRIPTS		
		else
			echo 'skip update procedure'
		fi

		if [[ "$GIT_CHANGE" = false || $FILE_CHANGED =~ $CREATE_VIEW_SCRIPTS1 ]]
		then
			echo "3rd update view"
			sed "s|EPM|$USER_NAME|g" $CREATE_VIEW_SCRIPTS > $EXEC_VIEW_SCRIPTS
			echo "hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR -I $EXEC_VIEW_SCRIPTS"
			hdbsql -u $USER_NAME -p $USER_PASSWORD -n $HANA_ADDR -I $EXEC_VIEW_SCRIPTS	
		else
			echo 'skip update view'
		fi	
	done
fi

echo "db update done"