#!/bin/bash
banner[1]='_______________________________________________________________________________'
banner[2]='                                                                               '
banner[3]='                      COMPILE = Compile GETSOURCES Utility                     '
banner[4]='              Alexander Men`shchikov, SAp IRFU CEA Saclay, France              '
banner[5]='                                Version 1.140127                               '
banner[6]='_______________________________________________________________________________'
#___________________________________________________________________________________________________________________________________

chk_rc ()
{
# Checking return code and exiting if abnormal termination.
 
  local rc=$?
  if [[ "$rc" != "0" ]]
  then where=$script; if [[ $fun != "" ]]; then where=$where': '$fun; fi; echo; echo ' ERROR in '$where': Aborted.'; exit "$rc"
  fi
}
#___________________________________________________________________________________________________________________________________
#
executing="$0"; scriptname=`basename "$executing"`; script=`echo $scriptname | tr a-z A-Z`; hell='/dev/null'
revision=''; default=''; verb=''; cdate=`date +%d\ %b\ %Y\ %a\ %H:%M:%S\ %Z`

fortcode=$1; arch=$2; what=$3; verb=$4

  if [[ "$OSTYPE" == "linux-gnu" ]]; then osystem='LINUX-GNU'
elif [[ "$OSTYPE" == "linux"     ]]; then osystem='LINUX'
elif [[ "$OSTYPE" == "darwin"*   ]]; then osystem='MACOSX'
elif [[ "$OSTYPE" == "cygwin"    ]]; then osystem='CYGWIN'
elif [[ "$OSTYPE" == "win32"     ]]; then osystem='WIN32'
elif [[ "$OSTYPE" == "freebsd"*  ]]; then osystem='FREEBSD'
else osystem='UNKNOWN'
fi

if [[ $arch == "" || $arch == "-debug" || $arch == "-revision" || $arch == "-default" ]]; then what=$arch; arch='64'; fi
if [[ $what == "" || ${what:0:5} == "-verb" ]]; then revision='-revision'; default='-default'; fi
if [[ $what == "-debug" ]]; then revision='-revision'; default=''; fi
if [[ $what == "-revision" ]]; then revision='-revision'; default=''; fi
if [[ $what == "-default" ]]; then revision=''; default='-default'; fi
if [[ $verb == "" ]]; then verb='-verb0'; fi
if [[ ! -e 'tmp' ]]; then \mkdir 'tmp'; fi

if [[ "$what" != "-verb0" && "$verb" != "-verb0" ]]
then
  for (( b=1; b <= 6; b++ ))
  do echo "${banner[b]}"; done
fi

if [[ "$fortcode" != "" && ($arch == "32" || $arch == "64") && \
     ($what == "" || $what == "-debug" || $what == "-revision" || $what == "-default" || ${what:0:5} == "-verb") ]]
then echo; echo ' === '$cdate' === '$script' === '$osystem': '$fortcode $what; echo
else echo; echo ' USAGE: '$scriptname' <utilname> [32|64] [-debug|-revision|-default] [-verb0]'; echo; exit
fi

if [[ $GETSOURCES_HOME == "" ]]
then
  echo; echo ' '$script': ERROR: Environment variable GETSOURCES_HOME not set.'
        echo ' '$script':        It must point to the home directory (...)/+GETSOURCES.'; echo; exit 99
fi
if [[ $GETSOURCES_BIN == "" ]]
then
  echo; echo ' '$script': ERROR: Environment variable GETSOURCES_BIN not set.'
        echo ' '$script':        It must point to the binary directory (...)/bin/+GETSOURCES.'; echo; exit 99
else
  if [[ $revision == "-revision" ]]
  then
    path=`pwd`; revisiondir=`basename "$path"`; if [[ ${revisiondir:0:1} != "v" ]]; then revisiondir=''; fi
  fi
fi

if [[ ! -e '+tools_lib' ]]
then
  echo ' '$script': ERROR: This script must be run from the home directory of GETSOURCES.'; echo; exit 99
fi

'g77'         --version &>$hell; rcgnu77=$?
'gfortran'    --version &>$hell; rcgfort=$?
'gfortran461' --version &>$hell; rcgfort461=$?
'ifort'       --version &>$hell; rcifort=$?

