#! /bin/sh

set -e

umask 0

#  make sure all directories exists.
for subdirs in gribex gribtables pbio bufrdc bufrtables crexdc crextables interpolation land_sea_mask examples config options
do
   if test ! -d $subdirs ; then
       echo
       echo "Tarfile is incomplete ;  $subdirs does not exist."
       echo "         Incorrect tarfile downloaded?"
       quit
   fi
done


# is the uname available ?

type uname > /dev/null 2>&1
if test $? -ne 0 ; then
    echo "install: uname not found"
    echo "You should try compilation without install script"
    quit
fi
target=`uname -s | tr '[A-Z]' '[a-z]'`

gnu=

echo

case $target in 

linux )
    target=linux
    gnu=yes
	platform=`uname -m`
	case $platform in
	x86_64 )
	    A64=A64
        CNAME=_x86_64
		;;
	ia64 )
        gnu=
	    A64=
		target=itanium
		;;
    * )
	    A64=
		;;
    esac
	    echo "Default compilers are Portland: pgf77 and pgcc."
    ;;
darwin* )
    target=darwin
    platform=`machine`
    case $platform in
    ppc970 )
        target=ppc
        A64=_G5
        ;;
    ppc* )
        target=ppc
        A64=
        ;;
    i486 )
        target=darwin
        A64=_i486
        ;;
    *64 )
        target=darwin
        A64=A64
        ;;
    * )
        target=darwin
        A64=
        ;;
    esac

       echo "Default compilers are : gfortran and gcc."
    ;;
sunos )
    target=sun4
#	platform=`uname -m`
#    if test "$platform" = "i86pc" ; then
#      target=i86pc
#    fi

    ;;
irix* )
    target=sgimips
    ;;
aix )
    target=rs6000
    ;;
osf1 )
    target=decalpha
    ;;
hp-ux )
    target=hppa
    platform=`uname -m`
    if test "$platform" = "ia64" ; then
      target=hpia64
    fi
    ;;
fujitsu* )
    target=FUJITSU
    ;;

 * )
        echo
        echo "install: products can only be installed on Linux, Solaris, Sgi"
        echo "Alpha, HP, IBM operating systems."
        echo "You should try compilation without install script"
        quit
	;;
esac

CNAME=
if test "$gnu" = "yes" ; then
       echo "Do you want to work with GNU gfortran/gcc (answer y), ifort (answer i) ? [n]"
       read ans
       echo
       if test -n "$ans" ; then
          if test "$ans" = "y" -o "$ans" = "yes" ; then
              CNAME=_gfortran
          fi
          if test "$ans" = "i" -o "$ans" = "intel" ; then
              CNAME=_intel
          fi
       fi
fi

if test "$target" = "rs6000" ; then
       echo "Do you want to force 64 bit addressing mode? (answer y) [n]"
       read ans
       echo
       if test -n "$ans" ; then
          if test "$ans" = "y" -o "$ans" = "yes" ; then
              A64=A64
          fi
       fi
fi


R64=
     echo "Default is  32 bit reals."
     echo "Do you want 64 bit reals ? [y,n]"
     read ans
     echo
if test -n "$ans" ; then
  if test "$ans" = "y" -o "$ans" = "yes" ; then
       R64=R64
  fi
fi

GRIB_API=
     echo "Do you want to build with grib_api ? [y,n]"
     read ans
     echo
if test -n "$ans" ; then
  if test "$ans" = "y" -o "$ans" = "yes" ; then
       GRIB_API=grib_api_merging
    echo "   Can you specify the directory path for GRIB_API ?"
	read ans
    GRIB_API_DIR=$ans
JASPER_DIR=
     echo "Do you need jpeg support for grib_api ? [y,n]"
     read ans
     echo
if test -n "$ans" ; then
  if test "$ans" = "y" -o "$ans" = "yes" ; then
    echo "   Can you specify the directory path for JASPER ?"
	read ans
    JASPER_DIR="$ans/libjasper.a"
  fi
fi
fi
fi

echo $R64 > .r64

cat Makefile.in | sed s:reals:$R64: | sed s:glue:$GRIB_API: > Makefile

for subdirs in gribex pbio bufrdc bufrtables crexdc interpolation fft
do
cat $subdirs/Makefile.in | sed s:reals:$R64: | sed s:arch:$target: | sed s:plat:$A64: | sed s:depl:emos: | sed s:comp:$CNAME: > $subdirs/Makefile
done

for subdirs in examples/gribex examples/bufr examples/crex examples/interpolation examples/fft
do
cat $subdirs/Makefile.in | sed s:reals:$R64: | sed s:arch:$target: | sed s:plat:$A64: | sed s:comp:$CNAME: > $subdirs/Makefile
done

#grib_api
cat grib_api_merging/Makefile.in | sed s:reals:$R64: | sed s:arch:$target: | sed s:plat:$A64: | sed s:comp:$CNAME: | sed s:depl:emos: | sed s:glue:$GRIB_API_DIR: > grib_api_merging/Makefile
cat examples/interpolation_grib_api/Makefile.in | sed s:reals:$R64: | sed s:arch:$target: | sed s:plat:$A64: | sed s:comp:$CNAME: | sed s:glue:$GRIB_API_DIR: | sed s:jasp:$JASPER_DIR: > examples/interpolation_grib_api/Makefile


INSTALL_DIR=/usr/local/lib

echo
echo "   The default directory for gribtables, bufrtables, crextables,"
echo "land_sea_mask, gribtemplates and libemos$R64.a will be $INSTALL_DIR,"
echo " but you may put them anywhere you wish assuming"
echo "you have permission to do so."
echo
echo "   You can specify the directory path for GRIB, BUFR, CREX tables"
echo "local definition templates, land-sea mask, and libemos$R64.a library"
echo "or just press RETURN if you want default directory."
echo
echo "Tables, templates, land-sea mask and libemos$R64.a directory [$INSTALL_DIR]: "
read ans

if test -n "$ans" ; then
    INSTALL_DIR=$ans
fi

echo $INSTALL_DIR > .emos

cat config/config.$target$CNAME$R64$A64.in | sed s:emos:$INSTALL_DIR: > config/config.$target$CNAME$R64$A64

make

chmod a-xw libemos$R64.a

ls bufrtables/*000* > .list/bufrtables

echo "*******************************************************************"
echo
echo "Compilation is completed !"
echo
echo "*******************************************************************"
echo
echo "  You can put other options for compilation in"
echo "config/config.$target$R64 like level of optimisation etc."
echo "and make compilation again. "
echo
echo "When you finish your changes run"
echo
echo "make clean"
echo
echo "make"
echo
echo "in order to produce library "
echo
echo "*******************************************************************"
echo
echo "   Now, if you want to install libemos$R64.a and gribtables, bufrtables, crextables,"
echo "land_sea_mask it is recomended to do that runing ./install script."
echo "You must have root permission runinig ./install , if you want"
echo "to put them on /usr/local/lib "
echo "Also you can choose your directories."
echo
echo "*******************************************************************"
echo
echo "Any comments on the document or the software would be appreciated"
echo
echo "e-mail software.services@ecmwf.int"
echo
echo "*******************************************************************"

exit 0


