#!/bin/ksh
#
# @01 06/30/03 LCL Initial
#
# This script invokes the Unix Candle kcfcrstr DB restore program

PROGNAME=$(basename $0)

Err_report() {
   case $1 in
      #$NLS-E$ "$PROGNAME warning: $2,continuing ..."
      warn) print -u2 "$PROGNAME `nls_replace 'KCI0535I'`: $2,`nls_replace 'KCI0536I'` ..."
            sleep 2
            return
            ;;
	  #$NLS-E$ "$PROGNAME failure: $2."		
      fail) print -u2 "$PROGNAME `nls_replace 'KCI0537I'`: $2."
            exit 2
            ;;
      info) print -u2 "$PROGNAME        : $2."
            return
            ;;
   esac
}

Use_report () {
    #$NLS-E$ "$PROGNAME restores the MQ Series configuration database from a backup file."
    print -u2 "`nls_replace 'KCI0895I' $PROGNAME`"
    print -u2 "$PROGNAME [-h CANDLEHOME] [-d] -i InputBackupFile -t CMS-Name"
	#$NLS-E$ "\t-d Allows debugging by using the RAS1 settings for the CMS."
    print -u2 "\t`nls_replace 'KCI0896I' -d`"
	#$NLS-E$ "\tInputBackupFile is the full path and name of the file that contains the backup file."
    print -u2 "\t`nls_replace 'KCI0897I' InputBackupFile`"
	#$NLS-E$ "\tCMS-Name is the name of the CMS."
    print -u2 "\t`nls_replace 'KCI0898I' CMS-Name`"
    exit 2
}

###################################################################
#                         Main Routine                            #
###################################################################
bindir=`pwd`/$(dirname $0)
[ "$(print $0 | grep '\./')" = "" ] && [ "$(print $0 | grep '^/.*')" = "" ] && [ `pwd` = '/' ] && bindir=`pwd`$(dirname $0)
[ "$(print $0 | grep '^/.*')" != "" ] && bindir=$(dirname $0)

export TEXTDOMAINDIR=$bindir/../config/nls/msg
export PATH=$bindir:$PATH

export PATH=/usr/bin:$PATH
UsrCmd="$0 $*"
HOPTION=""
debug="n"

while getopts ":dh:i:t:" OPTS
do case $OPTS in
     h) CANDLEHOME=$OPTARG
        HOPTION="ON"
        ;;
     i) backup=$OPTARG
        ;;
     t) cms=$OPTARG
        ;;
     d) debug="y"
        ;;
     ?) Use_report
        ;;
   esac
done

[ -z "$backup" ] && Use_report
[ -z "$cms" ] && Use_report

#$NLS-E$ "Backup file-> $backup"
Err_report info "`nls_replace 'KCI0899I' $backup`"
Err_report info "CMS-name   -> $cms"

[ -n "$CANDLEHOME" ] || CANDLEHOME=$(cd $(dirname $0) ; dirname $(pwd) )
export CANDLEHOME

# Valid CandleHome?
#$NLS-E$ "Cannot set CANDLEHOME environment variable"
[ ! -d "$CANDLEHOME" ] && Err_report fail "`nls_replace 'KCI0900E' CANDLEHOME`"

if [ $(print "$CANDLEHOME" | wc -w) -ne 1 ]
then
  #$NLS-E$ "Blanks are not permitted in CANDLEHOME"
  Err_report fail "`nls_replace 'KCI0682E' CANDLEHOME`"
fi

# Valid backup file?
#$NLS-E$ "Backup file does not exist."
[ -a "$backup" ] || Err_report fail "`nls_replace 'KCI0901E'`"
#$NLS-E$ "Backup file can't be read."
[ -r "$backup" ] || Err_report fail "`nls_replace 'KCI0902E'`"

CurRegPath=$CANDLEHOME/registry
ArchTbl=$CurRegPath/archdsc.tbl
DynShl=$CANDLEHOME/bin/dynarch.shl

. $DynShl $ArchTbl
setJavaEnv $cArch

# Make sure we have a CMS installed to begin with
#$NLS-E$ "Candle Management Server not installed"
[ -a $CANDLEHOME/config/.ConfigData/kmsenv ] || Err_report fail "`nls_replace 'KCI0903E'`"

# Valid CMS name?
grep "^$cms" $CANDLEHOME/config/.ConfigData/kmsenv > /dev/null 2>&1
#$NLS-E$ "CMS-name was not found in this CandleHome."
[ $? = 0 ] || Err_report fail "`nls_replace 'KCI0904E' CMS-name CandleHome`"

# Make sure CMS isn't running
runInfo=$(grep $cms $CANDLEHOME/config/.ConfigData/RunInfo|grep "^$(hostname)\|ms")
#$NLS-E$ "CMS-name is currently running and must be stopped before using this command."
[ $? = 0 ] && Err_report fail "`nls_replace 'KCI0905E' CMS-name`"

# Now get the binArch for the installed CMS so we can invoke the kcfcrstr binary
setBinLibArchs $cArch ms

# Before we invoke it, we have to setup the env variables so we use the config file for the CMS
cfgFile=$(grep -l $binArch/ms $CANDLEHOME/config/$(hostname)_ms_${cms}.config 2>/dev/null)
#$NLS-E$ "CMS has not been configured for this machine"
[ $? = 0 ] || Err_report fail "`nls_replace 'KCI0906E'`"

# Give them one last chance to back out...
while true
do
  print " "
  #$NLS-E$ "You are about to replace the existing MQSeries configuration data."
  print "`nls_replace 'KCI0907E'`"
  #$NLS-E$ "Are you sure you want to proceed? (y/n): \c"
  print "`nls_replace 'KCI0908I'` [1-`nls_replace 'KCI0197I'`, 2-`nls_replace 'KCI0198I'`]: \c"
  read Response
  case "$Response" in
    #$NLS-E$ "...Restore operation cancelled at user's request."
    n*|N*|2) print "`nls_replace 'KCI0909I'`"
	   exit 0
           ;;
    y*|Y*|1) break
           ;;
  esac
done

# Execute; export all environment variables
shTmp=$CANDLEHOME/tmp/shTmp
rm -f $shTmp
# For debugging, we just process all environment variables normally
if [ "$debug" = "y" ]
then
  cat $cfgFile | grep -v "^#" | grep -v "export"|sed "s/^/export /g" > $shTmp

# For debugging turned off, we skip the KBB_RAS1 setting if we find one.
else
  cat $cfgFile |grep -v "^#" |grep -v "KBB_RAS1="|grep -v "export"|sed "s/^/export /g" > $shTmp
fi
. $shTmp
rm -f $shTmp

$CANDLEHOME/$binArch/ms/bin/kcfcrstr -i $backup -o $CANDLEHOME/tables/$cms/RKCFAPLD 
