#!/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
#    \\/     M anipulation  |
#-------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software; you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by the
#     Free Software Foundation; either version 2 of the License, or (at your
#     option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM; if not, write to the Free Software Foundation,
#     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script
#     foamSystemCheck
#
# Description
#     Checks the machine system and the user's 
#     personal configuration for running FOAM.
#
#------------------------------------------------------------------------------

TMPTAB=tmptab.$$
LOG=log.$0
HLINE="-----------------------------------------------------------------------"

FATALERROR=0
SSHRSHOK=0
WIDTH=20
FOAM_VERSION=2.3
OS=`uname -s`

#FUNCTIONS
#~~~~~~~~~
heading () {
    echo ""
    echo ""
    echo "$1"
    echo "$HLINE"
}

lenBase () {
echo $1 | tr -d " " | wc -m | tr -d " "
}

length () {
    NOCHAR=`lenBase $1`
    NOCHAR=`expr $NOCHAR - 1`
    if [ $NOCHAR -eq -1 ]; then
        NOCHAR=0
    fi
    echo $NOCHAR
}

fixlen () {
    WORD=$1
    ONELEN=`length "$1"`
    LDIFF=`expr $ONELEN - $2`
    if [ $LDIFF -le 1 ]; then
        while [ $LDIFF -lt 0 ] ; do
            WORD="$WORD "
            LDIFF=`expr $LDIFF + 1`
        done
        echo "$WORD"
    else
        LDIFF=`expr $LDIFF + 4`
        WORD=`echo "$WORD" | cut -c${LDIFF}-`
        echo "...${WORD}"
    fi
}


checkSetup () {
cat <<EOF
Host: $HOST_NAME
OS: $OS
OS_version: $OS_VERSION
Shell: $USER_SHELL
User: $USER_NAME
EOF
}

checkEnvVariables () {
echo '$HOSTNAME:' `expenv "$HOSTNAME"`
}

checkNetworking () {
pingTest $HOST_NAME
pingTest localHost
checkRsh
checkSsh
}

pingTest () {
case $OS in
    SunOS) 
        if [ -x '/usr/sbin/ping' ] ; then
            PINGTEST=`/usr/sbin/ping $1 2>&1`
            if [ -n "`echo $PINGTEST | grep 'alive'`" ] ; then
               RESULT="Successful"
            elif [ -n "`echo $PINGTEST | grep 'unknown host'`" ] ; then
               RESULT="Unknown_host"
            else
               RESULT="Networking_not_set" 
            fi
        else
            RESULT="Ping_not_installed_in_usual_path"
        fi
        ;;
    FreeBSD)
        if [ -x '/sbin/ping' ] ; then
            PINGTEST=`/sbin/ping -c 1 $1 2>&1`
            if [ -n "`echo $PINGTEST | grep '1 packets received'`" ] ; then
                RESULT="Successful"
            elif [ -n "`echo $PINGTEST | grep 'unknown host'`" ] ; then
                RESULT='Unknown host'
            else
                RESULT='Networking not set'
            fi
        else
            RESULT="Ping_not_installed_in_usual_path"
        fi
        ;;

    *) 
        if [ -x '/bin/ping' ] ; then
            PINGTEST=`/bin/ping -w 3 -c 1 $1 2>&1`
            if [ -n "`echo $PINGTEST | grep '1 received'`" ] ; then
                RESULT="Successful"
            elif [ -n "`echo $PINGTEST | grep 'unknown host'`" ] ; then
                RESULT='Unknown host'
            else
                RESULT='Networking not set'
            fi
        else
            RESULT="Ping_not_installed_in_usual_path"
        fi
        ;;
esac
echo "`fixlen "Ping_$1:" $WIDTH` $RESULT"
if [ "$RESULT" != 'Successful' ]; then
    FATALERROR=`expr $FATALERROR + 1`
fi

}

telnetPortTest () {
telnet -e A $1 $2 <<EOF
A
quit
EOF
}

checkTelnetPort () {
if  [ -x "/usr/bin/telnet" ] || [ -x "/bin/telnet" ] ; then 
    RESULT=`telnetPortTest $1 $2 2>&1 | egrep "onnect.* [t|r]"` 
    if [ "`echo $RESULT | grep 'Connected to'`" ] ; then 
        RESULT='Successful' 
    elif [ "`echo $RESULT | grep 'Connection refused'`" ] ;
    then 
        RESULT='Unsuccessful, connection refused*' 
    else 
        RESULT="Not active*" 
    fi 
else 
    RESULT='No_telnet_running:_cannot_check*' 
fi 
}

checkRsh () {
checkTelnetPort $HOST_NAME 222
echo "`fixlen "Test_rsh:" $WIDTH` $RESULT"
if [ "$RESULT" != 'Successful' ]; then
    SSHRSHOK=`expr $SSHRSHOK + 1`
fi
}

checkSsh () {
checkTelnetPort $HOST_NAME 22
echo "`fixlen "Test_ssh:" $WIDTH` $RESULT"
if [ "$RESULT" != 'Successful' ]; then
    SSHRSHOK=`expr $SSHRSHOK + 1`
fi
}

expenv () {
    eval "echo $1"
}

findExec() {
OLD_IFS=$IFS
IFS=':'
for d in $1
do
    if [ ! -d "$d/$2" -a -x "$d/$2" ]; then
        IFS=$OLD_IFS
        echo "$d/$2"
        return 0
    fi
done
IFS=$OLD_IFS
return 1
}

