#!/data/data/com.termux/files/usr/bin/sh
#------------------------------------------------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#-------------------------------------------------------------------------------
#     Copyright (C) 2011 OpenFOAM Foundation
#     Copyright (C) 2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     bin/foamSystemCheck
#
# Description
#     Check the machine, software components, and the environment
#     for installing OpenFOAM.
#
#------------------------------------------------------------------------------

# General
WIDTH=12

# System variables
HOST="$(uname -n)"
OSTYPE="$(uname -s)"

# Global variables
unset fatalError criticalError

#==============================================================================
#                             HELPER FUNCTIONS
#==============================================================================

hline()
{
    echo "-------------------------------------------------------------------------------"
}


heading()
{
    echo
    echo "$1"
}

length()
{
    ## echo "length <$1>" 1>&2
    nChars="$(echo "$1" | tr -d ' ' | wc -c)"
    nChars="$((nChars - 1))"  # Remove newline from the count
    [ "$nChars" -ge 0 ] || nChars=0
    echo "$nChars"
}

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
}


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

checkEnvironment()
{
    if [ -d "$WM_PROJECT_DIR" ]
    then
        echo "$(fixlen OpenFOAM:   $WIDTH) ${WM_PROJECT_DIR##*/}"
    else
        echo
        echo "ERROR: OpenFOAM environment not configured."
        echo
        echo "    Please see the information in the README.md"
        echo "    <OpenFOAM installation dir>/OpenFOAM-${WM_PROJECT_VERSION}/README.md"
        echo "    for information on setting-up the OpenFOAM environment."
        echo
        fatalError="x${fatalError}"
        exit 1
    fi

    if [ -d "$WM_THIRD_PARTY_DIR" ]
    then
        echo "$(fixlen ThirdParty: $WIDTH) ${WM_THIRD_PARTY_DIR##*/}"
    else
        echo "$(fixlen ThirdParty: $WIDTH) [missing]"
        echo "This can be intentional, or indicate a faulty installation"
    fi
}


checkUserShell()
{
    echo "$(fixlen Shell: $WIDTH) ${SHELL##*/}"
    case "$SHELL" in
    */csh | */tcsh | */bash | */ksh)
        ;;
    */dash | */zsh)
        echo "[The ${SHELL##*/} shell is generally okay to use]"
        ;;
    *)
        echo "ERROR: Cannot identify the shell you are running."
        echo "       OpenFOAM ${WM_PROJECT_VERSION} is compatible with "
        echo "       csh, tcsh, bash, ksh (and possibly dash, zsh)"
        echo
        fatalError="x${fatalError}"
        ;;
    esac
}


checkHostName()
{
    echo "$(fixlen Host: $WIDTH) $HOST"
    if [ -z "$HOST" ]
    then
        echo "ERROR: Cannot stat hostname."
        echo "       OpenFOAM ${WM_PROJECT_VERSION} needs a valid hostname."
        echo "       Contact your system administrator."
        echo
        fatalError="x${fatalError}"
    fi
}


checkOS()
{
    case "$OSTYPE" in
    Linux* | Darwin* | SunOS )
        echo "$(fixlen OS: $WIDTH) $OSTYPE version $(uname -r)"
        ;;
    *)
        echo "ERROR: Incompatible operating system \"$OSTYPE\"."
        echo "       OpenFOAM ${WM_PROJECT_VERSION} is currently available for"
        echo "       Linux, Darwin and SunOS only."
        echo
        fatalError="x${fatalError}"
        ;;
    esac
}

#==============================================================================
#                                MAIN SCRIPT
#==============================================================================

heading "Checking basic system..."
hline

checkUserShell
checkHostName
checkOS

## # check user name
## USER_NAME="$LOGNAME"
## if [ $(length $USER_NAME) -eq 0 ]
## then
##     USER_NAME="$USER"
## fi
##
## echo "$(fixlen User: $WIDTH) ${USER_NAME}"
## if [ $(length $USER_NAME) -eq 0 ]
## then
##     echo "ERROR: Cannot stat user name $USER_NAME."
##     echo "       OpenFOAM $WM_PROJECT_VERSION needs a valid user name."
##     echo "       Contact your system administrator. "
##     echo
##     fatalError="x${fatalError}"
## fi

echo
if [ -n "$fatalError" ]
then
    cat << FAILED
System check: FAIL
==================
Your system may not compatible with the current OpenFOAM requirements.
Review the error messages and consult the documentation for further information
FAILED
else
    cat << PASSED
System check: PASS
==================
Can continue to OpenFOAM installation.
PASSED
fi

echo

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