#!/bin/bash -vx

# partial build script for cutest
# version for Bourne/bash shell

# syntax: ginstall_cutest

# N. Gould, D. Orban & Ph. Toint
# ( Last modified on 18 March 2013 at 13:45 GMT )

#  function to compile selected subset

cutest_compile () {
  echo -e "\n Installing the $PREC precision version"
  MACROS="PRECIS=$PREC PWD=$CUTEST/src CUTEST=$CUTEST"

  if (( $MATLAB_REQUIRED )) && [[ $PREC == 'double' ]] ; then
    if (( $DRYRUN )) ; then
      echo "$MAKE $OPTIONS all all_matlab $MACROS"
    else
      echo " compiling in $CUTEST/src with the comand"
      echo " $MAKE $OPTIONS all all_matlab $MACROS"
      $MAKE $OPTIONS all all_matlab $MACROS
      [[ $? != 0 ]] && exit 2
    fi
  else
    if (( $DRYRUN )) ; then
      echo "$MAKE $OPTIONS all $MACROS"
    else
      echo " compiling in $CUTEST/src with the comand"
      echo " $MAKE $OPTIONS all $MACROS"
      $MAKE $OPTIONS all $MACROS
      [[ $? != 0 ]] && exit 2
    fi
  fi
}

export CUTEST=`dirs -l`
export CUTEST=`echo $CUTEST | $SED 's"/tmp_mnt""'`

if [[ -e $CUTEST/versions/$VERSION ]]; then
    warning " There is already an installed CUTEST version $VERSION"
    yesno_default_yes ' Do you wish to proceed ... existing version will be updated'
    [[ $? != 1 ]] && exit 4
fi

#  ensure that objects and modules directories exist

if [[ ! -e $CUTEST/objects ]]; then
  $MKDIR $CUTEST/objects
fi

if [[ ! -e $CUTEST/modules ]]; then
  $MKDIR $CUTEST/modules
fi

#  create architecture-dependent object and module directories

OBJDIR=$CUTEST/objects/$VERSION
MODDIR=$CUTEST/modules/$VERSION
PKGDIR=$CUTEST/packages/$VERSION

echo "$MACHINE ($OPSYS) $COMPUSED" > $CUTEST/versions/$VERSION


if [[ ! -e $OBJDIR ]]; then
    $MKDIR $OBJDIR
    $MKDIR $OBJDIR/double $OBJDIR/single $OBJDIR/quadruple
    $MKDIR $OBJDIR/double_64 $OBJDIR/single_64 $OBJDIR/quadruple_64
else
    if [[ ! -e $OBJDIR/double ]]; then
    $MKDIR $OBJDIR/double
    fi
    if [[ ! -e $OBJDIR/single ]]; then
    $MKDIR $OBJDIR/single
    fi
    if [[ ! -e $OBJDIR/quadruple ]]; then
    $MKDIR $OBJDIR/quadruple
    fi
    if [[ ! -e $OBJDIR/double_64 ]]; then
    $MKDIR $OBJDIR/double_64
    fi
    if [[ ! -e $OBJDIR/single_64 ]]; then
    $MKDIR $OBJDIR/single_64
    fi
    if [[ ! -e $OBJDIR/quadruple_64 ]]; then
    $MKDIR $OBJDIR/quadruple_64
    fi
fi

if [[ ! -e $MODDIR ]]; then
    $MKDIR $MODDIR
    $MKDIR $MODDIR/double $MODDIR/single $MODDIR/quadruple
    $MKDIR $MODDIR/double_64 $MODDIR/single_64 $MODDIR/quadruple_64
else
    if [[ ! -e $MODDIR/double ]]; then
    $MKDIR $MODDIR/double
    fi
    if [[ ! -e $MODDIR/single ]]; then
    $MKDIR $MODDIR/single
    fi
    if [[ ! -e $MODDIR/quadruple ]]; then
    $MKDIR $MODDIR/quadruple
    fi
    if [[ ! -e $MODDIR/double_64 ]]; then
    $MKDIR $MODDIR/double_64
    fi
    if [[ ! -e $MODDIR/single_64 ]]; then
    $MKDIR $MODDIR/single_64
    fi
    if [[ ! -e $MODDIR/quadruple_64 ]]; then
    $MKDIR $MODDIR/quadruple_64
    fi
fi

[[ ! -e $PKGDIR ]] && $MKDIR $PKGDIR
[[ ! -e $PKGDIR/double ]] && $MKDIR $PKGDIR/double
[[ ! -e $PKGDIR/single ]] && $MKDIR $PKGDIR/single
[[ ! -e $PKGDIR/quadruple ]] && $MKDIR $PKGDIR/quadruple

if [[ $QUADREAL == "yes" ]] ; then
  QUADFLAG="-DCUTEST_16btye_reals_exist"
else
  QUADFLAG=""
