##########!/bin/sh -xv
#
# Copyright (c) 1999-2005 Easysoft Ltd. All rights reserved.
#
# $Id: install_check_products 32 2005-03-02 17:59:57Z martin $
#
# 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
#
# the first argument must be the name of the directory containing the
# "other" product and is also the stem of the _install.info file.
# the second argument is used for display purposes only
# the third argument is whether this product is required (y|n)
# the fourth argument is the minimum version of the "other" product.
#
# If no specific version is required it should be 0.
#
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=""
    #
    # The following algorithm has changed due to bz 1264.
    # Look of a PRODUCT dir, if found the product is installed and may
    # have the xxx_install.info file in the PRODUCT dir. If it doesn't
    # we have to fall back to looking at the easysoft/xxx_install.info.
    #
    if [ $TESTEXISTS "${INSTALLPATH}/${PRODUCT}" ]; then
      if [ $TESTEXISTS "$INSTALLPATH/${PRODUCT}/${PRODUCT}_install.info" ]; then
	PRODINFOFILE="$INSTALLPATH/${PRODUCT}/${PRODUCT}_install.info"
      elif [ $TESTEXISTS "${INSTALLPATH}/$PRODUCT}_install.info" ]; then
	PRODINFOFILE="$INSTALLPATH/${PRODUCT}_install.info"
      fi
    fi
    if [ "$PRODINFOFILE" != "" ];then
      $ECHO "Found $PRODINFOFILE"
    fi
    if [ "$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
