#!/bin/sh
#set -x
# Try to find $exe_file and use the absolute directory
# name to locate the packages.
#
# Algorithm:
#
#   1. If clustadd was invoked with a leading / , assume
#   the given path is a full path name.
# 
#   2. If clustadd is invoked starting with ./, use that
#   directory name in addition to the current working directory
#   to calculate the full path name.
#
#   3. Otherwise, search the path and use the first clustadd
#   encountered.
#
#   4. If none of the above work, exit gracefully.
#
# Result should be EXEC_DIR containing the full path name of the directory
# which holds the clustadd script.

PWD=`pwd`
exe_file=clustadd_exe
tmpfile=/tmp/`basename $0`"-tmp."$$
# Save the command line input option and arguments
echo " $* " > /tmp/.cmd.$$
cmd_option=`cat /tmp/.cmd.$$ | awk -F: '{ for ( i = 1; i <= NF; i++) print $i }'`
FIRST=`echo $0 | awk -F/ '{ print $1 }' `

if [ -z "$FIRST" ]; then
  # Either Leading slash or nothing specified
  if [ -z "$0" ]; then
    echo "Error: No command specified. Exiting..."
    exit 1
  else
    # Absolute path assumed
    EXEC_DIR=`dirname $0`
  fi
elif [ "$FIRST" = "." ]; then
  # ./<pathname or executable>
  EXEC_DIR=$PWD/`dirname $0`
  if [ ! -d $EXEC_DIR ]; then
    echo "Error: Cannot find executable directory. Exiting..."
    exit 1
  fi
else
  # <name>/<path> or <name>
  echo $0 | grep / > /dev/null 2>&1
  
  if [ $? -eq 0 ]; then
    # There is a slash in the pathname, assume it is relative
    EXEC_DIR=$PWD/`dirname $0`
    
  else
    # No slash in the pathname, find it in the path
    PATH_DIRS=`echo $PATH | awk -F: '{ for ( i = 1; i <= NF; i++) print $i }'`

    if [ -z "$PATH_DIRS" ]; then
      echo "Error: Cannot determine location of executable from $PATH, Exiting..."
      exit 1
    fi
      
    for d in $PATH_DIRS; do
      if [ -f $d/$0 ]; then
        EXEC_DIR=$d
        continue
      fi
    done
  fi
fi

if [ "$EXEC_DIR" = "." ]; then
  EXEC_DIR=$PWD
fi

EXEC_DIR=`echo $EXEC_DIR | sed s:'/bin$'::`

cd $EXEC_DIR

if [ ! -r bin/$exe_file ]; then
  echo "Error: cannot find the executable file $EXEC_DIR/bin/$exe_file, Exiting."
  exit 1
fi

echo "Checking for previously installed packages..."

# First, setup dummy response files for all packages.

RESP_DIR=/tmp/SSWS_response_files.$$

if [ -d $RESP_DIR ]; then
  rm -rf {RESP_DIR
fi

mkdir $RESP_DIR

cd packages

#for d in SPRO* SUNW* SPJA*;
# do
#  touch $RESP_DIR/$d
#done
find * -type f -name "pkginfo" -exec dirname  {} \; > /usr/tmp/$pk2.$$
for d in `cat /usr/tmp/$pk2.$$`
do
  touch ${RESP_DIR}/$d
done
rm -f /usr/tmp/$pk2.$$

cd $EXEC_DIR
# Now setup response files for special packages.

#  PJ fix/workaround  for AB2  book
# Install to non-default  location
NEW_DIR=""
    while getopts nxa:d:b:r:h i 
        do
           case $i in
           b)    
             NEW_DIR=$OPTARG;
             ;; 
           n|x|h|a|d|r) ;;
           esac
        done
 #shift `expr $OPTIND - 1`

SPECIAL_PKGS="SPROvisu SPROesrt SPROabcc SPROabcpl SPROabftn SPROabins SPROabov SPROabtw SPROabws"

for p in $SPECIAL_PKGS; do
  if [ -d $EXEC_DIR/packages/$p ]; then
    rm $RESP_DIR/$p
      pkgask -r ${RESP_DIR}/$p -d ${EXEC_DIR}/packages $p >> /dev/null 2>&1
      if [ "$NEW_DIR" != "" ];
      then
        cat ${RESP_DIR}/$p | sed -e "s;COLL_PARENTDIR=/opt;COLL_PARENTDIR=${NEW_DIR};" -e "s;AB2INSTALLDIR=/opt;AB2INSTALLDIR=${NEW_DIR};" -e "s;AB2DATADIR=/opt;AB2DATADIR=${NEW_DIR};" > ${RESP_DIR}/$p.new
          mv ${RESP_DIR}/$p.new ${RESP_DIR}/$p
      fi
  fi
done
# Second pass creates a pkgname.# file for each potential
# package to be created by pkgadd.  For each package installed
# if there is already a response file, create a new one with the
# suffix incremented.

pkginfo | awk '{ print $2 }' >$tmpfile
  
cd $EXEC_DIR/packages

for d in */pkginfo; do
  PREFIX=`dirname $d`
  pkg_vers=`cat $tmpfile | grep $PREFIX`

  for p in $pkg_vers; do
    CURR_SFX=`echo $p | awk -F. '{ print $2 }'`
	
    if [ -n "$CURR_SFX" ]; then
      MAX_SFX=`expr $CURR_SFX + 1`
    else
      # The suffix used by pkgadd starts with "2", not "1"
      MAX_SFX=2
    fi

    CURR_SFX=2

    while
      :
    do
      if [ "$PREFIX" = "SPROvisu" ]; then
        cp $RESP_DIR/SPROvisu $RESP_DIR/SPROvisu.$CURR_SFX
      elif [ "$PREFIX" = "SPROesrt" ]; then
        cp $RESP_DIR/SPROesrt $RESP_DIR/SPROesrt.$CURR_SFX
      elif [ "$PREFIX" = "SPROabcc" ]; then
        cp $RESP_DIR/SPROabcc $RESP_DIR/SPROabcc.$CURR_SFX
      elif [ "$PREFIX" = "SPROabcpl" ]; then
        cp $RESP_DIR/SPROabcpl $RESP_DIR/SPROabcpl.$CURR_SFX
      elif [ "$PREFIX" = "SPROabftn" ]; then
        cp $RESP_DIR/SPROabftn $RESP_DIR/SPROabftn.$CURR_SFX
      elif [ "$PREFIX" = "SPROabins" ]; then
        cp $RESP_DIR/SPROabins $RESP_DIR/SPROabins.$CURR_SFX
      elif [ "$PREFIX" = "SPROabov" ]; then
        cp $RESP_DIR/SPROabov $RESP_DIR/SPROabov.$CURR_SFX
      elif [ "$PREFIX" = "SPROabtw" ]; then
        cp $RESP_DIR/SPROabtw $RESP_DIR/SPROabtw.$CURR_SFX
      elif [ "$PREFIX" = "SPROabws" ]; then
        cp $RESP_DIR/SPROabws $RESP_DIR/SPROabws.$CURR_SFX
      else 
        touch $RESP_DIR/$PREFIX.$CURR_SFX
      fi
      if [ $CURR_SFX = $MAX_SFX ]; then
        break
      else
        CURR_SFX=`expr $CURR_SFX + 1`
      fi
    done
  done
done

cd $PWD

# Now invoke clustadd.

echo "Invoking $exe_file..."

bin/$exe_file -r $RESP_DIR  ${cmd_option}
status=$?

# Clean up.
rm -rf $RESP_DIR $tmpfile /tmp/.cmd.$$
exit $status
