#!/bin/sh
#
# $Id: create_make_gcm 1893 2013-10-16 12:36:06Z fhourdin $
#
#set -xv

rm -f create_make_gcm.log

machine=`hostname`
os=`uname`
gcm=`pwd`
libf=$gcm/libf
libo=$gcm/libo
CRAY=0
if [ "$machine" = "atlas" -o "$machine" = "etoile" -o "$machine" = "axis" ] ; then
  CRAY=1
fi
XNEC=0
if [ "$machine" = "rhodes" ] ; then
  XNEC=1
fi
X6NEC=0
if [ "$machine" = "mercure" ] ; then
  X6NEC=1
fi
X8BRODIE=0
if [ "$machine" = "brodie" ] ; then
  X8BRODIE=1
fi
VPP=0
if [ "$machine" = "nymphea0" ] ; then
  VPP=1
fi
#
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "# Definitions de Macros pour Make"
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo
echo "# Repertoires :"
echo
echo "GCM     = "$gcm
echo 'LIBF    = $(GCM)/libf'
if [ "$CRAY" = '0' ] ; then
   echo 'LIBO    = $(LIBOGCM)/$(MACHINE)'
else
   echo 'LIBO    = $(GCM)/libo'
fi
echo "LOCAL_DIR=`echo $localdir`"
echo 'BIBIO    = $(LIBF)/bibio'
echo "FILTRE   = filtre"
echo "PHYS  = "
echo "DYN  = dyn "
echo 'LIBPHY = $(LIBO)/libphy$(PHYS).a'
echo 'DIRMAIN=dyn$(DIM)d$(FLAG_PARA)'
echo 'RM=rm'
echo
echo "OPLINK = "
echo
echo '# Les differentes librairies pour l"edition des liens:'
echo
echo 'dyn3d            = $(LIBO)/libdyn3d.a $(LIBO)/lib$(FILTRE).a'
echo 'dyn3dpar      = $(LIBO)/libdyn3dpar.a $(LIBO)/lib$(FILTRE).a'
echo 'dyn2d            = $(LIBO)/libdyn2d.a'
echo 'dyn1d            = $(LIBO)/libdyn1d.a'
echo 'L_DYN      = -ldyn$(DIM)d$(FLAG_PARA)'
echo 'L_FILTRE   = -l$(FILTRE)'
echo 'L_PHY = -lphy$(PHYS) '
echo 'L_BIBIO    = -lbibio'
echo 'L_ADJNT    ='
echo 'L_COSP     = -lcosp'

echo
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "# Option de compilation FORTRAN"
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo
   echo 'COMPILE = $(F77) $(OPTIM) $(INCLUDE) -c'
   echo 'COMPILE90 = $(F90) $(OPTIM90) $(INCLUDE) -c'
   echo 'COMPTRU90 = $(F90) $(OPTIMTRU90) $(INCLUDE) -c'
   echo "LINK    = $LINK"
   echo "AR      = $AR"
echo
echo
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "# Creation des differents executables"
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo
echo "# Executables:"
echo "# ------------"
echo
echo "PROG = code"
echo
#echo 'main : chimie $(DYN) bibio phys $(OPTION_DEP) '
echo 'main : $(DYN) bibio phys $(OPTION_DEP) '
echo '	cd $(LIBO) ; $(RANLIB) lib*.a ; cd $(GCM) ;\'
echo '	cd $(LOCAL_DIR); \'
echo '	$(COMPILE90) $(LIBF)/$(DIRMAIN)/$(SOURCE) -o $(PROG).o ; \'
echo '	$(LINK) $(PROG).o -L$(LIBO) $(L_DYN) $(L_ADJNT) $(L_COSP) $(L_PHY) $(L_DYN) $(L_BIBIO) $(L_DYN) $(L_FILTRE) $(OPLINK) $(OPTION_LINK) -o $(LOCAL_DIR)/$(PROG).e ; $(RM) $(PROG).o '
echo
echo 'dyn : $(LIBO)/libdyn$(DIM)d$(FLAG_PARA).a $(FILTRE)$(DIM)d'
echo
echo 'phys : $(LIBPHY)'
echo
#echo 'chimie : $(LIBO)/libchimie.a'
echo
echo 'bibio : $(LIBO)/libbibio.a'
echo
echo 'adjnt : $(LIBO)/libadjnt.a'
echo
echo 'cosp : $(LIBO)/libcosp.a'
echo
echo '$(FILTRE)3d : $(LIBO)/lib$(FILTRE).a'
echo
echo '$(FILTRE)2d :'
echo
echo '$(FILTRE)1d :'
echo
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "# Contenu des differentes bibliotheques"
echo "#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo
echo
cd $libf >/dev/null 2>&1

for diri in ` ls ` ; do

   if [ -d $diri ] ; then
   if [ "`ls $diri/*.F $diri/*/*.F`" != "" ] || [ "`ls $diri/*.F90 $diri/*/*.F90`" != "" ]  ; then 
#      cd $diri >/dev/null 2>&1
      echo
      listlib=""
