#!/bin/sh

########################################################################
#
# Auteur: Frederic Hourdin        Septembre 1991
#
# Objet : creation automatique d'un makefile
#
# Ce shell-script genere un makefile a partir de l'analyse des fichiers
# *.f contenus dans le directory courant.
#
# !!! ATTENTION :
# les programmes principaux doivent contenir l'instruction
# FORTRAN: program
#
########################################################################

dirbase=`pwd`
nomlib=`basename $dirbase`
#libf=$dirbase
#libo=$dirbase
libf=.
libo=.
#
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "# Definitions de Macros pour Make"
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo
echo "# Repertoires :"
echo
echo "DIRBASE     = "$dirbase
echo 'LIBF    = '$libf
echo 'LIBO    = '$libo
echo "NOM_LIB = "$nomlib
echo
echo
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "# Option de compilation FORTRAN"
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo
echo "OPTIM   = -O3 "
echo 'COMPILE = gfortran $(OPTIM) -c'
echo 'LINK    = gfortran $(OPTIM) '
echo "AR      = ar"
echo "OPTION_LINK = -Wl,-rpath=/usr/lib:/home/util1/NETCDF/netcdf-4.0.1/lib -L/home/util1/NETCDF/netcdf-4.0.1/lib -lnetcdf -lnetcdff "
echo 'LINKS   = -Wl,-rpath=$(LIBO) -L$(LIBO) -l$(NOM_LIB) $(OPTION_LINK) '
echo
echo
echo
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "# Creation des executables "
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo
cd $libf
listlib=""
for i in `ls *.F90` ; do
   fili=`basename $i .F90`
#
# selection des programmes principaux
#
   if [ "` (  head $i | grep -i 'program' ) `" != "" ] ; then 
#
# creation des executables
#
      echo $fili' : $(LIBF)/'$fili'.e'
      echo '	echo Compilation de '$fili' OK'
      echo
      str1='$(LIBF)/'$fili'.e : $(LIBO)/lib$(NOM_LIB).a $(LIBF)/'$fili'.F90'

# construction de la liste des dependances correspondant aux INCLUDE
      for stri in ` grep -i '      include' $fili.F90 | cut -d"'" -f2 `
      do
         echo $str1 \\
         if [ "` echo $stri | cut -c1 `" = "/" ] ; then
	    str1=$stri
         else
            str1='$(LIBF)/'$stri
         fi
      done
      echo $str1

# commandes de compilation et edition de liens
      echo '	cd $(LIBF) ; $(COMPILE) '$fili'.F90 ; cd $(DIRBASE) ;\'
      echo '	$(LINK) $(LIBF)/'$fili'.o -o '$fili'.e $(LINKS) ;\'
      echo '	rm $(LIBF)/'$fili'.o'
      echo

# listlib est la liste des SUBROUTINE, FUNCTION, BLOCK DATA ...
#
   else
      listlib=$listlib" "$fili
   fi
#
done
#
echo
echo
echo
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "# Creation de la librairie de procedures"
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo

# contenu de la bibliotheque:
echo
echo "# Contenu de la bibliotheque correspondant "
echo
str1='$(LIBO)/lib$(NOM_LIB).a : '
for fili in $listlib
do
   echo $str1 \\
   str1='$(LIBO)/lib$(NOM_LIB).a('$fili'.o)'
done
echo $str1
echo
echo

echo '# Compilation des membres de la bibliotheque lib$(NOM_LIB).a'
echo
for fili in $listlib
do
   str1='$(LIBO)/lib$(NOM_LIB).a('$fili'.o) : $(LIBF)/'$fili.F90
   for stri in ` grep -i '      include' $fili.F90 | cut -d"'" -f2 `
   do
      echo $str1 \\
      if [ "` echo $stri | cut -c1 `" = "/" ] ; then
	 str1=$stri
      else
         str1='$(LIBF)/'$stri
      fi
   done
   echo $str1
   echo '	$(COMPILE) $(LIBF)/'$fili'.F90 ; \'
   echo '	$(AR) Ur $(LIBO)/lib$(NOM_LIB).a '$fili'.o ; rm '$fili'.o ; \'
   echo '	cd $(DIRBASE)'
echo
done
