#!/bin/sh
#
# $Id: create_make_gcm 1461 2010-12-03 11:12:25Z emillour $
#
#set -xv
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    = $(GCM)/libo/$(MACHINE)'
   echo 'LIBO    = $(LIBOGCM)/$(MACHINE)'
else
   echo 'LIBO    = $(GCM)/libo'
fi
#echo 'LOCAL_DIR=$(GCM)'
#echo $localdir
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 '# Option de compilation C'
echo '#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
echo
echo 'COMPILEC = $(CCC) $(OPTIMC) $(INCLUDEC) -c'
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 : bibio $(DYN) 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_FILTRE) $(L_PHY) $(L_DYN) $(L_BIBIO) $(L_DYN) $(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 'chimtitan : $(LIBO)/libchimtitan.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`" != "" ] || [ "`ls $diri/*.F90`" != "" ]  ; then 
      cd $diri >/dev/null 2>&1
      echo
      listlib=""
      for i in `ls *.F`
      do
         fili=`basename $i .F`
         # Check if file is a routine or main program
         # i.e: look for the "program" keword preceeded by leading spaces
         test=` (  head $i | grep -i '^ *program' ) `
         if [ "$test" = "" ] ; then 
            # if it is not a main program, add it to the list
            listlib=$listlib" "$fili
         fi
      done
      for i in `ls *.F90`
      do
         fili=`basename $i .F90`
         # Check if file is a routine or main program
         # i.e. look for "program" keyword (with possibly some leading spaces)
         test=` (  head $i | grep -i '^ \{0,\}program' ) `
         if [ "$test" = "" ] ; then
            # if it is not a main program, add it to the list
            listlib=$listlib" "$fili
         fi 
      done
#
      echo
      echo
      echo "#======================================================================="
      echo "# Contenu de la bibliotheque correspondant au Directory "$diri
      echo "#======================================================================="
      echo
      for fili in $listlib
      do
         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 fili in $listlib
      do
         if [ -f $fili.F90 ] ; then
           trufile=$fili.F90
         else
           trufile=$fili.F
         fi
         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)/'$diri/$trufile
         [ "$fili" = "chem.subs" ] && str1=$str1' $(LIBF)/'$diri/chem.mods.F
         for stri in ` ( sed -n "/\#include/s/\#include//p" $trufile | sed 's/\"//g' ; egrep -i '^ *use ' $trufile | sed -e 's/,/ /' | awk ' { print $2 } ' ) `
         do


# Differents cas de dependance correspondant a des include ou des
# use module.
# soit dans le repertoire local soit dans un autre.

            stri=`echo $stri | tr [A-Z] [a-z]`
            if [ -f $stri ] ; then
               echo $str1 \\
               str1='$(LIBF)/'$diri'/'$stri
            else
               if [ -f $stri.F ] || [ -f $stri.F90 ] ; then
                  echo $str1 \\
                  str1='$(LIBO)/lib'$diri'.a('$stri'.o)'
               else
                  for dirinc in dyn3d grid bibio filtrez
                  do
                     if [ -f ../$dirinc/$stri ] ; then
                        echo $str1 \\
                        str1='$(LIBF)/'`cd .. ; ls */$stri | head -1`
                     fi
                     if [ -f ../$dirinc/$stri.F90 ] ; then
                        echo $str1 \\
                        str1='$(LIBO)/lib'$dirinc'.a('$stri'.o)'
                     fi
                  done
               fi
            fi
         done
         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)/'$diri'/'$trufile' ; \'
	 else
         ## Fortran 90
           if [ -f $fili.F90 ] ; then
	      ## Free Form
              echo '	$(COMPTRU90) $(LIBF)/'$diri'/'$trufile' ; \'
           else
	      echo '	$(COMPILE90) $(LIBF)/'$diri'/'$trufile' ; \'
           fi
	 fi
         # Put generated object in library
         echo '	$(AR) r $(LIBO)/lib'$diri'.a '$fili'.o ; $(RM) '$fili'.o ; \'
         echo '	cd $(GCM)'
	 echo
      done
#	 
      echo
      cd $libf
##############################
## CAS DES FICHIERS EN C
######
   elif [ "`ls $diri/*.c`" != "" ] ; then 
      cd $diri >/dev/null 2>&1
      echo
      listlib=""
      for i in `ls *.c`
      do
         fili=`basename $i .c`
         # Check if file is a function or main program
         # i.e. look for "int main" keywords (with possibly some leading spaces)
         test=` (  head $i | grep '^ \{0,\}int main' ) `
         if [ "$test" = "" ] ; then 
            # if it is not a main program, add it to the list
            listlib=$listlib" "$fili
         fi
      done
#
      echo
      echo
      echo '#======================================================================='
      echo '# Contenu de la bibliotheque correspondant au Directory '$diri
      echo '#======================================================================='
      echo
      for fili in $listlib
      do
         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 fili in $listlib
      do
         str1='$(LIBO)/lib'$diri'.a('$fili'.o) : $(LIBF)/'$diri/$fili.c
         for stri in ` ( sed -n "/\#include/s/\#include//p" $fili.c  | sed 's/\"//g' ) `
         do
            if [ -f $stri ] ; then
               echo $str1 \\
               str1='$(LIBF)/'$diri'/'$stri
            else
               for dirinc in dyn3d grid bibio
               do
                  if [ -f ../$dirinc/$stri ] ; then
                     echo $str1 \\
                     str1='$(LIBF)/'`cd .. ; ls */$stri | head -1`
                  fi
               done
            fi
         done
         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 ; \'
         ## C
         echo '	$(COMPILEC) $(LIBF)/'$diri'/'$fili'.c; \'
         # Put generated object in library
         echo '	$(AR) r $(LIBO)/lib'$diri'.a '$fili'.o ; $(RM) '$fili'.o ; \'
         echo '	cd $(GCM)'
	 echo
      done
#	 
      echo
      cd $libf
##############################
   fi
   fi
done
