#!/bin/ksh
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: checkTcltk,v 1.14 2012/11/05 09:28:58 eallaert Exp $"
#
# who       when        what
# --------  ----------  ----------------------------------------------
# wpirani   31/08/99    Created for OCT99 (New Tcl/Tk baseline)
# eallaert  2007-09-03  updated for ACS 7.0
# eallaert  2011-06-03  updated for ACS 10.0

#************************************************************************
#   NAME
#
#   checkTcltk
#
#   SYNOPSIS
#
#   checkTcltk
# 
#   DESCRIPTION
#
#   Check installed Tcl/Tk by comparing the content of $TCLTK_ROOT
#   with a reference file
#
#   BUGS     
#
#   None, just features!
#
#------------------------------------------------------------------------
#

##########################################################################################
# Function definitions used later in this script.
##########################################################################################
# definition of a function to print the usage statement for this script
printUsageAndExit ()
{
    echo ""
    echo "Usage: $0 [-h] [-m]";
    echo "Options: "
    echo "   -h | --help: print this usage message and exit."
    echo "   -m | --msql: check for the msql stuff as well"
    echo ""
    exit $1;
}

##########################################################################################
# Script logic starts here...                                                            #
##########################################################################################
MY_NAME=`basename $0`

#
# These will contain the command line arguments and/or options
#
HELP=
MSQL=
MSQL_NAME=NO_MSQL

#
# These options can be recognized (longopts comma-separated. Colon means 1 argument is required)
#
LONGOPTS=help,msql
SHORTOPTS=h,m
#
# Run getopt (posixly_correct needed). We run twice:
# First run is simply to check the commandline for correctness.
# Second run is does the real work and sets execution flags for this script,
# as appropriate.
export POSIXLY_CORRECT=1

getopt -n $MY_NAME -a -l $LONGOPTS $SHORTOPTS "$@" > /dev/null
if [ $? -ne 0 ] 
    then 
    printUsageAndExit 1;
fi