reportExecutable () {
VERSION=""
APP_PATH=`findExec "$PATH" $1`                     # Leave this...
if [ $? -ne 0 ] ; then
    echo "$1: ---- Not_installed"
    return 1
fi
#APP_PATH=`echo $APP_PATH | sed -e s%"$HOME"%"~"%g` # ... as it is.
case $1 in
    icoFoam) VERSION=`$1 2>&1 | grep ' Version:' | sed -e 's/.*Version:/Version:/' | cut -d" " -f2`;;
    gcc) VERSION=`$1 -v 2>&1 | grep 'version' | cut -d" " -f3`;;
    gtar|tar) VERSION=`$APP_PATH --version 2>&1 | head -1 | sed -e s/"[^1-9.]"/""/g`;;
#    gtar|tar) VERSION=`$APP_PATH --version | head -1` ;;
    gzip) VERSION=`$1 --version 2>&1 | head -1 | cut -d" " -f2`;;
    dx) unset DXEXEC; VERSION=`$1 -version 2>&1 | grep 'Open ' | grep 'Script' | cut -d" " -f6`;;
    java) VERSION=`$1 -version 2>&1 | grep 'version' | cut -d" " -f3 | tr -d "\""`;;
esac
APP_PATH=`echo $APP_PATH | sed -e s%"$HOME"%"~"%g` # ... as it is.
echo "$1: $VERSION $APP_PATH"
}

checkInstalledSoftware () {
if [ "$OS" = "Linux" ] ; then
    reportExecutable tar
else
    reportExecutable gtar 
fi
reportExecutable gzip
reportExecutable gcc
reportExecutable java
reportExecutable dx
reportExecutable icoFoam
}

#MAIN CODE
#~~~~~~~~~
HOST_NAME=`uname -n`
#HOST_NAME=""
OS=`uname -s`
#OS=""
OS_VERSION=`uname -r`
#case $OS in
#    AIX*) OS_VERSION="`uname -v`.`uname -r`" ;;
#    *) OS_VERSION=`uname -r` ;;
#esac
USER_SHELL=`basename $SHELL`
#USER_SHELL="polly"
USER_NAME=$LOGNAME
#USER_NAME=""
#USER="ed"
#
if [ `length $USER_NAME` -eq 0 ] ; then
    USER_NAME=$USER
fi
#
WIDTH=20

heading "Checking basic system..."
case $USER_SHELL in
    csh|tcsh) USER_CONFIG_TYPE=cshrc
              echo "`fixlen "Shell:" $WIDTH` $USER_SHELL"
              ;;
    bash|ksh) USER_CONFIG_TYPE=bashrc
              echo "`fixlen "Shell:" $WIDTH` $USER_SHELL"
              ;;
    *) USER_CONFIG_TYPE=""
       echo "`fixlen "Shell:" $WIDTH` $USER_SHELL"
       echo "FATALERROR: Cannot identify the current shell."
       echo "            Foam ${FOAM_VERSION} is compatible"
       echo "            with csh, tcsh, ksh and bash."
       echo
       FATALERROR=`expr $FATALERROR + 1`;;
esac
#
if [ `length $HOST_NAME` -eq 0 ]; then
    echo "`fixlen "Host:" $WIDTH` ${HOST_NAME}"
    echo "FATALERROR: Cannot stat hostname."
    echo "            Foam ${FOAM_VERSION} needs a valid hostname to function."
    echo "            Contact your system administrator. "
    echo
    FATALERROR=`expr $FATALERROR + 1`
else
    echo "`fixlen "Host:" $WIDTH` ${HOST_NAME}"
fi
#
if [ "$OS" = "Linux" ] || [ "$OS" = "LinuxAMD64" ] || [ "$OS" = "SunOS" ]; then
    echo "`fixlen "OS:" $WIDTH` ${OS} version ${OS_VERSION}"
else
    echo "FATALERROR: Incompatible operating system \"$OS\"."
    echo "            Foam${FOAM_VERSION} is currently available for "
    echo "            Linux, LinuxAMD64 and SunOS only."
    echo
    FATALERROR=`expr $FATALERROR + 1`
fi
#
if [ `length $USER_NAME` -eq 0 ]; then
    echo "`fixlen "User:" $WIDTH` ${USER_NAME}"
    echo "FATALERROR: Cannot stat user name ${USER_NAME}." 
    echo "            Foam${FOAM_VERSION} needs a valid user name."
    echo "            Contact your system administrator. "
    echo ""
    FATALERROR=`expr $FATALERROR + 1`
else
    echo "`fixlen "User:" $WIDTH` ${USER_NAME}"
fi
#

#heading "Checking environment variables..."
#checkEnvVariables |  awk '{printf "%-12s %-50s\n", $1, $2}'

heading "Checking networking..."
checkNetworking 
#| awk '{printf "%-20s %-50s\n", $1, $2}'
if [ $SSHRSHOK -eq 1 ]; then
    echo "(*) Only one of rsh or ssh is required by the Foam enviroment."
elif [ $SSHRSHOK -gt 1  ]; then
    echo "FATALERROR: No remote shell available."
    echo "            Foam${FOAM_VERSION} enviroment requires passwordless "
    echo "            'ssh' and/or 'rsh' to the current host."
    echo "            Contact your system administrator."
    echo
    FATALERROR=`expr $FATALERROR + 1`
fi
#echo '(*) Note: rsh/ssh tests are not necessarily conclusive.'
#echo '    If either are not successful, please check manually by executing:'
#echo '    rsh $HOSTNAME (or ssh $HOSTNAME)'

echo ""
echo ""
if [ $FATALERROR -gt 0 ]; then
    echo "System check: FAIL"
    echo "=================="
    echo "Your system is not currently compatible with Foam installation "
    echo "requirements. Review the error messages and consult the documentation"
    echo "for further instructions."
    echo
else
    echo "System check: PASS"
    echo "=================="
    echo "Continue Foam installation."
    echo
fi

#------------------------------------------------------------------------------
