#!/bin/bash
if [ $# -lt 1 ];
then
  echo "USAGE: $0 -hanaDocker -hanaDockerTgt -migDirEcs /data_disk/hana_data/SCHEMA_MIG -remoteHost 115.159.64.183 -remoteDir /data_disk/SCHEMA_MIG -skipExport"
  exit 1
fi
echo "inpurt params:$*"
#nodejs
remoteHana='www.epmsquare.cn:30915'
remoteSysPass='Wang_2017'
remoteUserPass='EPM_user_2017'
remoteHost='www.epmsquare.cn'
remoteSSHport='3052'
remoteDir='/home/epm/SCHEMA_MIG'
migDirTgt=''
hdbsqlBin='/usr/sap/hdbclient'
while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in
    -skipMX)
        skipMX=true
        shift
        ;;
    -skipExport)
        skipExport=true
        shift
        ;;
    -skipScp)
        skipScp=true
        shift
        ;;
    -hdbsqlBin)
        hdbsqlBin=$2
        shift 2
        ;;
    -hanaDocker)
      hanaDocker=true
      shift
      ;;
    -hanaDockerTgt)
      hanaDockerTgt=true
      shift
      ;;
    -replaceTarget)
      replaceTarget=true
      shift
      ;;
    -schemaNameTgt)
      schemaNameTgt=$2
      shift 2
      ;;
    -schemaNameSrc)
      schemaNameSrc=$2
      shift 2
      ;;
    -localHana)
      localHana=$2
      shift 2
      ;;
    -localSysPass)
      localSysPass=$2
      shift 2
      ;;
    -localUserPass)
      localUserPass=$2
      shift 2
      ;;
    -remoteHana)
      remoteHana=$2
      shift 2
      ;;
    -remoteSysPass)
      remoteSysPass=$2
      shift 2
      ;;
    -remoteUserPass)
      remoteUserPass=$2
      shift 2
      ;;
    -migDirEcs)
      migDirEcs=$2
      shift 2
      ;;
    -remoteHost)
      remoteHost=$2
      shift 2
      ;;   
    -remoteSSHport)
      remoteSSHport=$2
      shift 2
      ;; 
    -remoteSSHkey)
      remoteSSHkey=$2
      shift 2
      ;;   
    -remoteDir)
      remoteDir=$2
      shift 2
      ;;
    *)
      break
      ;;
  esac
done

echo "remoteHana $remoteHana remoteSysPass $remoteSysPass remoteUserPass $remoteUserPass remoteHost $remoteHost remoteSSHport $remoteSSHport remoteDir $remoteDir hdbsqlBin $hdbsqlBin skipScp $skipScp hanaDocker $hanaDocker replaceTarget $replaceTarget schemaNameTgt $schemaNameTgt schemaNameSrc $schemaNameSrc localHana $localHana localSysPass $localSysPass  localUserPass $localUserPass migDirEcs $migDirEcs"

if [[ $schemaNameTgt ]]; then
   echo "input schemaNameTgt $schemaNameTgt"
else
   schemaNameTgt=$schemaNameSrc
fi

#schema=$schemaNameSrc
if [[ $hanaDocker ]]; then
   migDir='/hana/mounts/SCHEMA_MIG'
else
   migDir=$migDirEcs
fi
if [[ $hanaDockerTgt ]]; then
   migDirTgt='/hana/mounts/SCHEMA_MIG'
else
   migDirTgt=$remoteDir
fi

if [[ $skipExport ]]; then
  echo "skip export"
else
  echo "exporting $schemaNameSrc,$hdbsqlBin/hdbsql -u $schemaNameSrc -p $localUserPass -n $localHana \"EXPORT $schemaNameSrc.\"*\" AS BINARY INTO '$migDir/$schemaNameSrc' WITH REPLACE\""
  if [[ $skipMX ]]; then
     echo "skip m table"
    $hdbsqlBin/hdbsql -u $schemaNameSrc -p $localUserPass -n $localHana "EXPORT ALL HAVING schema_name = '$schemaNameSrc' AND object_type = 'TABLE' AND object_name not like 'M_DEFAULT_%' AS BINARY INTO '$migDir/$schemaNameSrc' WITH REPLACE"
  else
     echo "full copy"
    $hdbsqlBin/hdbsql -u $schemaNameSrc -p $localUserPass -n $localHana "EXPORT $schemaNameSrc.\"*\" AS BINARY INTO '$migDir/$schemaNameSrc' WITH REPLACE"
  fi
  echo "exporting success"
