#!/bin/ksh
###################################################################
#             (C) COPYRIGHT Candle Corporation 1999               #
#                      All Rights Reserved                        #
#                                                                 #
#       Licensed Materials - Property of Candle Corporation       #
#         Duplication or disclosure prohibited without the        #
#        expressed written consent of the Candle Corporation.     #
#                                                                 #
###################################################################

# @(#) untarpkg - unloads a .ctp or .tar file.  This script is generally
# @(#) called from a Java class.

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 () 
{
  print -u2 "$PROGNAME [ -h CANDLEHOME ] [ -d pkgloc ] archive_name"
  exit 2
}


###################################################################
#                          Main                                   #
###################################################################
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
PROGNAME=$(basename $0)

while getopts :h:d:c: opt
do case $opt in
  h) CANDLEHOME=$OPTARG
     CANDLEHOME=$(eval print $CANDLEHOME)  # Process ~ if it was entered.
     ;;
  d) PKGLOC=$OPTARG
     PKGLOC=$(eval print $PKGLOC) 
     ;;
  ?) Use_report
     ;;
  :) Use_report
     ;;
  esac
done
shift OPTIND-1
[ $# -eq 1 ] || Use_report
archive=$1

#$NLS-E$ "CANDLEHOME directory not set or not specified"
[ -n "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0680E' CANDLEHOME`"
#$NLS-E$ "\"$CANDLEHOME\" is not a directory"
[ -d "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0681E' \\"$CANDLEHOME\\"`"
#$NLS-E$ "cannot write to CANDLEHOME directory \"$CANDLEHOME\""
[ -w "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0950E' CANDLEHOME \\"$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

cd $CANDLEHOME > /dev/null 2>&1
#$NLS-E$ "could not change to directory \"$CANDLEHOME\""
[ $? -eq 0 ] ||  Err_report fail "`nls_replace 'KCI0951E' \\"$CANDLEHOME\\"`"
export CANDLEHOME

PKGLOC=${PKGLOC:-$(pwd)}  # e.g. /cdrom/unix
pkg=$PKGLOC/$archive
#$NLS-E$ "cannot read package \"$pkg\""
[ -r $pkg ] || Err_report fail "`nls_replace 'KCI0952E' \\"$pkg\\"`"
ext=${archive#*.}  # The extension must be either ctp or tar.

if [ "$ext" = "tar" ]  # Uncompressed tar file.
then
  #$NLS-E$ "unloading package \"$pkg\" to \"$CANDLEHOME\""
  Err_report info "`nls_replace 'KCI0953I' \\"$pkg\\" \\"$CANDLEHOME\\"`"
  tar -xf $pkg > /dev/null 2>&1
  #$NLS-E$ "could not unload package \"$pkg\""
  [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0954E' \\"$pkg\\"`"
else  # Compressed tar file - either .ctp or .zxx.
  #$NLS-E$ "unloading package \"$pkg\" to \"$CANDLEHOME\""
  Err_report info "`nls_replace 'KCI0953I' \\"$pkg\\" \\"$CANDLEHOME\\"`"
  zcat < $pkg | tar -xf - > /dev/null 2>&1
  #$NLS-E$ "could not unload package \"$pkg\""
  [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0954E' \\"$pkg\\"`"
fi

exit 0