set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`

# Some programs behave differently if POSIXLY_CORRECT is set. In particular
# the configure/build of TclX and Itcl may fail ...
unset POSIXLY_CORRECT

#
# Iterate over getopt's output and set variables accordingly
#
while :
  do
  case "$1" in
      -h|--help)       HELP=true ;;
      -m|--msql)       MSQL=true; MSQL_NAME=WITH_MSQL ;;
      --) break ;;
  esac
  shift
done
shift

if [ "$HELP" ] ; 
then
    printUsageAndExit 0
fi

# first, verify that the script was invoked without any command-line arguments 
if [ $# -ne 0 ] ;
then 
    printUsageAndExit 1
fi

if [ "$DEBUG" ]
then
    set -x
fi

#
# print header
#
TITLE="     Checking Tcl/Tk installation  "
BUILD_NAME="Tcltk"
. ./standardPrologue

TCLTK=${INSTALL_ROOT}/PRODUCTS/tcltk

#
# get current operating system
#
build_OS=`uname -s`
build_OSV=`uname -r`

if [ ${build_OS} = "Linux" ]
then
     distro=`cat /etc/redhat-release | awk '{ print $1 $2 }'`
     if [ "X${distro}" = "XRedHat" ] ; then
          build_OSV=${distro}`cat /etc/redhat-release | awk '{ print $3 $7 }'`
     elif [ "X${distro}" = "XScientificLinux" ] ; then
          build_OSV=${distro}`cat /etc/redhat-release | awk '{ print $(NF-1) }'`
     elif [ "X${distro}" = "XFedoraCore" ] ; then 
          build_OSV=${distro}`cat /etc/redhat-release | awk '{ print $4 }'`
     fi   
fi

echo $SEPARATOR
echo "Checking on $build_OS version $build_OSV:"
echo ""

if [     ${build_OS}-${build_OSV} != "SunOS-5.8"                  \
     -a  ${build_OS}-${build_OSV} != "Linux-RedHatEnterprise4"    \
     -a  ${build_OS}-${build_OSV} != "Linux-RedHatEnterprise5.2"  \
     -a  ${build_OS}-${build_OSV} != "Linux-RedHatEnterprise5.3"  \
     -a  ${build_OS}-${build_OSV} != "Linux-RedHatEnterprise6.1"  \
     -a  ${build_OS}-${build_OSV} != "Linux-FedoraCore6"          \
   ]
then
    echo ""
    echo "WARNING: this procedure has not been tested on this system"
    echo ""
fi


#-------------------
# check that ACSDATA is defined as directory (needed by msql)
#
if [ "$MSQL" ]
then 
    echo "ACSDATA . . .\c"
    if [ "$ACSDATA" = "" ]
    then	
        echo "ACSDATA not defined, cannot continue."
        exit 1
    fi

    echo "defined as: $ACSDATA . . .\c"
    if [ ! -d $ACSDATA ]
    then
        echo "but is not a directory, cannot continue."
        exit 1
    fi

    if [ ! -w $ACSDATA ]
    then
        echo "but is not a writable directory, cannot continue."
        exit 1
    fi
    echo " . . . ok"
fi

#
# setup and check target directory
#
if [ "$TCLTK_ROOT" = "" ]
then
    TCLTK_ROOT=/usr/local
fi

PREFIX=$TCLTK_ROOT
export PREFIX
echo "Install directory defined as: $PREFIX . . . \c"

if [ ! -d $PREFIX ]
then
    echo "but this is not a directory, cannot continue."
    exit 1
fi

if [ ! -w $PREFIX ]
then
    echo "but this is not a writable directory, cannot continue."
    exit 1
fi
echo " . . . ok"


# 
# check that all expected files have been created 
#
if [ "$build_OS" = "$CYGWIN_VER" ]; then
  SRC="$TCLTK/$build_OS"
  # Whether of not msql is included is indicated by the first argument 
  # passed to this script.
  reference=${TCLTK}/TCLTK.${build_OS}_$MSQL_NAME
else
  SRC="$TCLTK/$build_OS-$build_OSV"
  # Whether of not msql is included is indicated by the first argument 
  # passed to this script.
  reference=${TCLTK}/TCLTK.${build_OS}-${build_OSV}_$MSQL_NAME
fi

cd $PREFIX
echo ""
echo "Checking that all expected files have been created"
echo "(reference list: $reference) "
cat  $reference | grep -v "###" | xargs -i ${INSTALL_ROOT}/INSTALL/buildCheckFileExist $PREFIX/\{\}
echo ""

#
# test that each shell has the expected
# set of commands. This cannot be done with tkman and tkinspect
# because they are are interactive programs.
#
# Make sure the newly built Tcl/Tk binaries and shared libs are found first.
PATH=${TCLTK_ROOT}/bin:$PATH
LD_LIBRARY_PATH=${TCLTK_ROOT}/lib:$LD_LIBRARY_PATH

echo $SEPARATOR
echo "verify:"
rm -f test.tcl
for shell in bltwish expect expectk tcl tclsh wish wishx
do
    echo "    ${shell} . . .\c"
    out=${SRC}/${shell}.out
    ref=${TCLTK}/ref/${shell}.ref
    if [ -x $PREFIX/bin/$shell ] 
    then
	echo "puts [lsort [info commands]]; exit"  | $PREFIX/bin/$shell > $out
        diff $out $ref
        if [ $? -ne 0 ] 
        then	
            echo " FAILED"
        else
            echo " ok"
        fi
    else
        echo " FAILED: $PREFIX/bin/$shell does not exist!"
    fi
done

echo $SEPARATOR
echo "\007"; sleep 1;echo "\007"; sleep 1;echo "\007"; sleep 1
echo "Check completed. Please check the output for possible errors."

. $CWD/standardEpilogue

#
#___oOo___
