#!/bin/sh
DATE="10/08/1998"
MYCOMMAND=/usr/local/share/vecfem3/vempfade
#**************************************************************************
#**                                                                     ***
#**   Compilation of a FORTRAN program                                  ***
#**                                                                     ***
#**   call:                                                             ***
#**                                                                     ***
#**        vemfc [-c] <source> <target>                                 ***
#**                                                                     ***
#**  <source> is the name of the FORTRAN source code                    ***
#**           without extension f.                                      ***
#**                                                                     ***
#**  <target> name of the target object                                 ***
#**                                                                     ***
#**  -c       the program is not linked.                                ***
#**                                                                     ***
#**  used variables: VECFEM_ARCH VECFEM_ROOT                            ***
#**                                                                     ***
#**************************************************************************
#**                                                                     ***
#**        Copyrights University of Karlsruhe, 1996                     ***
#**        Program by L. Grosz                                          ***
#**                                                                     ***
#**************************************************************************
if [ $# -lt 3 ]
 then
    CF=0
    VEMSOURCE_FILE=$1
    VEMTARGET_FILE=$2
 else
    CF=1
    VEMSOURCE_FILE=$2
    VEMTARGET_FILE=$3
fi
#
#***************************************************************************
#
EXT=$$
ARCH=`echo $VECFEM_ARCH | sed 's/_DEMO//'`
#
#***************************************************************************
#
#     in some cases the file has to be prepared:
#
case $ARCH in
    SOLARIS | SunOS | AP1000)
          sed 's/IMPLICIT NONE//
               s/implicit none//' $VEMSOURCE_FILE.f  |
          sed  '/^[^Cc]/s/.*include/#include/
                /^[^Cc]/s/.*INCLUDE/#INCLUDE/' > prog$EXT.F;
          RMFLAG=1;
          F77CTARGET=prog$EXT.o;
          FILE=prog$EXT.F;;
    CRAYTS | CRAYJ90 | CRAYC90 | CRAYT3E | CRAYT3D | CRAYYMP | CRAYYMP_MPI) 
          sed 's/double precision/REAL\*8/g
               s/DOUBLE PRECISION/REAL\*8/g
               s/DBLE/FLOAT/g
               s/dble/FLOAT/g
               s/\.D0/\.E0/g
               s/\.D-/\.E-/g' $VEMSOURCE_FILE.f > prog$EXT.F;
          RMFLAG=1;
          F77CTARGET=prog$EXT.o;
          FILE=prog$EXT.F;;
    *) RMFLAG=0;
       F77CTARGET=$VEMSOURCE_FILE.o
       FILE=$VEMSOURCE_FILE.f
esac
#
#***************************************************************************
#
#     set the name of the compiler :
#
case $ARCH in

  FreeBSD) F77="f77 -O";;
  FreeBSD_MPI) F77="/usr/local/mpich/bin/mpif77";;
  LINUX) F77="g77 -O";;
  LINUX_PG) F77="pgf77 -O -Munroll -Mnoframe -tp px -Minfo -Mvect=assoc,cachesize:128000 -Mcache_align";;
  LINUX_PPG) F77="pgf90 -O -Munroll -Mnoframe -tp px -Minfo -Mvect=assoc,cachesize:128000 -Mcache_align";;
  LINUX_MPI) F77="mpif77 -O";;  
  DECALPHA) F77="f77 -O";;
  HP-UX)   F77="fort77";;
  AIX)     F77="xlf -O";;
  AIXSP)   F77="mpxlf -O3 -qarch=pwr2 -qtune=pwr2";;
  IRIX6N32) F77="f77 -n32";;
  IRIX6N64) F77="f77 -n64";;
  IRIX5)   F77="f77";;
  NECSX)   F77="f77";;
  Paragon) F77="if77 -nx -O3 -Mvect";;
  FSUN)  F77="frt";;
  VPP300)  F77="frt";;
  VPP300_MPI)  F77="frt -I /usr/lang/mpi/include -Wl,-P";;
  VPP300_PVM)  F77="frt -I /usr/lang/pvm/include -Wl,-P";;
  AP1000)  F77="f77.mpi -mpifast -cell -03 ";;
  CRAYTS ) export TARGET=cray-ts;
           F77="f90 -dp -O 1";;
  CRAYJ90 ) export TARGET=cray-j90;
           F77="cf77 -dp";;
  CRAYC90 ) export TARGET=cray-c90;
           F77="cf77 -dp";;
  CRAYT3E ) export TARGET=cray-ts;
           F77="f90 -dp";;
  CRAYT3D ) export TARGET=cray-t3d;
           F77="/mpp/bin/cf77 -dp";;
  CRAYYMP | CRAYYMP_MPI ) export TARGET=CRAY-YMP;
           F77="f90";;
  SOLARIS | SunOS) F77="f77 -O";;
  SOLARISF90) F77="f90 -O3";;
  FREEBSD) F77= "f77 -O";;


  *) echo "unknown architecture : $ARCH " >&2; exit 1;;
