#!/bin/bash
dateStr=`date '+%F_%H_%M_%S'`
echo "start: $dateStr"

if [ $# -lt 1 ];
then
  echo "USAGE: $0 -src www.epmsquare.cn:40003:yatal:user1 -tgt www.epmsquare.cn:40005:yatalleon:user2"
  exit 1
fi

#srcAddr='www.epmsquare.cn:40003:yatal'
cmdDir='/usr/bin'
while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in    
    -skipDump)
      skipDump=true
      shift
      ;;
    -src)
      srcAddr=$2
      shift 2
      ;;
    -tgt)
      tgtAddr=$2
      shift 2
      ;;
    -cmdDir)
      cmdDir=$2
      shift 2
      ;;
    -epm)
      epm=true
      shift
      ;;
    -schema)
      schema=$2
      shift 2
      ;;
    *)
      break
      ;;
  esac
done

echo "input: $0 $srcAddr $tgtAddr $cmdDir"

#src
pgAddrRegx="(\S+):(\S+):(\S+):(\S+)"
if [[ $srcAddr =~ $pgAddrRegx ]]; then
    testIp=${BASH_REMATCH[1]}
    testPort=${BASH_REMATCH[2]}
    testDb=${BASH_REMATCH[3]}
    testUser=${BASH_REMATCH[4]}
else
    echo "invalid test addr: $srcAddr"
fi

#tgt
if [[ $tgtAddr =~ $pgAddrRegx ]]; then
    devIp=${BASH_REMATCH[1]}
    devPort=${BASH_REMATCH[2]}
    devDb=${BASH_REMATCH[3]}
    devUser=${BASH_REMATCH[4]}
else
    echo "invalid dev addr: $tgtAddr"
fi

if [[ $epm ]]; then
  schema='public'
fi

#1: backup
#/usr/pgsql-14/bin/pg_dump -h hanatest1.epmtest.cn -p 5432 -U postgres -n public yatal -Fc -f yatal.c
if [[ $schema ]]; then
  opt="-h $testIp -p $testPort -U $testUser -n $schema $testDb -Fc -f $testDb.c"
else
  opt="-h $testIp -p $testPort -U $testUser $testDb -Fc -f $testDb.c"
fi
#output sql opt="-h $testIp -p $testPort -U postgres -n public $testDb  -f $testDb.sql"
if [[ $skipDump ]]; then
 echo 'skipDump'
else
  echo "pg_dump $opt"
  $cmdDir/pg_dump $opt
  echo "pg_dump done"
fi

#2 create db on target
opt="-h $devIp -p $devPort -U $devUser -f $devDb"
echo "dropdb $opt"
#/usr/pgsql-14/bin/dropdb  -h hanatest1.epmtest.cn -p 5432 -U postgres yatal2
$cmdDir/dropdb $opt
echo "dropdb done"

#/usr/pgsql-14/bin/createdb  -h hanatest1.epmtest.cn -p 5432 -U postgres yatal2
opt="-h $devIp -p $devPort -U $devUser $devDb"
echo "createdb $opt"
$cmdDir/createdb $opt
echo "createdb done"

#3 create ltree
if [[ $epm ]]; then
  #psql -h 127.0.0.1 -p 5433 -U postgres -d yatalleon -c 'CREATE EXTENSION IF NOT EXISTS ltree;'
  #ltreeSql='CREATE EXTENSION IF NOT EXISTS ltree;'
  opt="-h $devIp -p $devPort -U $devUser -d $devDb"
  echo "psql $opt -c 'CREATE EXTENSION IF NOT EXISTS ltree;'"
  $cmdDir/psql $opt -c 'CREATE EXTENSION IF NOT EXISTS ltree;'
  echo "psql CREATE EXTENSION done"
fi

#4 restore
# /usr/pgsql-14/bin/pg_restore  -h hanatest1.epmtest.cn -p 5432 -U postgres -d yatal2 yatal.c
opt="-h $devIp -p $devPort -U $devUser -d $devDb $testDb.c"
echo "pg_restore $opt"
$cmdDir/pg_restore $opt
echo "pg_restore done"

# update cubedefinition
if [[ $epm ]]; then
  devDbUpper=${devDb^^}
  testDbUpper=${testDb^^}
  updateSql="update cubedefinition set \"catalog\" = '$devDbUpper';UPDATE cubedefinition SET objdef = REPLACE(objdef, '\"catalog\": \"$testDbUpper\"', '\"catalog\": \"$devDbUpper\"');UPDATE cubedefinition SET objdef = REPLACE(objdef, '\"catalog\": \"$testDb\"', '\"catalog\": \"$devDbUpper\"');UPDATE cubedefinition SET objdef = REPLACE(objdef, '\"catalog\":\"$testDbUpper\"', '\"catalog\": \"$devDbUpper\"');UPDATE cubedefinition SET objdef = REPLACE(objdef, '\"catalog\":\"$testDb\"', '\"catalog\": \"$devDbUpper\"')"
  opt="-h $devIp -p $devPort -U $devUser -d $devDb"
  echo "psql $opt -c $updateSql"
  $cmdDir/psql $opt -c "$updateSql"
  echo "psql update cubedefinition done"
fi

dateStr=`date '+%F_%H_%M_%S'`
echo "end: $dateStr"