#!/bin/bash -vx
# Build script for SIFDecode / CUTEst / GALAHAD

# syntax: select_arch

# N. Gould, D. Orban & Ph. Toint
# ( Last modified on 6 February 2013 at 07:15 GMT )

# . helper_functions

#  check input arguments (if any)

if [ $# != 0 ]; then
   echo "Use: select_arch"
   exit 1
fi

#  locate the archdefs directory

if [[ -z "$ARCHDEFS" ]]; then
  if [[ ! -d "$PWD/../archdefs" ]]; then
    error ' The environment variable ARCHDEFS is not set and the directory
 ../../archdefs does not exist. Install the archdefs package,
 set $ARCHDEFS to the archdefs directory and re-run.'
    exit 1
  else
    export ARCH=$PWD/archdefs
  fi
else
  export ARCH=${ARCHDEFS}
fi

#  determine the platform and operating system used

CORRECT_PLATFORM="false"
while [ $CORRECT_PLATFORM == "false" ]; do
   echo -e ' Select platform\n'
   echo '   (1) Compaq (DEC) alpha'
   echo '   (2) Cray'
   echo '   (3) HP workstation'
   echo '   (4) IBM PowerPC'
   echo '   (5) PC'
   echo '   (6) PC with generic 64-bit processor'
   echo '   (7) PC with 64-bit Itanium processor'
   echo '   (8) PC with 64-bit Opteron processor'
   echo '   (9) PC with 64-bit Athlon processor'
   echo '  (10) SGI workstation'
   echo '  (11) SUN workstation'
   echo '  (12) MAC OS/X'
   echo '  (13) 64-bit MAC OS/X'

   read CHOICE

   case  $CHOICE  in
       "1")
            CORRECT_PLATFORM="true"
            export MCH="alp"
            export MACHINE="Compaq Alpha"

            CORRECT_OS="false"
            while [ $CORRECT_OS == "false" ]; do

               echo -e ' Select operating system\n'
               echo '   (1) Digital Unix'
               echo '   (2) Tru-64 Unix'
               echo '   (3) Linux'

               read CHOICE

               case  $CHOICE  in
               1)
                  CORRECT_OS="true"
                  export OS="dux"
                  export OPSYS="Digital Unix"
;;
               2)
                  CORRECT_OS="true"
                  export OS="t64"
                  export OPSYS="Tru-64 Unix"
;;
               3)
                  CORRECT_OS="true"
                  export OS="lnx"
                  export OPSYS="Linux"
;;
               *)
                  echo ' Please give an integer between 1 and 3'
               esac
            done
;;
       "2")
            CORRECT_PLATFORM="true"
            export MCH="cry"
            export MACHINE="CRAY T3E"
            export OS="unc"
            export OPSYS="UNICOS"
;;
       "3")
            CORRECT_PLATFORM="true"
            export MCH="hp"
            export MACHINE="HP workstation"

            CORRECT_OS="false"
            while [ $CORRECT_OS == "false" ]; do

               echo ' Select operating system'
               echo
               echo '   (1) HP-UX'
               echo '   (2) Linux'

               read CHOICE

               case  $CHOICE  in
               1)
                  CORRECT_OS="true"
                  export OS="hpu"
                  export OPSYS="HP-UX"
;;
               2)
                  CORRECT_OS="true"
                  export OS="lnx"
                  export OPSYS="Linux"
;;
               *)
                  echo ' Please give an integer between 1 and 2'
               esac
            done
;;
       "4")
            CORRECT_PLATFORM="true"
            export MCH="ppc"
            export MACHINE="IBM PowerPC"

            CORRECT_OS="false"
            while [ $CORRECT_OS == "false" ]; do

               echo -e ' Select operating system\n'
               echo '   (1) AIX'
               echo '   (2) Linux'

               read CHOICE

               case  $CHOICE  in
               1)
                  CORRECT_OS="true"
                  export OS="aix"
                  export OPSYS="AIX"
;;
               2)
                  CORRECT_OS="true"
                  export OS="lnx"
                  export OPSYS="Linux"
