#!/bin/bash

## -------------------  check_system -------------------------------- ##
##                                                         HDM 05/06  ##
########################################################################
#  This script checks the current system for available compilers etc.  #
#                                                                      #
########################################################################


minu40='----------------------------------------'
minu20='--------------------'
minu10='----------'
star40='****************************************'
star20='********************'

if [ "$1" = "-h" ] || [ "$2" = "-h" ] ||  [ "$3" = "-h" ] ; then
   echo "$minu40$minu40"
   echo "MCTDH Check System."
   echo "Purpose: Checks the system for availability of compilers,"\
        "make, and Gnuplot."
   echo "Usage  : ./check_system [-h]   (while being in install directory)."
   echo '-h     : print this help text.'
   echo "$minu40$minu40"
   exit
fi


echo "$star40$star40"
echo '*'"$star20    -------  CHECK SYSTEM  -------    $star20"'*'
echo "$star40$star40"
echo
echo $(date)

#-----------------------------------------------------------------------
# SETTING UP ENVIRONMENT VARIABLE MCTDH_DIR.
# The script thus may be called form any directory.
#-----------------------------------------------------------------------
bname=`basename $0`
path=${0%$bname} ; path=${path%/}
if [ -n "${path%.}" ] ; then cd $path ; fi
path=$(pwd)
path=${path%/}
path1=${path#"$ignore"}
if [ "$path1" != "$path" ] ; then  path="$substi$path1" ; fi
mctdh_dir=${path%/*}

echo
echo "The path of the MCTDH-directory is: $mctdh_dir"
echo

system=`uname -s`
opsys=`uname -o 2>/dev/null`
host=`uname -n`
platform=`uname -m`
processor=`uname -p`
kernel=`uname -s`

file /lib/libc-*.so | grep 32-bit >/dev/null && bit='32-bit'
file /lib/libc-*.so | grep 64-bit >/dev/null && bit='64-bit'

echo "System           : $system"
echo "Platform         : $platform"
echo "Operating-System : $opsys"
echo "Machine          : $host"
echo "Processor        : $processor"
echo "Kernel           : $kernel"
echo
if [ -n "$bit" ]; then
   echo "This is a $bit system."
else
   echo "Wordlength (32 or 64 bit) unknown for this system."
fi
echo
echo "These are the variables determined by platform.cnf"
source platform.cnf.def
echo "MCTDH_PLATFORM = $MCTDH_PLATFORM"
echo "MCTDH_COMPILER = $MCTDH_COMPILER"
if [ -n "$MCTDH_GFORTRAN_VERSION" ]; then
   echo "MCTDH_GFORTRAN_VERSION = $MCTDH_GFORTRAN_VERSION"
fi
if [ -n "$MCTDH_G77_VERSION" ]; then
   echo "MCTDH_G77_VERSION = $MCTDH_G77_VERSION"
fi
echo
echo "These are the default compilers"
source compile.cnf_le
echo "Fortran compiler: $FC"
echo "C compiler      : $CC"
echo "make command    : $MCTDH_MAKE_CMD"
echo
echo "If you want to use other compilers please edit platform.cnf* and"
echo "possibly also compile.cnf* (or use the -c option when compiling)."
echo

fc=`which $FC | fgrep "/$FC"`
cc=`which $CC | fgrep "/$CC"`
mk=`which $MCTDH_MAKE_CMD | fgrep "/$MCTDH_MAKE_CMD"`
if [ -n "$fc" ]; then
   echo "The Fortran compiler $FC is $fc"
else
   echo "Sorry, the Fortran compiler $FC does not exist on your system!"
fi
if [ -n "$cc" ]; then
   echo "The C compiler $CC is $cc"
else
   echo "Sorry, the C compiler $CC does not exist on your system!"
fi
if [ -n "$mk" ]; then
   echo "The make command  $MCTDH_MAKE_CMD  is $mk"
   gnumake=`$MCTDH_MAKE_CMD --version 2>/dev/null | fgrep GNU`
   if [ -n "$gnumake" ]; then
      echo "Congratulation, you have a GNU make."
   else
      echo "This does not seem to be a GNU make. You may run into problems."
   fi
else
   echo "Sorry, the make $MCTDH_MAKE_CMD does not exist on your system!"
fi

echo
gnup=`gnuplot --version 2>/dev/null | fgrep gnuplot`
if [ -n "$gnup" ]; then
   echo "GNUPLOT exist on your system: $gnup"
fi
gnpv=${gnup%patchlevel*}
gnpv=`echo "$gnpv" | grep " [456789]\.[0123456789]"`
if [ -z "$gnpv" ]; then
   echo "The GNUPLOT version is not 4.0 or higher, consider upgrading."
fi

echo
pyth=`python --version 2>&1 | fgrep -i python`
if [ -n "$pyth" ]; then
   echo "Python exist on your system: $pyth"
fi
pythv=`echo "$pyth" | grep " [3]\.[0123456789]"`
if [ -n "$pythv" ]; then
   echo "The Python version is 3.x, please use 2.x with x=4 or higher."
   pyth=' 2.6'
fi
pythv=`echo "$pyth" | grep " [2]\.[456789]"`
if [ -z "$pythv" ]; then
   echo "The Python version is not 2.x with x=4 or higher, consider upgrading."
fi

echo
bashv=`bash --version | egrep '(bash|version)' | cut -c 1-35`
echo "Your bash is: $bashv"

echo
mpi=`mpif77 -v 2>&1 | grep -i 'version '`
if [ -n "$mpi" ]; then
   echo "Distributed memory parallelization with MPI seems to be possible!"
   echo "For this use Fortran compiler consistent with:"
   echo "$mpi"
else
   echo "Distributed memory parallelization with MPI is NOT possible!"
   echo "To enable the use of MPI you have first to install and"
   echo "configure the relevant software."
fi

echo
pth=`whereis pthread.h | grep 'pthread.h'`
if [ -n "$pth" ]; then
   echo "Shared memory parallelization with POSIX-Threads seems to be possible!"
else
   echo "Shared memory parallelization with POSIX-Threads is NOT possible!"
   echo "To enable the use of POSIX-Threads you have first to install and"
   echo "configure the relevant software."
fi

echo
numa=`whereis numa.h | grep 'numa.h'`
if [ -n "$numa" ]; then
   echo "The use of the NUMA library for the shared memory parallelization"
   echo "with POSIX-Threads seems to be possible!"
   echo 'Compile with "-u" option to enable the use of NUMA. (See "compile -h").'
else
   echo "The use of the NUMA library for the shared memory parallelization"
   echo "with POSIX-Threads seems NOT to be possible!"
   echo "To enable the use of NUMA you have first to install and"
   echo "configure the relevant software. (Try: compile -u mctdh)"
fi

if [ `uname -s` = Darwin ]; then
   echo
   echo 'You are running on a Mac OS (Darwin).'
   echo 'As certain software and libraries are missing on this OS, one has to'
   echo 'choose "no parallelization", ie. "lenp" (or benp) when the install'
   echo 'script ask for choosing between LITTLE ENDIANS or BIG ENDIANS formats.'
fi

if [ -n "$fc" ] && [ -n "$cc" ] && [ -n "$gnumake" ]; then
   echo
   echo 'The standard installation should work without problems.'
   echo 'If not already done, you should now run "./install_mctdh".'
fi

echo
echo "$star40$star40"
echo "$star20  -----   Finished System Check  -----  $star20"
echo "$star40$star40"
echo

exit

