#!/bin/bash
# if appdir exist, overwrite
# if need only update, use update app

# private env:  -name etl/web
# # install only etl: ./app.sh -appName etl -custName xxx -mongoHost 192.168.124.248 -hanaHost 192.168.124.248 -installPath /data_disk/EPM2 > app.log 2>&1

#install all
# private env: ./appHana.sh -webPort 3000 -devPort 3100 -custName xxx -mongoHost 192.168.124.248 -hanaHost 192.168.124.248 -installPath /data_disk/EPM2 > app.log 2>&1

# pg: ./app.sh -webPort 3000 -devPort 3100^CpgPort 5432 -mdxPort 9010 -pgHost 192.168.124.135 -mongoHost 192.168.124.131 -mongoPort 27018 -custName msinspg -installPath /data_disk/EPM2 

#set -xe
set -x

dateStr=`date '+%Y%m%d-%H%M%S'`

appTarfile=''

echo "install epm app $dateStr"
baseDir=`pwd`
echo $baseDir

#default to private
#installPath='xxx/EPM2'
hanaHost='127.0.0.1'
mongoHost='127.0.0.1'
mongoPort=27017
processNum=4

while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in
    -pgPort)
      pgPort=$2
      echo 'pgPort'
      shift 2
      ;;
    -mdxPort)
      mdxPort=$2
      echo 'mdxPort'
      shift 2
      ;;
    -pgHost)
      pgHost=$2
      echo 'pgHost'
      shift 2
      ;;
    -appName)
      appName=$2
      shift 2
      ;;
    -webPort)
      webPort=$2
      shift 2
      ;;
    -devPort)
      devPort=$2
      shift 2
      ;;
    -processNum)
      processNum=$2
      shift 2
      ;;
    -mongoPort)
      mongoPort=$2
      shift 2
      ;;
    -mongoHost)
      mongoHost=$2
      shift 2
      ;;
    -hanaHost)
      hanaHost=$2
      shift 2
      ;;
    -custName)
      custName=$2
      shift 2
      ;;
    -installPath)
      installPath=$2
      shift 2
      ;;
    *)
      break
      ;;
  esac
done

echo 'private cloud'
appDir="$installPath/APP"
devDir="$installPath/DEV"

if [[ $pgPort ]];then
  configTemp='confiure-template-pg.json'
else
  configTemp='confiure-template-private.json'  
fi

uploadFolder1="$installPath/UPLOAD"

rm -rf $appDir
mkdir -p $appDir
mkdir -p $uploadFolder1/dev $uploadFolder1/sys
chmod -R a+w $uploadFolder1
cp -f $configTemp $appDir/configure.json
 # dev depart from app
rm -rf $devDir
mkdir -p $devDir
ln -sf $appDir/configure.json $devDir/configure.json

appTarfile=`ls cons*.tar`

if [ $appTarfile ]
then
    echo "local client exist: $appTarfile"
    cp -f $appTarfile $uploadFolder1/sys
else
    echo "client does not exist, get_build"
    exit 1
fi
#replace for sed
uploadFolder=${uploadFolder1//\//\\\/}

echo "appTarfile $appTarfile"

echo 'clean process'
pm2 del dev
pm2 del backend
pm2 del etl_worker
# done

echo "$webPort $devPort"

if [[ $appTarfile ]];then
   
    tar zxf $uploadFolder1/sys/$appTarfile -C $appDir
    if [[ $private = true ]];then
      tar zxf $uploadFolder1/sys/$appTarfile -C $devDir
      echo 'link license dir'
      rm -rf $devDir/license
      ln -s $appDir/license $devDir/license
    fi

    cd $appDir
    echo 'update config'
    sed -i "s/\"postgre_host\":.*/\"postgre_host\": \"$pgHost\",/g" configure.json
    sed -i "s/\"emailCustName\":.*/\"emailCustName\": \"$custName\",/g" configure.json
    sed -i "s/\"host\":.*/\"host\": \"$hanaHost\",/g" configure.json
    sed -i "s/\"mongodb_host\":.*/\"mongodb_host\": \"$mongoHost\",/g" configure.json
    sed -i "s/\"upload_folder\":.*/\"upload_folder\":\"$uploadFolder\",/g" configure.json

    if [[ $appName = 'web' ]];then
       sed -i "s/\"etlPm2Proc\":.*/\"etlPm2Proc\":\"\",/g" configure.json
    elif [[ $appName = 'etl' ]];then
       sed -i "s/\"webPm2Proc\":.*/\"webPm2Proc\":\"\",/g" configure.json
       sed -i "s/\"webHealthCheckCron\":.*/\"webHealthCheckCron\":\"\",/g" configure.json
       sed -i "s/\"schemaBackupCron\":.*/\"schemaBackupCron\":\"\",/g" configure.json
       sed -i "s/\"hanaBackupCron\":.*/\"hanaBackupCron\":\"\",/g" configure.json
    fi
    
    if [[ $appName = '' ]];then    
       PM2_LIST=("dev" "backend" "etl_worker")
    elif [[ $appName = 'web' ]];then    
       PM2_LIST=("dev" "backend")
    else 
       PM2_LIST=("dev" "etl_worker")
    fi

    for pm2proc in ${PM2_LIST[@]}
    do
      echo "process $pm2proc start"

      pm2proc1=$pm2proc

      if [[ $pm2proc = 'dev' ]]; then  
          procDir=$devDir
      else
          procDir=$appDir
      fi

      cd $procDir
     
      if [[ $pm2proc = 'backend' ]]; then  
        NODE_OPTIONS=--max-old-space-size=32056 pm2 start ${procDir}/${pm2proc1}.js -i $processNum --merge-logs 
      else          
          NODE_OPTIONS=--max-old-space-size=32056 DEPLOY_NAME=KEYFUNC_OFFICE pm2 start ${procDir}/${pm2proc1}.js       
      fi
    done

    pm2 save
    pm2 startup      
    echo 'app install done'
    echo "webPort $webPort devPort $devPort"
else
   echo 'build not exist'
   exit 1
fi