;;
               *)
                  echo ' Please give an integer between 1 and 2'
               esac
            done
;;
       "5")
            CORRECT_PLATFORM="true"
            export MCH="pc"
            export MACHINE="Intel-like PC"

            CORRECT_OS="false"
            while [ $CORRECT_OS == "false" ]; do

               echo -e ' Select operating system\n'
               echo '   (1) Windows 2000/XP with MinGW/Msys'
               echo '   (2) Linux'

               read CHOICE

               case  $CHOICE  in
               1)
                  CORRECT_OS="true"
                  export OS="mgw"
                  export OPSYS="MGW"
;;
               2)
                  CORRECT_OS="true"
                  export OS="lnx"
                  export OPSYS="Linux"
;;
               *)
                  echo ' Please give an integer between 1 and 2'
               esac
            done
;;
       "6")
            CORRECT_PLATFORM="true"
            export MCH="pc64"
            export MACHINE="Intel-like PC with a 64-bit processor"

            CORRECT_OS="false"
            while [ $CORRECT_OS == "false" ]; do

               echo -e ' Select operating system\n'
               echo '   (1) Windows 2000/XP with MinGW/Msys'
               echo '   (2) Windows 10 with MinGW64/Msys2'
               echo '   (3) Linux'

               read CHOICE

               case  $CHOICE  in
               1)
                  CORRECT_OS="true"
                  export OS="mgw"
                  export OPSYS="MGW"
;;
               2)
                  CORRECT_OS="true"
                  export OS="msys2"
                  export OPSYS="MSYS2"
;;
               3)
                  CORRECT_OS="true"
                  export OS="lnx"
                  export OPSYS="Linux"
;;
               *)
                  echo ' Please give an integer between 1 and 3'
               esac
            done
;;
       "7")
            CORRECT_PLATFORM="true"
            export MCH="i64"
            export MACHINE="Intel-like PC with a 64-bit Itanium processor"
            export OS="lnx"
            export OPSYS="Linux"
;;
       "8")
            CORRECT_PLATFORM="true"
            export MCH="opt"
            export MACHINE="AMD-like PC with a 64-bit Opteron processor"
            export OS="lnx"
            export OPSYS="Linux"
;;
       "9")
            CORRECT_PLATFORM="true"
            export MCH="ath"
            export MACHINE="AMD-like PC with a 64-bit Athlon processor"
            export OS="lnx"
            export OPSYS="Linux"
;;
       "10")
            CORRECT_PLATFORM="true"
            export MCH="sgi"
            export MACHINE="SGI workstation"

            CORRECT_OS="false"
            while [ $CORRECT_OS == "false" ]; do

               echo -e ' Select operating system\n'
               echo '   (1) IRIX'
               echo '   (2) Linux'

               read CHOICE

               case  $CHOICE  in
               1)
                  CORRECT_OS="true"
                  export OS="irx"
                  export OPSYS="IRIX"
;;
               2)
                  CORRECT_OS="true"
                  export OS="lnx"
                  export OPSYS="Linux"
;;
               *)
                  echo ' Please give an integer between 1 and 2'
               esac
            done
;;
       "11")
            CORRECT_PLATFORM="true"
            export MCH="sun"
            export MACHINE="Sun workstation"

            CORRECT_OS="false"
            while [ $CORRECT_OS == "false" ]; do

               echo -e ' Select operating system\n'
               echo '   (1) Solaris'
               echo '   (2) Linux'

               read CHOICE

               case  $CHOICE  in
               1)
                  CORRECT_OS="true"
                  export OS="sol"
                  export OPSYS="Solaris"
;;
               2)
                  CORRECT_OS="true"
                  export OS="lnx"
                  export OPSYS="Linux"
;;
               *)
                  echo ' Please give an integer between 1 and 2'
               esac
            done
;;
       "12")
             CORRECT_PLATFORM="true"
             export MCH="mac"
             export MACHINE="Mac"
             export OS="osx"
             export OPSYS="MacOSX"
