#!/bin/bash
# relocdb - Relocate databases for a  particular trisul context 
#           <required> -t Destination
#           <optional> -c Context
#           	default context is CONTEXT0(Primary) 
#           	default Source is  /usr/local/var
# (c) 2015, Unleash Networks
####################################################################################################
Usage() { 
	echo "Usage: relocdb  [-c contextname]  -t relocate_to_directory  " 1>&2
	echo "Opts :                                   " 1>&2
	echo "       -c   contextname (default primary )   " 1>&2
	echo "       -t   directory to relocate the context to " 1>&2
	exit 1
}
Error() {
  echo -e "\e[91mError : $1 \e[0m"
  exit 1
}
  
if [ $# -lt 2 ]; then
  Usage
fi

while getopts ":c:t:" o; do
	case "${o}" in

		c)
			ctxname=${OPTARG}
			;;

		t)
			relocto=${OPTARG}
			;;

		*)
			usage
			;;
	esac
done
shift $((OPTIND-1))

# usage , required params 
if [ -z "$relocto" ]; then Usage; fi 

# remove any trailing slashes from input 
relocto=`echo $relocto | sed -e "s/\/*$//"`

# Check the relocation input
if ! [ -d $(dirname $relocto) ]; then
  Error "Base directory $(dirname $relocto) does not exist "
elif [ -d $relocto ]; then
  Error  "Relocation directory already exists : $relocto "
fi


# check the context
if [ -z "$ctxname" ]; then
  trisul_env="env.eff"
  trisul_cfg="trisulConfig.xml"
  wt_ctx="root_ctx0"
  ctxname="CONTEXT0"
elif ! [ -f env.eff.$ctxname ];then
  Error "context $ctxname not found."
else
  trisul_env="env.eff.$ctxname"
  trisul_cfg="trisulConfig_$ctxname.xml"
  wt_ctx="root__$ctxname"
fi


source $trisul_env
MOVEFILES=true

# check if trisul is running 
if test -f $SQPIDFILE; then
  PID=$(cat $SQPIDFILE)
  PS=$(ps $PID)
  if [[ $PS =~ $trisul_cfg  ]]; then
    Error "Error : Trisul is running with PID $PID. Please stop trisul first before relocating"
    exit 1
   fi
fi


# check context directory exists?
if  ! test -d $SQRINGDIR; then
  echo  "   Context $ctxname  not found in $SQRINGDIR"
  echo  "   Directory doesnt exist, volume probably removed"
  echo  -n "   Do you want to adjust the config files alone ? Enter (y/n) : "
  read chce
  if test $chce ==  "y"; then
    MOVEFILES=false
  else 
    echo "  Bye ! No changes made."
    exit 1
  fi
fi

# create layout in new location
echo "Creating layout in new location $relocto"
mkdir -p $relocto
mkdir -p $relocto/{lib,log,run}/trisul

#relocate the context:
if $MOVEFILES; then 
	echo "Moving files from  context $ctxname from $SQRINGDIR to $relocto "
	if  test -d $SQRINGDIR; then
	  mv $SQRINGDIR $relocto/lib/trisul
	fi

	if [ $trisul_env == "env.eff" ] ;then
	    mv $SQLOGDIR/*.log  $relocto/log/trisul
	elif test -d $SQLOGDIR; then
	    mv $SQLOGDIR  $relocto/log/trisul
	fi

	if test -f $SQPIDFILE; then
	    mv $SQPIDFILE $relocto/run
	fi
fi 


# make changes in environment file for the context
relocfrom=$(dirname $(dirname $(dirname $SQRINGDIR)))  
echo "Rewriting $trisul_env moving from  $relocfrom to $relocto "
sed "s@$relocfrom@$relocto@g" $trisul_env > "$trisul_env.new"
mv "$trisul_env.new" "$trisul_env"

# make changes in trisulConfig(_xyz).xml file for the context
echo "Rewriting $SQCFGDIR/$trisul_cfg File"
sed "s@$relocfrom@$relocto@g" $SQCFGDIR/$trisul_cfg > "$SQCFGDIR/$trisul_cfg.new"
mv "$SQCFGDIR/$trisul_cfg.new" "$SQCFGDIR/$trisul_cfg"

# change soft link in webtrisul db if exists
pushd $SQDATADIR/../webtrisul/db 1> /dev/null 
if  test -h $wt_ctx ; then 
  echo "Relocating webtrisul context"
  unlink $wt_ctx
  if test -z $SQCONTEXT; then
    SQCONTEXT=0
  fi
  ln -s  $relocto/lib/trisul/CONTEXT$SQCONTEXT  $wt_ctx
fi
popd 1>/dev/null

if [ $trisul_env == "env.eff" ] ;then
	echo "Converting CONTEXT0 into a stub for RPM/DEB install"
	mkdir -p $SQRINGDIR/config
	echo "STUB FOR RPM/DEB" > $SQRINGDIR/config/TRISULCONFIG.SQDB
fi

echo "Finished  relocating context $ctxname FROM $relocfrom TO $relocto"
echo #
echo # Please restart Web Trisul and login again (sudo service webtrisuld restart) 
echo # 
exit 0