# Liste des fichiers .F et .F90 n'etant pas des programmes principaux
      for fili in `ls $diri/*.F $diri/*/*.F` ; do
         test=` (  head $fili | grep '      PROGRAM' ) `
         if [ "$test" = "" ] ; then listlib=$listlib" "$fili ; fi
      done
      for fili in `ls $diri/*.F90 $diri/*/*.F90` ; do
         test=` (  head $fili | grep 'PROGRAM' ) `
         if [ "$test" = "" ] ; then listlib=$listlib" "$fili ; fi 
      done
      echo
      echo $listlib >> create_makeg_gcm.log
      echo
      echo "#=================================================================="
      echo "# Contenu de la bibliotheque correspondant au Directory "$diri
      echo "#=================================================================="
      echo
      for trufile in $listlib ; do
         fili=`echo $trufile | awk -F/ ' { print $NF } ' | cut -d. -f1`
         echo '$(LIBO)/lib'$diri".a : " '$(LIBO)/lib'$diri".a("$fili".o)"
         echo
      done
      echo '.PRECIOUS	: $(LIBO)/lib'$diri'.a'
      echo
      echo
      echo "# Compilation des membres de la bibliotheque lib"$diri".a"
      echo
      for trufile in $listlib ; do
         fili=`echo $trufile | awk -F/ ' { print $NF } ' | cut -d. -f1`
         F90=0 ; egrep -i '^ *use ' $trufile > /dev/null 2>&1 && F90=1
                 egrep -i '^ *module ' $trufile > /dev/null 2>&1 && F90=1
                 egrep -i '#include*.inc ' $trufile > /dev/null 2>&1 && F90=1
         str1='$(LIBO)/lib'$diri'.a('$fili'.o) : $(LIBF)/'$trufile
         [ "$fili" = "$diri/chem.subs" ] && str1=$str1' $(LIBF)/'$diri/chem.mods.F

##########################################################################
# TRAITEMENT DES DEPENDANCES
# Differents cas de dependance correspondant a des include ou des
# use module.
# soit dans le repertoire local soit dans un autre.
# Pour dyn3d, il faudrait remplacer la chaine de caractere par $DYN
# ou quelque chose du genre.
##########################################################################

         for stri in ` ( sed -e '/^[cC\!]/d' $trufile | sed -n "/\#include/s/\#include//p" | sed 's/\"//g' ; sed -e '/^[cC\!]/d' $trufile | egrep -i '^ *use ' | sed -e 's/,/ /' | awk ' { print $2 } ' ) ` ; do
            echo $trufile $fili $stri >> create_make_gcm.log
            strj=`echo $stri | tr [A-Z] [a-z]`
            str2=""
            for dirinc in filtrez bibio grid dyn3d phydev $diri $diri/*/ ; do
# Recherche dans l'ordre hierarchique inverse car seule la derniere
# ligne est conservee
               if [ $dirinc = phydev ] ; then
                   dirstr='$(PHYS)'
                   libstr='phy$(PHYS)'
               else
                   dirstr=$dirinc
                   libstr=$dirinc
               fi
               if [ -f $dirinc/$stri ] ; then
                  str2='$(LIBF)/'$dirstr/$stri
               elif [ -f $dirinc/$strj ] ; then
                  str2='$(LIBF)/'$dirstr/$stri
               elif [ -f $dirinc/$strj.F90 ]  || [ -f $dirinc/$strj.F ]  ; then 
                  strlib=`echo $libstr | awk -F/ ' { print $1 } '`
                  str2='$(LIBO)/lib'$strlib'.a('$strj'.o)'
               elif [ -f $dirinc/$stri.F90 ]  || [ -f $dirinc/$stri.F ]  ; then 
                  strlib=`echo $libstr | awk -F/ ' { print $1 } '`
                  str2='$(LIBO)/lib'$strlib'.a('$stri'.o)'
               fi
            done
            if [ "$str2" = "" ] ; then
               echo Warn: Include $diri $trufile $fili $stri pas trouve >> $gcm/create_make_gcm.log
            elif [ "$str2" = "$str1" ] ; then
               echo Warn: Include $diri $trufile $fili $stri $str1 trouve 2x >> $gcm/create_make_gcm.log
            else
               echo $str1 \\
               str1=$str2
            fi
         done # Fin de la boucle sur les dependances et ecriture de la derniere
         echo $str1
         # Compile in LIBO directory; and before compiling, remove
         # object from library
         echo '	cd $(LIBO); \'
         echo '	$(AR) d $(LIBO)/lib'$diri'.a '$fili'.o ; \'
	 if [ "$F90" -eq '0' ] ; then
         ## Fixed Form Fortran 77
	   echo '	$(COMPILE) $(LIBF)/'$trufile' ; \'
	 else
         ## Fortran 90
           if [ `echo $trufile | cut -d. -f2` = F90 ] ; then
	      ## Free Form
              echo '	$(COMPTRU90) $(LIBF)/'$trufile' ; \'
           else
	      echo '	$(COMPILE90) $(LIBF)/'$trufile' ; \'
           fi
	 fi
         # Put generated object in library
         echo '	$(AR) r $(LIBO)/lib'$diri'.a '$fili'.o ; $(RM) '$fili'.o ; \'
	 echo
      done
#	 
      echo
   cd $libf
   fi
   fi
done