esac
#
#***************************************************************************
#
#     set the paths of inlcude files and libraries:
#
case $ARCH in
 SunOS | SOLARIS | IRIX6N32 | IRIX6N64 | AP1000 | DECALPHA | LINUX | LINUX_MPI | SOLARISF90 | FREEBSD )
      IFLAGS="-I"$VECFEM_ROOT"/include/vecfem3" ;
      LFLAGS="-L"$VECFEM_ROOT/lib ;;
 IRIX5 )
      IFLAGS="-Wf,-I"$VECFEM_ROOT"/include/vecfem3" ;
      LFLAGS="-L"$VECFEM_ROOT/lib ;;
 *)
      IFLAGS="-I "$VECFEM_ROOT"/include/vecfem3";
      LFLAGS="-L "$VECFEM_ROOT/lib;;
esac
#***************************************************************************
#
#     start compiler :
#
rm -f $VEMTARGET_FILE
if [ "$CF" = "1" ]; then 
  echo $F77 -c $IFLAGS $FILE
  $F77 -c $IFLAGS $FILE
  if [ $? -ne 0 ]; then exit 1; fi
  if [ "$F77CTARGET" != "$VEMTARGET_FILE" ]; then mv $F77CTARGET $VEMTARGET_FILE; fi
else
  LIBS="-lvecfem3"
  if test -r $VECFEM_ROOT/lib/liblsolpp.a; then
    LIBS=$LIBS" -llsolpp" 
  fi
#
# if pvm is used additional libraries are used:
#
  if [ "${PVM_ARCH:+yes}" = "yes" ]; then
     if test -r $PVM_ROOT"/lib/"$PVM_ARCH/libfpvm3,a; then
       LIBS=$LIBS" -lpvm3 -lfpvm3"
       LFLAGS=$LFLAGS" -L"$PVM_ROOT"/lib/"$PVM_ARCH
    fi
  fi
  if [ "$ARCH" = "LINUX_PPG" ]; then
     IFLAG=$IFLAG"-I"$PGI"/linux86/include"
     LIBS=$LIBS" -lfmpich -lmpich -llmpi"
     LFLAGS=$LFLAGS" -I"$PGI"/linux86/lib"
  fi
#
# if MPI is used on VPP additional libraries are used:
#
  if [ "$ARCH" = "VPP300_MPI" ]; then
     LIBS=$LIBS" -lmpi -lmp"
     LFLAGS=$LFLAGS" -L/usr/lang/mpi/lib"
  fi
  if [ "$ARCH" = "VPP300_PVM" ]; then
     LIBS=$LIBS" -lpvm -lmp"
     LFLAGS=$LFLAGS" -L/usr/lang/pvm/lib/VPP300"
  fi
#
  echo "include directory : "$IFLAGS
  echo "library directory : "$LFLAGS
  echo "libraries : "$LIBS

  $F77 $IFLAGS $FILE $LFLAGS $LIBS -o $VEMTARGET_FILE
  if [ $? -ne 0 ]; then exit 1; fi
fi
#***************************************************************************
if [ $RMFLAG -eq 1 ]; then rm -f prog$EXT.F; fi