fi

#  write out the cutest/bin/sys file for this architecture

SYSFILE=$CUTEST/bin/sys/$VERSION

echo 'RM="'$RM'"'                                                  >  $SYSFILE
echo 'MAKE="'$MAKE'"'                                              >> $SYSFILE
echo 'CAT="'$CAT'"'                                                >> $SYSFILE
echo 'SED="'$SED'"'                                                >> $SYSFILE
echo 'MV="'$MV'"'                                                  >> $SYSFILE
echo 'CP="'$CP'"'                                                  >> $SYSFILE
echo 'LS="'$LS'"'                                                  >> $SYSFILE
echo 'LN="'$LN'"'                                                  >> $SYSFILE
echo 'FORTRAN="'$FORTRAN'"'                                        >> $SYSFILE
MOD='$CUTEST/modules/'$VERSION'/$PRECIS'
FFLAGS="$LIBCMD"' '`eval echo $MODCMD`' '"$F90"
echo 'FFLAGS="'$FFLAGS' '$OPENMP'"'                                >> $SYSFILE
echo 'PROBFLAGS="'$FFLAGS' '$BASIC' '$OPTIMIZATION' '$F77' "'      >> $SYSFILE
echo 'BLAS="'$BLAS'"'                                              >> $SYSFILE
echo 'LAPACK="'$LAPACK'"'                                          >> $SYSFILE

#  write out the cutest/makefile/ file for this architecture

MAKEFILE=$CUTEST/makefiles/$VERSION

echo ' '                                                           >  $MAKEFILE
echo '#  Architecture dependent makefile'                          >> $MAKEFILE
echo '#  (automatically generated by install_cutest)'              >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'VERSION = '$VERSION                                          >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo '#  Basic system commands'                                    >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'CP = '$CP                                                    >> $MAKEFILE
echo 'MV = '$MV                                                    >> $MAKEFILE
echo 'RM = '$RM                                                    >> $MAKEFILE
echo 'LN = '$LN                                                    >> $MAKEFILE
echo 'SED = '$SED                                                  >> $MAKEFILE
echo 'GREP = '$GREP                                                >> $MAKEFILE
echo 'AR = '$AR                                                    >> $MAKEFILE
echo 'ARREPFLAGS = '$ARREPFLAGS                                    >> $MAKEFILE
echo 'RANLIB = '$RANLIB                                            >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo '#  Directory for binaries'                                   >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'PRECIS = double'                                             >> $MAKEFILE
echo 'OBJ = $(CUTEST)/objects/$(VERSION)/$(PRECIS)'                >> $MAKEFILE
echo 'OBJS = $(CUTEST)/objects/$(VERSION)/single'                  >> $MAKEFILE
echo 'OBJD = $(CUTEST)/objects/$(VERSION)/double'                  >> $MAKEFILE
echo 'OBJQ = $(CUTEST)/objects/$(VERSION)/quadruple'               >> $MAKEFILE
echo 'OBJS64 = $(CUTEST)/objects/$(VERSION)/single_64'             >> $MAKEFILE
echo 'OBJD64 = $(CUTEST)/objects/$(VERSION)/double_64'             >> $MAKEFILE
echo 'OBJQ64 = $(CUTEST)/objects/$(VERSION)/quadruple_64'          >> $MAKEFILE
echo 'MOD = $(CUTEST)/modules/$(VERSION)/$(PRECIS)'                >> $MAKEFILE
echo 'SEDS = $(CUTEST)/seds/$(PRECIS).sed'                         >> $MAKEFILE
echo 'MVMODS = '"$MVMODS"                                          >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo '#  Compiler options'                                         >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'FORTRAN = '$FORTRAN                                          >> $MAKEFILE
echo 'BASIC = '$BASIC                                              >> $MAKEFILE
MODTMP="$LIBCMD"' '`echo $MODCMD | $SED 's/MOD/(MOD)/g'`
echo 'MODULES = '$MODTMP                                           >> $MAKEFILE
echo 'OPTIMIZATION = '$OPTIMIZATION                                >> $MAKEFILE
echo 'NOOPTIMIZATION = '$NOOPTIMIZATION                            >> $MAKEFILE
echo 'DEBUG = '$DEBUG                                              >> $MAKEFILE
echo 'OPENMP = '$OPENMP                                            >> $MAKEFILE
echo 'F77 = '$F77                                                  >> $MAKEFILE
echo 'F90 = '$F90                                                  >> $MAKEFILE
echo 'F95 = '$F95                                                  >> $MAKEFILE
echo 'NOFMAIN = '$NOFMAIN                                          >> $MAKEFILE
echo 'USUAL = '$USUAL                                              >> $MAKEFILE
echo 'SPECIAL = '$SPECIAL                                          >> $MAKEFILE
echo 'F77SUFFIX = '$F77SUFFIX                                      >> $MAKEFILE
echo 'F95SUFFIX  = '$F95SUFFIX                                     >> $MAKEFILE
echo 'TIMER = '$TIMER                                              >> $MAKEFILE
echo 'NOT95 = '$NOT95                                              >> $MAKEFILE
echo 'NOT64 = '$NOT64                                              >> $MAKEFILE
echo 'INTEGER = '$INTEGER                                          >> $MAKEFILE
echo 'QUADREAL = '$QUADFLAG                                        >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'AMPLDIR   = '$AMPLLIBDIR                                     >> $MAKEFILE
echo 'CC        = '$CC                                             >> $MAKEFILE
echo 'CCBASIC   = '$CCBASIC                                        >> $MAKEFILE
echo 'CCISO     = '$CCISO                                          >> $MAKEFILE
echo 'CCONDEF   = '$CCONDEF                                        >> $MAKEFILE
echo 'CCDEBUG   = '$CCDEBUG                                        >> $MAKEFILE
echo 'CCFFLAGS  = '$CCFFLAGS                                       >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo '#  Special flags'                                            >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo '#  Libraries'                                                >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'BLAS = '$BLAS                                                >> $MAKEFILE
echo 'LAPACK = '$LAPACK                                            >> $MAKEFILE
echo 'CUTESTUSED = '                                               >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo '#  Shell used'                                               >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'BINSHELL = '$BINSHELL                                        >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo '#  Set directories for optional packages'                    >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'include $(CUTEST)/src/makedefs/packages'                     >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo '#  Body of makefile'                                         >> $MAKEFILE
echo ' '                                                           >> $MAKEFILE
echo 'include $(PWD)/makemaster'                                   >> $MAKEFILE

