##########!/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 compares them with whatever is already installed. It outputs the
# differences.
#
# Calling format:
#
# install_check_tools input_file output_file
#
# returns:
#
# 0 = success
# 1 = invalid command line
# 2 = environment variable not defined
# 3 = description file not found or not readable
#
#
# 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_check_products,v 1.3 2003/03/14 17:44:03 martin Exp $
#
# $Log: install_check_products,v $
# Revision 1.3  2003/03/14 17:44:03  martin
# Move to 3 part numbering.
#
# Revision 1.2  2001/05/09 10:25:02  martin
# Unwritten edits.
#
# Revision 1.1  2001/05/09 10:22:59  martin
# Initial revision
#
#
#
INSTALLPATH=/usr/local/easysoft
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"
  OUTFILE="$2"
fi
#
# If command line error
#
if [ "$cmdline_error" = "1" ];then
  $ECHO "\"$*\""
  $ECHO "Invalid command line, $cmdlinestr"
  $ECHO "Usage:"
  $ECHO "install_check_products input_description_file output_description_file"
  exit 1
fi
#
# Check the description file
#
if [ "$DESCFILE" != "stdin" ];then
  if [ ! -r "$DESCFILE" ];then
    $ECHO "Cannot find/read: $DESCFILE"
    exit 3
  fi
  $0 stdin $OUTFILE < "$DESCFILE"
  exit $?
fi
eof=0
> $OUTFILE
while [ $eof -eq 0 ]
do
  read LINE
  eof=$?
  if [ $eof -eq 0 ];then
    FOUNDPART=""
    #
    # Get the product, required and version fields
    #
    PRODUCT=`$ECHO $LINE | awk -F, '{print $1}'`
    PRODUCTLONG=`$ECHO $LINE | awk -F, '{print $2}'`
    REQUIRED=`$ECHO $LINE | awk -F, '{print $3}'`
    VERSION=`$ECHO $LINE | awk -F, '{print $4}'`
    PRODINFOFILE="$INSTALLPATH/${PRODUCT}_install.info"
    #$ECHO "Doing $PRODINFOFILE"
    if [ ! $TESTEXISTS "$PRODINFOFILE" ];then
      #
      # Product not found - what was the required field?
      #
      if [ "$REQUIRED" = "y" ];then
        $ECHO "Cannot find $PRODUCTLONG and it is a required component"
        $ECHO "You need to download/install version $VERSION of $PRODUCTLONG"
        $ECHO "$LINE" >> "$OUTFILE"
      elif [ "$REQUIRED" = "n" ];then
        $ECHO "Cannot locate $PRODUCTLONG but it is not required"
      elif [ "$REQUIRED" = "r" ];then
        $ECHO "Cannot locate $PRODUCTLONG and it is recommended"
        if [ "$VERSION" = "0" ];then
          $ECHO "You should consider downloading/installing $PRODUCTLONG"
        else
          $ECHO "You should consider downloading/installing version $VERSION of $PRODUCTLONG"
        fi
      fi
    elif [ "$VERSION" != "0" ]; then
      #
      # Product found and has a required version
      #
      # Extract the version
      #
      ACTVERSION=`grep version "$PRODINFOFILE" | tail -1 | awk '{print $2}'`
      ACTPRODUCT=`grep product "$PRODINFOFILE" | tail -1 | awk '{print $2}'`
      ACTDATE=`grep date "$PRODINFOFILE" | tail -1 | awk '{print $2}'`
      oACT="$ACTVERSION"
      ACTVERSION=`$ECHO $ACTVERSION | sed 's/\/.*\/.*\([0-9]\{1,2\}\)\.\([0-9]\{1,2\}\)\.\([0-9]\{1,2\}\)\.\([0-9]\{1,2\}\).*/\1\.\2\.\3\.\4/'`
      if [ "$ACTVERSION" = "$oACT" ];then
 	ACTVERSION=`$ECHO $ACTVERSION | sed 's/\/.*\/.*\([0-9]\{1,2\}\)\.\([0-9]\{1,2\}\)\.\([0-9]\{1,2\}\).*/\1\.\2\.\3/'`
        FOUNDPART="3"
      else
        FOUNDPART="4"
      fi
      if [ "$ACTVERSION" = "" ];then
	$ECHO "Failed to extract version from $PRODUCTINFOFILE"
	if [ "$FOUNDPART" = "4" ];then
          ACTVERSION="0.0.0.0"
	else
          ACTVERSION="0.0.0"
        fi
        $ECHO "Assuming $PRODUCT at version $ACTVERSION"
      fi
      $ECHO "Found $PRODUCT version $ACTVERSION"
      #
      # Check versions
      #
      #$ECHO "Comparing $ACTVERSION with required $VERSION"
      VERSION1=`$ECHO $VERSION | awk -F. '{print $1}'`
      VERSION2=`$ECHO $VERSION | awk -F. '{print $2}'`
      VERSION3=`$ECHO $VERSION | awk -F. '{print $3}'`

      ACTVERSION1=`$ECHO $ACTVERSION | awk -F. '{print $1}'`
      ACTVERSION2=`$ECHO $ACTVERSION | awk -F. '{print $2}'`
      ACTVERSION3=`$ECHO $ACTVERSION | awk -F. '{print $3}'`

      mismatched=""
      if [ $ACTVERSION1 -lt $VERSION1 ];then
	mismatched="major"
      elif [ $ACTVERSION2 -lt $VERSION2 ];then
        mismatched="minor"
      elif [ $ACTVERSION3 -lt $VERSION3 ];then
        mismatched="build"
      fi
      if [ "$mismatched" != "" ];then
        $ECHO "Installed and required $PRODUCTLONG version mismatch"
	$ECHO "Requires: $VERSION, Installed: $ACTVERSION (mismatched $mismatched)"
        $ECHO $LINE >> $OUTFILE
      fi
    fi
  fi
done