fi

if [[ $skipScp ]]; then
  echo "skip scp to remote"
  exit 0
fi
echo "scp to remote"
rm -rf $migDirEcs/$schemaNameSrc/export/$schemaNameSrc/TC
rm -rf $migDirEcs/$schemaNameSrc/export/$schemaNameSrc/'$D'
rm -rf $migDirEcs/$schemaNameSrc/index/$schemaNameSrc/T1
rm -rf $migDirEcs/$schemaNameSrc/index/$schemaNameSrc/\"$\"M
rm -rf $migDirEcs/$schemaNameSrc/index/$schemaNameSrc/'$M'
cd $migDirEcs
tar zcf  $schemaNameSrc.tar $schemaNameSrc
echo "scp -i $remoteSSHkey -P $remoteSSHport $migDirEcs/$schemaNameSrc.tar epm@$remoteHost:$remoteDir"
#su - epm
scp -i $remoteSSHkey -P $remoteSSHport $migDirEcs/$schemaNameSrc.tar epm@$remoteHost:$remoteDir
if [ $? != "0" ]; then
    echo "scp fails"
    exit 1
else
    echo "scp success"
fi

echo "ssh  -i $remoteSSHkey -p $remoteSSHport epm@$remoteHost tar zxf $remoteDir/$schemaNameSrc.tar -C $remoteDir"
ssh -i $remoteSSHkey -p $remoteSSHport epm@$remoteHost tar zxf $remoteDir/$schemaNameSrc.tar -C $remoteDir
ssh -i $remoteSSHkey -p $remoteSSHport epm@$remoteHost chmod -R 777 $remoteDir/$schemaNameSrc
if [ $? != "0" ]; then
    echo "untar fails"
    exit 1
else
    echo "untar success"
fi

#REPLACE USER
if [[ $replaceTarget ]]; then
    echo "drop user/schema if exist,$schemaNameTgt"
    echo "hdbsql -u SYSTEM -p $remoteSysPass -n $remoteHana drop user $schemaNameTgt cascade"
    $hdbsqlBin/hdbsql -u SYSTEM -p $remoteSysPass -n $remoteHana drop user $schemaNameTgt cascade
fi
echo "CREATE USER"
$hdbsqlBin/hdbsql -u SYSTEM -p $remoteSysPass -n $remoteHana CREATE USER $schemaNameTgt PASSWORD $remoteUserPass NO FORCE_FIRST_PASSWORD_CHANGE
$hdbsqlBin/hdbsql -u SYSTEM -p $remoteSysPass -n $remoteHana ALTER USER $schemaNameTgt DISABLE PASSWORD LIFETIME
echo "GRANT IMPORT/export"
$hdbsqlBin/hdbsql -u SYSTEM -p $remoteSysPass -n $remoteHana GRANT IMPORT TO $schemaNameTgt
$hdbsqlBin/hdbsql -u SYSTEM -p $remoteSysPass -n $remoteHana GRANT EXPORT TO $schemaNameTgt

echo "import from remote"
if [[ $schemaNameSrc = $schemaNameTgt ]]; then
  echo "target and source have same target"
  echo "$hdbsqlBin/hdbsql -u $schemaNameTgt -p $remoteUserPass -n $remoteHana \"IMPORT $schemaNameSrc.\"*\" AS BINARY FROM '/hana/mounts/SCHEMA_MIG/$schemaNameSrc' WITH REPLACE\""
  $hdbsqlBin/hdbsql -u $schemaNameTgt -p $remoteUserPass -n $remoteHana "IMPORT $schemaNameSrc.\"*\" AS BINARY FROM '$migDirTgt/$schemaNameSrc' WITH REPLACE"
else
  #rename schema "EPM" to "HXCOPY";
  echo "target and source have different target"
  $hdbsqlBin/hdbsql -u $schemaNameTgt -p $remoteUserPass -n $remoteHana "IMPORT $schemaNameSrc.\"*\" AS BINARY FROM '$migDirTgt/$schemaNameSrc' WITH REPLACE rename schema $schemaNameSrc to $schemaNameTgt"
fi
echo "import done"