#!/bin/sh
if test $# -eq 0
then echo ""
echo "    /******************************************************************/"
echo "    /**                        F 9 0 L I N K                         **/"
echo "    /**                                                              **/"
echo "    /** F90LINK links Fortran 90 programs with the DISLIN library.   **/"
echo "    /**                                                              **/"
echo "    /** Command:    f90link    [option]   main                       **/"
echo "    /**                                                              **/"
echo "    /** option      is an optional  parameter  that can  have one of **/"
echo "    /**             the following values:                            **/"
echo "    /**        -c   for compiling programs before linking            **/"
echo "    /**        -r   for running programs after linking               **/"
echo "    /**        -a   for compiling, linking and running programs.     **/"
echo "    /**                                                              **/"
echo "    /** main        is the name of the main program.                 **/"
echo "    /**                                                              **/"
echo "    /** Example:    f90link  -a  test                                **/"
echo "    /** Version:    Fortran 90, Portland Group                       **/"
echo "    /******************************************************************/"
  exit 0
fi

if test ! $DISLIN; then
 DISLIN=/usr/local/dislin
fi
:
option=-X
if test $1 = -c ; then
  option=-C
elif test $1 = -C ; then
  option=-C
elif test $1 = -a ; then
  option=-A
elif test $1 = -A ; then
  option=-A
elif test $1 = -r ; then
  option=-R 
elif test $1 = -R ; then
  option=-R
fi

if test $option = -X ; then 
  name=$1
elif test $# -eq 1 ; then
  echo "<<<< File is missing!"
  exit 0
else
  name=$2
fi

if test $option = -X || test $option = -R ; then
  if test ! -f ${name}.o ; then
     echo " >>>> Cannot find file ${name}.o" 
     exit 0
  fi
  bname=`basename ${name}`
fi

if test $option = -C || test $option = -A ; then
  if test ! -f ${name}.f90 ; then
     echo " >>>> Cannot find file ${name}.f90" 
     exit 0
  fi
  bname=`basename ${name}`
fi

if test $option = -X ; then
  cmd="$2 $3 $4 $5 $6 $7 $8 $9 -L$DISLIN -ldislin"
else
  cmd="$3 $4 $5 $6 $7 $8 $9 -L$DISLIN -ldislin"
fi

: Linken und evtl. Compilieren
if test $option = -C ; then
  pgf90 -I$DISLIN/pgf  ${name}.f90  -o $bname $cmd 
elif test $option = -A ; then
  pgf90 -I$DISLIN/pgf  ${name}.f90  -o $bname $cmd 
else
  pgf90  ${name}.o  -o $bname $cmd 
fi

: Starten
if test $option = -A ; then
  $bname
elif test $option = -R ; then
  $bname
fi