#  optionally compile the selected packages

yesno_default_yes ' Would you like to compile CUTEst ...'

if [[ $? != 0 ]]; then
  cd $CUTEST/src/
  OPTIONS="-s -f $CUTEST/makefiles/$VERSION"

  if [[ $QUADREAL == "yes" ]] ; then
    if [[ $GALAHAD_REQPREC == "" ]] ; then
      echo -e '\n CUTEst may be compiled in (S)ingle, (D)ouble or'
      echo -e ' (Q)uadruple precision or (A)ll of these.'
      read -p ' Which precision do you require for the installed subset (D/s/q/a) ? ' -r -n 1 GALAHAD_REQPREC;
      if [[ $GALAHAD_REQPREC == "" ]] ; then
        GALAHAD_REQPREC="D"
      fi
    fi

  else
    if [[ $GALAHAD_REQPREC == "" ]] ; then
      echo -e '\n CUTEst may be compiled in (S)ingle or (D)ouble precision or (B)oth.'
      read -p ' Which precision do you require for the installed subset (D/s/b) ? ' -r -n 1 GALAHAD_REQPREC;
      if [[ $GALAHAD_REQPREC == "" ]] ; then
        GALAHAD_REQPREC="D"
      fi
    fi
  fi

  if [[ $GALAHAD_REQINT == "" ]] ; then
    echo -e '\n It may also be compiled for 32-bit (I)ntegers or 64-bit (L)ong ones.'
    read -p ' Which integer type do you require for the installed subset (I/l) ? ' -r -n 1 GALAHAD_REQINT;
    if [[ $GALAHAD_REQINT == "" ]] ; then
      GALAHAD_REQINT="I"
    fi
  fi

  if [[ $GALAHAD_REQPREC == [Ss] ]] ; then
    if [[ $GALAHAD_REQINT == [Ll] ]] ; then
      PREC='single_64'
    else
      PREC='single'
    fi
  elif [[ $GALAHAD_REQPREC == [Qq] ]] ; then
    if [[ $GALAHAD_REQINT == [Ll] ]] ; then
      PREC='quadruple_64'
    else
      PREC='quadruple'
    fi
  else
    if [[ $GALAHAD_REQINT == [Ll] ]] ; then
      PREC='double_64'
    else
      PREC='double'
    fi
  fi
  cutest_compile

#  optionally compile the selected packages in the other (single) precision

  if [[ $GALAHAD_REQPREC == [Bb] ]] ; then
    if [[ $GALAHAD_REQINT == [Ll] ]] ; then
      PREC='single_64'
    else
      PREC='single'
    fi
    cutest_compile
  fi

#  optionally compile the selected packages in all the other precisions

  if [[ $GALAHAD_REQPREC == [Aa] ]] ; then
    if [[ $GALAHAD_REQINT == [Ll] ]] ; then
      PREC='single_64'
    else
      PREC='single'
    fi
    cutest_compile
    if [[ $GALAHAD_REQINT == [Ll] ]] ; then
      PREC='quadruple_64'
    else
      PREC='quadruple'
    fi
    cutest_compile
  fi

  success 'CUTEst successfully installed'
fi

#exit 0