;;
       "13")
             CORRECT_PLATFORM="true"
             export MCH="mac64"
             export MACHINE="64-bit Mac"
             export OS="osx"
             export OPSYS="MacOSX"
;;
       *)
         echo ' Please give an integer between 1 and 13'
   esac
done

PLPLOTUSED="unused"
PLPLOT=""
MBASIC=""

unalias source 2>/dev/null

yesno_default_no ' Would you like to review and modify the system commands'
[[ $? == 1 ]] && ${FCEDIT:-${VISUAL:-${EDITOR:-vim}}} $ARCH/system.$OS

source $ARCH/system.$OS

if (( $MATLAB_REQUIRED )); then
  MATLABOMP4="no"
  CORRECT_MATLAB="false"
  while [ $CORRECT_MATLAB == "false" ]; do
     echo -e ' Select Matlab version\n'
     echo '   (1) before R2011a'
     echo '   (2) R2011a-R2013a'
     echo '   (3) R2013b-R2016a'
     echo '   (4) R2016b-R2017b'
     echo '   (5) R2018a or later'

     read CHOICE

     case  $CHOICE  in
         "1")
              CORRECT_MATLAB="true"
              if [[ ${OS} == "osx" ]]; then
                MATLABGCC="gcc"
                MATLABGCXX="g++"
                MATLABGFORTRAN="g95"
              else
                MATLABGCC="gcc"
                MATLABGCXX="g++"
                MATLABGFORTRAN="g95"
              fi
;;
         "2")
              CORRECT_MATLAB="true"
              if [[ ${OS} == "osx" ]]; then
                MATLABGCC="gcc-4.3"
                MATLABGCXX="g++-4.3"
                MATLABGFORTRAN="gfortran-4.3"
              else
                MATLABGCC="gcc-4.4"
                MATLABGCXX="g++-4.4"
                MATLABGFORTRAN="gfortran-4.4"
              fi
;;
         "3")
              CORRECT_MATLAB="true"
              if [[ ${OS} == "osx" ]]; then
                MATLABGCC="gcc-4.3"
                MATLABGCXX="g++-4.3"
                MATLABGFORTRAN="gfortran-4.3"
              else
                MATLABGCC="gcc-4.7"
                MATLABGCXX="g++-4.7"
                MATLABGFORTRAN="gfortran-4.7"
              fi
;;
         "4")
              CORRECT_MATLAB="true"
              if [[ ${OS} == "osx" ]]; then
                MATLABGCC="gcc-4.3"
                MATLABGCXX="g++-4.3"
                MATLABGFORTRAN="gfortran-4.3"
              else
                MATLABGCC="gcc-4.9"
                MATLABGCXX="g++-4.9"
                MATLABGFORTRAN="gfortran-4.9"
                MATLABOMP4="yes"
              fi
;;
         "5")
              CORRECT_MATLAB="true"
              if [[ ${OS} == "osx" ]]; then
                MATLABGCC="gcc-4.3"
                MATLABGCXX="g++-4.3"
                MATLABGFORTRAN="gfortran-4.3"
              else
                MATLABGCC="gcc-6"
                MATLABGCXX="g++-6"
                MATLABGFORTRAN="gfortran-6"
                MATLABOMP4="yes"
              fi
;;
         *)
           echo ' Please give an integer between 1 and 5'
     esac
  done
else
  if [[ ${OS} == "osx" ]]; then
    MATLABGCC="gcc-4.3"
    MATLABGFORTRAN="gfortran-4.3"
  else
    MATLABGCC="gcc-6"
    MATLABGFORTRAN="gfortran-6"
  fi
fi