if [[ $rcifort -eq 127 && $rcgfort -eq 127 && $rcgfort461 -eq 127 && $rcgnu77 -eq 127 ]]
then
  echo; echo ' '$script': ERROR: Neither Intel nor Gnu FORTRAN compiler found...'
        echo ' '$script':        Cannot install GETSOURCES.'; echo; exit 99
fi

if [[ ! -e $GETSOURCES_HOME'/libcfitsio.a' ]]
then
  echo; echo ' '$script': ERROR: CFITSIO library '\''libcfitsio.a'\'' not found in'
        echo ' '$script':        directory '$GETSOURCES_HOME; echo; exit 99
fi

if [[ $rcgnu77 -eq 0 ]]
then
  fortran='f77'
  options='-m'$arch' -cpp -O2 -ffixed-line-length-0'
  common1="-I./ -I../+tools_lib"
  common2=""
  libraries="../+tools_lib/tools.a $GETSOURCES_HOME/libcfitsio.a -lpthread"
fi

if [[ $rcgfort -eq 0 || $rcgfort461 -eq 0 ]]
then
  if [[ $rcgfort    -eq 0 ]]; then fortran='gfortran'   ; fi
  if [[ $rcgfort461 -eq 0 ]]; then fortran='gfortran461'; fi
  options='-m'$arch' -cpp -O2 -ffixed-line-length-0'
  common1="-I./ -I../+tools_lib"
  common2=""
  libraries="../+tools_lib/tools.a $GETSOURCES_HOME/libcfitsio.a -lpthread"
fi

if [[ $rcifort -eq 0 ]]
then
  fortran='ifort'
  if [[ "$what" != "-debug" ]]
  then options='-m'$arch' -fpp -132 -O2'
  else options='-m'$arch' -fpp -132 -g -C -check -ftrapuv'
  fi
  if [[ ${THIS:0:3} == "MAC" && $USER == "amenshch" ]]
  then
    options=$options' -static-intel'
  fi
  common1="-fp-model precise -fp-speculation off -warn all -warn nointerfaces -watch source -traceback"
  common2="-heap-arrays -parallel -module ../+$fortcode/modules -par-report1 -openmp-report1"
  libraries="../+tools_lib/tools.a $GETSOURCES_HOME/libcfitsio.a"
fi
echo ' Using FORTRAN compiler '\'$fortran\'
echo

if [[ "$what" != "-debug" ]]
then echo ' Optimizing: '$options
else echo '  Debugging: '$options
fi
if [[ $common1 != '' ]]; then echo ' '$common1; fi
if [[ $common2 != '' ]]; then echo ' '$common2; fi
if [[ $libraries != '' ]]; then echo ' '$libraries; fi

if [[ ! -e '+'$fortcode'/modules' ]]; then mkdir -p '+'$fortcode'/modules'; fi

\cd tmp >$hell; echo

$fortran $options $common1 $common2 -o $fortcode '../+'$fortcode'/'$fortcode'.for' $libraries; chk_rc

if [[ -e "../+$fortcode/modules" ]]; then \rm -R '../+'$fortcode'/modules'; fi

if [[ -e $GETSOURCES_BIN ]]
then
  if [[ $default == "-default" ]]
  then
    echo
    echo ' Copying binary to '$GETSOURCES_BIN'...'
    \cp $fortcode $GETSOURCES_BIN; chk_rc
  fi
  if [[ $revision == "-revision" ]]
  then
    if [[ ! -e $GETSOURCES_BIN'/'$revisiondir ]]
    then
      \mkdir $GETSOURCES_BIN'/'$revisiondir; chk_rc
    fi
    if [[ $default == "" ]]
    then echo
    fi
    echo ' Copying binary to '$GETSOURCES_BIN'/'$revisiondir'...'
    \cp $fortcode $GETSOURCES_BIN'/'$revisiondir; chk_rc
  fi
  if [[ -e $fortcode ]]
  then \rm $fortcode; chk_rc
  fi
fi

echo
if [[ $rc -eq 0 || ("$what" != "-verb0" && "$verb" != "-verb0") ]]
then
  echo ' Done.'
else
  echo -n ' Done.'
fi
exit $rc
