#!/bin/bash
banner[1]='_______________________________________________________________________________'
banner[2]='                                                                               '
banner[3]='                                     COMPLIB                                   '
banner[4]='                         • Compile GETSOURCES Library •                        '
banner[5]='              Alexander Men`shchikov, DAp IRFU CEA Saclay, France              '
banner[6]='                                Version 2.180510                               '
banner[7]='_______________________________________________________________________________'

executing="$0"; scriptname=`basename "$executing"`; script=`printf "%s\n" $scriptname | tr a-z A-Z`; originpath=`pwd`
revision=''; default=''; verb=''; cdate=`date +%d\ %b\ %Y\ %a\ %H:%M:%S\ %Z`; hell='/dev/null'; logfile=$hell
arch=$1; libname=$2; compiler=$3; verb=$4
#___________________________________________________________________________________________________________________________________

print () 
{ 
  (printf "%s\n" "$*")
}
printl () 
{ 
  (printf "$*") | 'tee' -a $logfile
}
printlh () 
{ 
  (printf "%s\n" "$*") | "tee" -a $logfile | $highlight
}
#___________________________________________________________________________________________________________________________________

lenexe=${#executing}; lenscr=${#scriptname}
lastps=$(( lenexe - lenscr )); lastp=$(( lastps - 1 ))
if [[ "${executing:lastps:lenexe}" == "$scriptname" ]]
then path=${executing:0:lastp}
else path=''
fi
if [[ "$path" == '.' ]]
then path=${originpath/+tools_lib/}
fi
themefile=$path'/sasha.theme'
langfile=$path'/sasha.lang'
highlight='highlight'
$highlight --version 2>$hell >$hell; rchigh=$?
if [[ $rchigh -gt 0 && -e $langfile && -e $themefile ]]
then 
  highlight='/opt/local/bin/highlight'
  $highlight --version 2>$hell >$hell; rchigh=$?
fi
if [[ $rchigh -eq 0 ]]
then 
  highlight=$highlight" --out-format=xterm256 --config-file=$themefile --config-file=$langfile"
else
  highlight='tee'
fi
#___________________________________________________________________________________________________________________________________

chk_rc ()
{ # Checking return code and exiting if abnormal termination.
 
  local rc=$?
  if [[ $rc -eq 0 && "$1" != "" ]]; then rc=$1; fi
  if [[ $rc -gt 0 ]]
  then 
    where=$script
    if [[ $fun != "" ]]; then where=$where': '$fun; fi
    printlh
    printlh ' ERROR in '$where': RC='$rc': Aborted.'
    exit "$rc"
  fi
}
#___________________________________________________________________________________________________________________________________

chk_rch ()
{
  rc=`cat < .rc | { IFS= read -r first; printf "%s\n" "$first"; }`; chk_rc $rc
}
#___________________________________________________________________________________________________________________________________

  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
osyslowcase=`printf "%s\n" $osystem | tr A-Z a-z`
'g77'      --version &>$hell; rcgnu77=$?
'gfortran' --version &>$hell; rcgfortran=$?
'ifort'    --version &>$hell; rcifort=$?
fortranversion=''

if [[ $rcifort -eq 127 && $rcgfortran -eq 127 && $rcgnu77 -eq 127 ]]
then
  printlh; printlh ' ERROR: Neither Intel nor Gnu FORTRAN compiler found...'
           printlh '        Cannot compile library.'; printlh; exit 99
fi
if [[ $arch == "" ]]
then
  for (( b=1; b <= 7; b++ )); do printlh "${banner[b]}"; done
  printlh
  printlh ' USAGE: +complib 32|64 <libname> default|ifort|gfortran|gnu77 [-verb[0|1|2]]'
  printlh
  exit 1
fi
cdate=`date +%d\ %b\ %Y\ %a\ %H:%M:%S\ %Z`
printlh
printlh '·•· '$cdate' ·•· '$script' ·•· '$osystem' ·•· '$libname
printlh
printlh ' LIBRARY: '$libname
printlh
if [[ $rcgnu77 -eq 0 && ($compiler == "default" || $compiler == "gnu77" ) ]]
then
  fortran='f77'
  fortranversion=`$fortran --version | { IFS= read -r first; printf "%s\n" "$first"; }`
  foptions='-m'$arch' -O2 -c -ffixed-line-length-0 -Wall'
  foptions1=''
  foptions2=''
fi
if [[ $rcgfortran -eq 0 && ($compiler == "default" || $compiler == "gfortran" ) ]]
then
  fortran='gfortran'
  fortranversion=`$fortran --version | { IFS= read -r first; printf "%s\n" "$first"; }`
  foptions='-m'$arch' -O2 -c -ffixed-line-length-0 -Wall'
  foptions1=''
  foptions2=''
fi
if [[ $rcifort -eq 0 && ($compiler == "default" || $compiler == "ifort" ) ]]
then
  fortran='ifort'
  fortranversion=`$fortran --version | { IFS= read -r first; printf "%s\n" "$first"; }`
  foptions='-xHost -m'$arch' -fpp -132 -O2 -c'
  common1="-fp-speculation off -warn all -warn nointerfaces -watch source -traceback -heap-arrays -parallel -static-intel"
  if [[ "${fortranversion:0:17}" == 'ifort (IFORT) 15.' ]]
  then
    common2="-fp-model precise -module modules -qopt-report-file=+optimization.report -qopt-report-phase=vec -qopt-report-phase=par -qopt-report-phase=openmp -threads -par-num-threads=4"
    common2=$common2" -static-libstdc++ -static-libstdc"
  elif [[ "${fortranversion:0:17}" == 'ifort (IFORT) 13.' ]]
  then
    common2="-fp-model source -module modules -par-report1 -vec-report1 -openmp-report1"
    common2=$common2" -static-libstdc++"
  elif [[ "${fortranversion:0:17}" == 'ifort (IFORT) 12.' ]]
  then
    common2="-fp-model source -module modules -par-report1 -vec-report1 -openmp-report1"
  else
    common2="-fp-model source -module modules -par-report1 -vec-report1 -openmp-report1"
  fi
fi
printlh ' Using FORTRAN compiler '\'$fortran\' $fortranversion
printlh

if [[ ${libname:0:6} != "psplot" && ${libname:0:5} != "ps12d" ]]
then
  if [[ -e $libname'.ifc.for' ]]
  then
    \cp $libname'.ifc.for' $libname'.for'
  fi
  if [[ $fortran != 'ifort' ]]
  then
    \cp $libname'.for' $libname'.ifc.for'
    if [[ -e $libname'.gnu.for' ]]
    then \cp $libname'.gnu.for' $libname'.for'
    fi
    if [[ $fortran != 'gfortran' ]]
    then
      if [[ -e '../+plots_lib/'$osyslowcase'/psplot.a.gnu' ]]
      then \cp '../+plots_lib/'$osyslowcase'/psplot.a.gnu' ../tmp/psplot.a; rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi
      fi
      if [[ -e '../+plots_lib/'$osyslowcase'/ps12d.a.gnu' ]]
      then \cp '../+plots_lib/'$osyslowcase'/ps12d.a.gnu'  ../tmp/ps12d.a;  rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi
      fi
    else
      if [[ -e '../+plots_lib/'$osyslowcase'/psplot.a.gnu' ]]
      then \cp '../+plots_lib/'$osyslowcase'/psplot.a.gnu' ../tmp/psplot.a; rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi
      fi
      if [[ -e '../+plots_lib/'$osyslowcase'/ps12d.a.gnu' ]]
      then \cp '../+plots_lib/'$osyslowcase'/ps12d.a.gnu'  ../tmp/ps12d.a;  rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi
      fi
    fi
  else
    if [[ -e '../+plots_lib/'$osyslowcase'/psplot.a.ifort' ]]
    then \cp '../+plots_lib/'$osyslowcase'/psplot.a.ifort' ../tmp/psplot.a; rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi
    fi
    if [[ -e '../+plots_lib/'$osyslowcase'/ps12d.a.ifort' ]]
    then \cp '../+plots_lib/'$osyslowcase'/ps12d.a.ifort'  ../tmp/ps12d.a;  rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi
    fi
  fi
fi
printlh ' OPTIMIZING: '$foptions
if [[ $common1 != '' ]]; then printlh ' '$common1; fi
if [[ $common2 != '' ]]; then printlh ' '$common2; fi
printlh

if [[ ! -e "modules" ]]; then \mkdir -p 'modules'; fi

($fortran $foptions $common1 $common2 $libname'.for'; rc=$?; echo $rc > .rc) 2>&1 | $highlight; chk_rch

if [[ -e "modules" ]]; then \rm -R 'modules'; fi

if [[ -e $libname'.a' ]]
then
  \rm $libname'.a'; rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi
fi
'ar' rv $libname'.a' $libname'.o'; rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi
'ranlib' $libname'.a'; rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi

if [[ $fortran == 'ifort' ]]
then
  if [[ $THIS == "PROTOSERVER" && -e $PROHOME'/codes/f77' ]]
  then
    printlh; printlh ' Copying library '\'$libname'.a'\'' to '$PROHOME'/codes/f77/'$libname'.a...'
    \cp $libname'.a' $PROHOME'/codes/f77/'$libname'.a'
  fi
else
  if [[ -e $libname'.ifc.for' ]]
  then
    \cp $libname'.ifc.for' $libname'.for'
  fi
fi

if [[ $fortran != 'ifort' ]]
then
  \rm $libname'.o'
fi

printlh
if [[ $rc -eq 0 || $verb != "-verb0" ]]
then printlh ' Done.'
else printl ' Done.'
fi
exit $rc