if (( $MATLAB_REQUIRED )); then
  COMP=( `$LS $ARCH/compiler.${MCH}.${OS}.gfo 2>/dev/null` )
  NUMBER=${#COMP[@]}
  if (( $NUMBER )) ; then
    type -P $MATLABGFORTRAN &>/dev/null || \
     { printf " Warning: you need to install $MATLABGFORTRAN to allow\n for Matlab and CUTEst interaction. Aborting\n" >&2; exit 3; }
    echo " GNU $MATLABGFORTRAN compiler will be used for Matlab compatibility"
    COMPILER="compiler.${MCH}.${OS}.gfo"
    CMP="gfo"
    COMPUSED=`$LS $ARCH/compiler.${MCH}.${OS}.gfo 2>/dev/null`
  else
    error " GNU $MATLABGFORTRAN/$MATLABGCC compilers required for
 Matlab compatibility not available.
 Installation abandoned"
    exit 999
  fi
else
  echo -e ' Select fortran compiler\n'
  COMP=( `$LS $ARCH/compiler.${MCH}.${OS}.* \
              $ARCH/compiler.all.all.* 2>/dev/null` )
  NUMBER=${#COMP[@]}
  LIST=( ${COMP[@]} )
  let count=-1
  for i  in  ${COMP[@]}; do
    (( count++ ))
    COMP[$count]="`$SED q $i | $SED 's/^[# ]*//'`"
  done

  CORRECT_COMPILER="false"
  let count=-1
  for i  in  ${LIST[@]}; do
    (( count++ ))
    let counter=count+1
    echo "        ($counter) ${COMP[$count]}"
  done

  while [ $CORRECT_COMPILER == "false" ]; do
    read CHOICE
    let CHOICE=CHOICE-1
    if (( 0 <= CHOICE && CHOICE < NUMBER )); then
      CORRECT_COMPILER="true"
      COMPILER=${LIST[$CHOICE]##*/}
      CMP=${COMPILER##*\.} #${LIST[$CHOICE]%*\.}
      COMPUSED="${COMP[$CHOICE]}"
    else
      echo " Please give an integer between 1 and $NUMBER"
    fi
  done
fi

#  optionally edit the compiler and platform settings

yesno_default_no ' Would you like to review and modify the fortran compiler settings'
[[ $? == 1 ]] && ${FCEDIT:-${VISUAL:-${EDITOR:-vim}}} $ARCH/$COMPILER

#  record the required fortran details

source $ARCH/$COMPILER
if (( $MATLAB_REQUIRED )); then
  FORTRAN=$MATLABGFORTRAN
  CUDACOMP=$MATLABGFORTRAN
  OMP4=$MATLABOMP4
fi

VERSION=${MCH}.${OS}.${CMP}

#  use ssids as the default symmetric linear solver iff OMP4 is available

#echo "omp4 ? " $OMP4

if [ $OMP4 == "yes" ]; then
 SSIDS_REQUIRED=1
 SSIDS="ssids"
 SPRAL="$SPRAL -lstdc++ -lhwloc"
else
 SSIDS_REQUIRED=0
 SSIDS="ssids_dum"
fi

#  if a C compiler is required, get details

if (( $AMPL_REQUIRED )) || (( $MATLAB_REQUIRED )) || \
   (( $CUTEST_REQUIRED )) || (( $SSIDS_REQUIRED )); then
  if (( $MATLAB_REQUIRED )); then
    CCOMP=( `$LS $ARCH/ccompiler.${MCH}.${OS}.gcc 2>/dev/null` )
    NUMBER=${#CCOMP[@]}
    if (( $NUMBER )) ; then
      type -P $MATLABGCC &>/dev/null || \
       { echo " Warning: you need to install $MATLABGCC" \
         echo "to allow for Matlab and CUTEst interaction. Aborting " \
         >&2; exit 3; }
      echo " GNU $MATLABGCC compiler will be used for Matlab compatibility"
      CCOMPILER="ccompiler.${MCH}.${OS}.gcc"
      #CMP="gcc"
      CCOMPUSED=`$LS $ARCH/ccompiler.${MCH}.${OS}.gcc 2>/dev/null`
#     SSIDS="ssids_dum"
    else
      error " GNU $MATLABGFORTRAN/$MATLABGCC compilers required for
 Matlab compatibility not available.
 Installation abandoned"
      exit 999
    fi
  else
    echo -e ' Select C compiler\n'
    CCOMPB=( `$LS $ARCH/ccompiler.${MCH}.${OS}.* $ARCH/ccompiler.all.all.*`)
    if [ "${CMP}" == "g95" ]; then
      declare -a CCOMPC=( ${CCOMPB[@]/*.gccomp4/} )
      declare -a CCOMPD=( ${CCOMPC[@]/*.gcc/} )
      declare -a CCOMPE=( ${CCOMPD[@]/*.gc5/} )
      declare -a CCOMP=( ${CCOMPE[@]/*.gc6/} )
    else
      declare -a CCOMP=( ${CCOMPB[@]/*.g95/} )
    fi
    NUMBER=${#CCOMP[@]}
    LIST=( ${CCOMP[@]} )
    let count=-1
    for i  in  ${CCOMP[@]}; do
#     echo $i
      (( count++ ))
      CCOMP[$count]="`$SED q $i | $SED 's/^[# ]*//'`"
    done

    CORRECT_CCOMPILER="false"
    let count=-1
    for i  in  ${LIST[@]}; do
      (( count++ ))
      let counter=count+1
      echo "        ($counter) ${CCOMP[$count]}"
    done

    while [[ $CORRECT_CCOMPILER == "false" ]]; do
      read CHOICE
      let CHOICE=CHOICE-1
      if (( 0 <= CHOICE && CHOICE < NUMBER )); then
        CORRECT_CCOMPILER="true"
        CCOMPILER=${LIST[$CHOICE]##*/}
        #CMP=${COMPILER##*\.} #${LIST[$CHOICE]%*\.}
        CCOMPUSED="${COMP[$CHOICE]}"
      else
        echo " Please give an integer between 1 and $NUMBER"
      fi
    done
  fi

#  optionally edit the compiler and platform settings

  yesno_default_no ' Would you like to review and modify the C compiler settings'
  [[ $? == 1 ]] && ${FCEDIT:-${VISUAL:-${EDITOR:-vim}}} $ARCH/$CCOMPILER


#  record the required C details

  source $ARCH/$CCOMPILER
  if (( $MATLAB_REQUIRED )); then
    CC=$MATLABGCC
    CXX=$MATLABGCXX
  fi
fi

if (( $AMPL_REQUIRED )); then
  if [[ ${AMPLDIR+set} == 'set' ]]; then
    AMPLLIBDIR=$AMPLDIR
    YESNO=""
    echo ' By default, the AMPL interface library you wish to use is in'
    echo "  $AMPLLIBDIR"
    echo ' Is this OK (Y/n)?'
    while [[ $YESNO != 'Y' && $YESNO != 'N' ]]; do
        read YESNO
        if [[ $YESNO == "" ]]; then
      YESNO="Y"
    fi
        YESNO=`echo $YESNO | tr a-z A-Z`
    done
  else
     echo ' You plan to use the AMPL interface but the'
     echo ' AMPLDIR environment variable is not currently set'
     YESNO='N'
  fi

  CORRECT_AMPLLIBDIR="false"
  while [[ $CORRECT_AMPLLIBDIR == "false" ]]; do
     if [[ $YESNO == 'N' ]]; then
        echo ' Please give the name of the directory'
        echo ' containing the AMPL interface library:'
        read AMPLLIBDIR
     fi
#    if [[ -e $AMPLLIBDIR/amplsolver.a ]]; then
#    if [[ -e $AMPLLIBDIR/solvers/amplsolver.a ]]; then
     if [[ -e $AMPLLIBDIR/amplsolver.a || \
           -e $AMPLLIBDIR/solvers/amplsolver.a ]]; then
        CORRECT_AMPLLIBDIR="true"
     else
        echo " The directory $AMPLLIBDIR"
        echo " does not appear to contain a working AMPL interface library."
        YESNO='N'
        yesno_default_no ' Do you wish to abort'
        [[ $? != 0 ]] && exit 4
     fi
  done
else
  AMPLLIBDIR=
fi

#echo "ssids 2 ? " $SSIDS
