##########!/bin/sh -xv
#
# Copyright (c) 1999-2001 Easysoft Ltd. All rights reserved.
#
# This script takes a file describing the products we would like installed
# and attempts to install them.
#
# Calling format:
#
# install_other_products input_file install_options
#
# returns:
#
# 0 = success
# 1 = invalid command line
# 2 = environment variable not defined
# 3 = description file not found or not readable
# 4 = product distribution not found
# 5 = aborted at user request
#
#
# The input description file should look like this:
#
# product, product-long, required, version
#
# e.g. 
# oob,odbc-odbc-bridge,y,1.0.0.27
#
# If no specific version is required it should be 0.
#
# $Id: install_other_products,v 1.6 2004/04/16 13:47:17 martin Exp $
#
# $Log: install_other_products,v $
# Revision 1.6  2004/04/16 13:47:17  martin
# whitespace change only.
#
# Revision 1.5  2003/11/03 15:16:40  martin
# BZ 100 - default installed product to item [1].
#
# Revision 1.4  2001/05/11 09:24:00  martin
# Bourne shell on AIX 4.1 appears to dislike cd xxx*.
#
#
# Revision 1.3  2001/05/10 14:21:36  martin
# Not searching current dir for extra products due to typo.
#
# Revision 1.2  2001/05/10 07:38:10  martin
# Do not print 1..1 if there is only one choice.
# Fix problem with shell expansion in backticks.
#
# Revision 1.1  2001/05/09 10:23:28  martin
# Initial revision
#
#
#
INSTALLPATH=/usr/local/easysoft
FTPHOST=ftp.easysoft.com
if [ "$ECHO" = "" ];then
  echo "error - ECHO not defined, expect it to be exported"
  exit 2
fi
if [ "$TESTEXISTS" = "" ];then
  echo "error - TESTEXISTS not defined, expect it to be exported"
  exit 2
fi
#
# Check command line
#
cmdline_error=""
if [ $# -ne 2 ];then
  cmdlinestr="incorrect number of parameters"
  cmdline_error="1"
else
  DESCFILE="$1"
  INSTALLOPTIONS="$2"
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "\"$*\""
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "install_other_products input_description_file install_options"
  exit 1
fi
#
# Check the description file
#
if [ ! -r "$DESCFILE" ];then
  $ECHO "Cannot find/read: $DESCFILE"
  exit 3
fi
for productline in `cat $DESCFILE`
do
  #
  # Get the product, required and version fields
  #
  PRODUCT=`$ECHO $productline | awk -F, '{print $1}'`
  PRODUCTLONG=`$ECHO $productline | awk -F, '{print $2}'`
  REQUIRED=`$ECHO $productline | awk -F, '{print $3}'`
  VERSION=`$ECHO $productline | awk -F, '{print $4}'`
  PRODINFOFILE="/usr/local/easysoft/${PRODUCT}_install.info"
  #
  # Look for a tar file of this product
  #
  $ECHO "Looking for $PRODUCTLONG distribution file"
  distribfiles=""
  cwd=`pwd`
  cd ..
  upcwd=`pwd`
  cd $cwd
  for dir in $cwd $upcwd
  do
    $ECHO "Checking directory $dir"
    l=`ls $dir/$PRODUCTLONG*.tar* 2>/dev/null`
    if [ "$l" != "" ];then
      distribfiles="$distribfiles $l"
    fi
  done
  if [ "$distribfiles" != "" ];then
    found=`$ECHO $distribfiles | awk '{print NF}'`
    $ECHO "Found $found potential distribution file(s) for $PRODUCTLONG"
    choice=""
    while [ "$choice" = "" ]
    do
      $ECHO
      $ECHO $distribfiles | awk '{for (i=1; i <= NF; i++) {print i,$i;}}'
      $ECHO
      cat <<EOF
Please choose a distribution file from the above list by entering the
number in the left column.

EOF
      #
      # Special handling for only one distrib found
      #
      if [ $found -eq 1 ];then
        $ECHO "Distribution? ([1]): \c"
      else
        $ECHO "Distribution? (1..$found) [1]: \c"
      fi
      read choice
      #
      # If CR hit autoselect [1]
      #
      if [ "$choice" = ""  ];then
        choice="1"
      fi
      c=`$ECHO $choice | sed 's/[0-9]*//'`
      if [ "$c" != "" ];then
        $ECHO "$choice is not a valid input"
        choice=""
      elif [ \( $choice -lt 1 \) -o \( $choice -gt $found \) ];then
        $ECHO "$choice is not a valid choice"
        choice=""
      fi
      if [ "$choice" != "" ];then
        dfile=`$ECHO $distribfiles | awk "{print \\\$$choice}"`
      fi
    done
    answer="hoho"
    while [ \( "$answer" != "y" \) -a \( "$answer" != "n" \) -a \( "$answer" != "q" \) ]
    do
      $ECHO "Unpack and install ${dfile}? (y,n,q=quit [y]): \c"
      read answer
      if [ "$answer" = "" ];then
        answer="y"
      fi
      if [ "$answer" = "q" ];then
        $ECHO "install_other_products aborted at user request"
        exit 5
      fi
    done
    if [ "$answer" = "y" ]; then
      cat <<EOF
You have asked this script to unpack and install $PRODUCTLONG
from distribution file $dfile. 
EOF
      unpackdir=""
      while [ "$unpackdir" = "" ];
      do
        cwd=`pwd`
        $ECHO "Unpack into which directory ([$cwd]): \c"
        read unpackdir
        if [ "$unpackdir" = "" ]; then
          unpackdir="$cwd"
        fi
        if [ ! $TESTEXISTS $unpackdir ];then
          $ECHO "$unpackdir does not seem to exist"
          unpackdir=""
        fi
      done
      $ECHO
      $ECHO "Unpacking $PRODUCTLONG from $dfile in $unpackdir"
      $ECHO
      $ECHO "Examining distribution for compression method"
      x=`$ECHO $dfile | sed 's/.*\.tar//'`
      if [ "$x" = "" ];then
        compression=""
        $ECHO "No compression"
      else
        x=`$ECHO $dfile | sed 's/.*\.Z//'`
        if [ "$x" = "" ];then
          compression="uncompress -c "
          $ECHO "distribution compressed"
        else
          x=`$ECHO $dfile | sed 's/.*\.gz//'`
          if [ "$x" = "" ];then
            compression="gzip -cd "
            $ECHO "distribution gzipped"
          else
            x=`$ECHO $dfile | sed 's/.*\.bz2//'`
            if [ "$x" = "" ];then
              compression="bzip2 -cd"
	      $ECHO "distribution bzipped"
            fi
          fi
        fi
      fi
      cwd=`pwd`
      cd $unpackdir
      if [ "$compression" != "" ];then
        $compression $dfile | tar xvf -
      else
        tar -xvf $dfile
      fi
      dir=`basename $dfile | sed 's/\(.*\)\.tar/\1/'`
      cd $dir
      ./install $INSTALLOPTIONS
      cd $cwd
    fi
  else
    cat <<EOF
Cannot find any potential distribution files for $PRODUCTLONG.
Please download $PRODUCTLONG from ftp://ftp.easysoft.com and install it.
EOF
    exit 4
  fi
done
