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

arch=$1; libname=$2; verb=$3

  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=`echo $osystem | tr A-Z a-z`
hell='/dev/null'
'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 ' ERROR: Neither Intel nor Gnu FORTRAN compiler found...'
        echo '        Cannot compile library.'; echo; exit 99
fi
scriptname=`basename "$0"`; script=`echo $scriptname | tr a-z A-Z`; cdate=`date +%d\ %b\ %Y\ %a\ %H:%M:%S\ %Z`
echo
echo ' === '$cdate' === '$script' === '$osystem': '$libname
echo
echo ' LIBRARY: '$libname
echo

if [[ $rcgnu77 -eq 0 ]]
then
  fortran='f77'
  foptions='-m'$arch' -cpp -O2 -c -ffixed-line-length-0'
  foptions1=''
  foptions2=''
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
  foptions='-m'$arch' -cpp -O2 -c -ffixed-line-length-0'
  foptions1=''
  foptions2=''
fi
      
if [[ $rcifort -eq 0 ]]
then
  fortran='ifort'
  foptions='-m'$arch' -fpp -132 -O2 -c'
  foptions1='-fp-model precise -fp-speculation off -warn all -warn nointerfaces -watch source -traceback'
  foptions2='-heap-arrays -parallel -module modules -par-report1 -openmp-report1'
fi
echo ' Using FORTRAN compiler '\'$fortran\'
echo

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
fi

echo ' Optimizing: '$foptions
if [[ $foptions1 != '' ]]; then echo ' '$foptions1; fi
if [[ $foptions2 != '' ]]; then echo ' '$foptions2; fi
echo

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

$fortran $foptions $foptions1 $foptions2 $libname'.for'; rc=$?; if [[ $rc -gt 0 ]]; then exit $rc; fi

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
    echo; echo ' Copying library 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

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