#!/bin/sh
#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
# $Id: install_other_products 32 2005-03-02 17:59:57Z martin $
#
# 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.
#
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}'`
  VMAJOR=`$ECHO $VERSION | awk -F. '{print $1}'`
  VMINOR=`$ECHO $VERSION | awk -F. '{print $2}'`
  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
  #$ECHO "$VERSION $VMAJOR.$VMINOR"
  for dir in $cwd $upcwd
  do
    majorsearch=""
    minorsearch=""
    for x in 9 8 7 6 5 4 3 2 1
    do
      if [ "$x" -gt "$VMAJOR" ]; then
        majorsearch="$x$majorsearch"
      fi
      if [ "$x" -ge "$VMINOR" ]; then
        minorsearch="$x$minorsearch"
      fi
    done 
    #$ECHO "$majorsearch, $minorsearch"
    $ECHO "Checking directory $dir"
    l=`ls $dir/$PRODUCTLONG*[$majorsearch].[0-9].[0-9]*.tar* 2>/dev/null`
    if [ "$l" != "" ];then
      distribfiles="$distribfiles $l"
    fi
    l=`ls $dir/$PRODUCTLONG*$VMAJOR.[$minorsearch].[0-9]*.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
