#!/bin/bash 
function clrd(){
    local color=$1;
    local backgnd=$2;
    case $color in
        0) tput setaf 0 ;; # black
        1) tput setaf 1 ;; # red
        2) tput setaf 2 ;; # green
        3) tput setaf 3 ;; # yellow
        4) tput setaf 4 ;; # blue
        5) tput setaf 5 ;; # magenta
        6) tput setaf 6 ;; # cyan
        7) tput setaf 7 ;; # white
        *) tput sgr0;
    esac
    case $backgnd in
        0) tput setb 0 ;; # black
        1) tput setb 1 ;; # red
        2) tput setb 2 ;; # green
        3) tput setb 3 ;; # yellow
        4) tput setb 4 ;; # blue
        5) tput setb 5 ;; # magenta
        6) tput setb 6 ;; # cyan
        7) tput setb 7 ;; # white
    esac
}
#
#
tput clear
divider=================================
divider=$divider$divider$divider$divider$divider$divider$divider
width=`tput cols`

if (($#<1)) ; then
###############################
text="DEFINE FORTRAN COMPILER"
###############################
lng=`expr length "${text}"`
printf "%$width.${width}s\n\v" "$divider"
printf "%`expr \( $width / 2 \) + \( $lng / 2 \)`s\n\v" "${text}"
printf "%$width.${width}s\n" "$divider"

ltest="true"
pgf90  2> /dev/null
if [ $? -eq 0 ] ; then
  clr1=2
  msg1=""
else
  clr1=1
  msg1="Not Available"
fi
ftn  2> /dev/null
if [ $? -eq 1 ] ; then
  clr2=2
  msg2=""
else
  msg2="Not Available"
  clr2=1
fi
nvfortran 2> /dev/null
if [ $? -eq 0 ] ; then
  clr3=2
  msg3=""
else
  msg3="Not Available"
  clr3=1
fi
gfortran 2> /dev/null
if [ $? -eq 1 ] ; then
  clr4=2
  msg4=""
else
  msg4="Not Available"
  clr4=1
fi

while [ ${ltest} = "true" ] ;do
printf "%s\n" "Fortran Compiler"
printf "%s\n" "$(clrd ${clr1})PGI      : 1 ${msg1}$(clrd)"
printf "%s\n" "$(clrd ${clr2})CRAY     : 2 ${msg2}$(clrd)"
printf "%s\n" "$(clrd ${clr3})NVIDIA   : 3 ${msg3}$(clrd)"
printf "%s\n" "$(clrd ${clr4})GFORTRAN : 4 ${msg4}$(clrd)"
printf "%s" "Choose [$(clrd ${clr1})1$(clrd)/$(clrd ${clr2})2$(clrd)/$(clrd ${clr3})3$(clrd)/$(clrd ${clr4})4$(clrd)]: "
read ans
if test -n "${ans}" ; then
  case $ans in
  1 )
     export  FCMP="PGI"
     ltest="false"
    ;;
  2 )
     export  FCMP="CRAY"
     ltest="false"
    ;;
  3 )
     export  FCMP="NVIDIA"
     ltest="false"
    ;;
  4 )
     export  FCMP="GNU"
     ltest="false"
    ;;
   *   )
    echo
    echo "ERROR: '${ans}': unknown input.  "
    #quit
    ;;
  esac
fi
done
#
if [ -s ./configure/make.inc_${FCMP} ] ; then
   cp ./configure/make.inc_${FCMP} ./configure/make.inc

   cd ./dummyMPI
    make -f Makefile_${FCMP} clean
    make -f Makefile_${FCMP} 
    cd ../w3lib-1.9.source
    make -f Makefile_${FCMP} clean
    make -f Makefile_${FCMP} 
    #
    cd ../iplib.source.le
    make -f Makefile_${FCMP} clean
    make -f Makefile_${FCMP} 
    #
    cd ../ei_bufrlib.source
    make -f Makefile_${FCMP} clean
    make -f Makefile_${FCMP} 
    #
    exit 0
else
  ###########################################################
  text="make.inc_${FCMP} not found in configure directory!"
  ###########################################################
  lng=`expr length "${text}"`
  printf "%$width.${width}s\n\v" "$divider"
  printf "%`expr \( $width / 2 \) + \( $lng / 2 \)`s\n\v" "${text}"
  printf "%$width.${width}s\n" "$divider"
  exit 1
fi
fi
