#!/bin/csh 
#  Script de generation des Makefile/smakefile pour modules
#  librairies SOPHYA  
#  C. Magneville / R. Ansari - Mai 2005
#------
#  Option controlant la generation des smakefile OU Makefile
#  smakefile utilise pour le developpement - contient une reference 
#  a $(SOPHYABASE)/include/sophyamake.inc
#  les Makefile reference ../BuildMgr/sophyamake.inc
#------

if ( ! $?TMPDIR ) set TMPDIR = /tmp
set makname = "Makefile"
unset targetdir 

if ( $#argv < 1) then 
  echo ' Usage: mkmflib [-dev] [-sbase SOPHYABASE] ModuleName '
  exit -1
endif

while ( $#argv > 0 )
  if( "$1" == "-h" ) then
    echo ' Usage: mkmflib [-dev] [-sbase SOPHYABASE] ModuleName '
    exit -1
  else if ( "$1" == "-dev" ) then 
   set makname = "smakefile"
  else if( "$1" == "-sbase" ) then
    setenv SOPHYABASE $2
    shift
  else 
    set targetdir = $1
  endif
  shift
end

if( ! $?SOPHYABASE ) then
  echo 'ERROR: (mkmflib) define SOPHYABASE or use script arguments'
  exit -2
endif

if( ! $?targetdir ) then
  echo 'ERROR: (mkmflib) No target module (directory) specified'
  exit -3
endif

cd ../$targetdir

#--- fichier des fichiers a exclure
if ( ! -f exclude ) touch exclude
sort exclude >! $TMPDIR/exclude.sort
set exclfile = 'exclude'

echo "mkmflib: Creating $targetdir/objlist.list , $targetdir/$makname" 

#--- fichier de la liste des .o
set liblist = objlist.list
rm -f $liblist
touch $liblist
ls -1 *.{c,cc} | comm -3 -1 $TMPDIR/exclude.sort - | sed -e 's/\.cc/.o/' | sed -e 's/\.c/.o/' >> $liblist
unset liblist

#--- Creation de smakefile
set libf='$(SOPHYALIBP)lib'$targetdir'.a'
rm -f $makname; touch $makname

if ($makname == "smakefile") then 
  echo 'include $(SOPHYABASE)/include/sophyamake.inc' >> $makname 
else 
  echo 'include ../BuildMgr/sophyamake.inc' >> $makname 
endif

echo ' ' >> $makname
echo 'all:' $libf  >> $makname 
echo ' ' >> $makname

echo 'clean:' >> $makname
set toto='rm -f'
foreach f ( *.cc *.c ) 
  set toto= ( $toto '$(SOPHYAOBJP)'$f:r.o )
end
echo '	' $toto >> $makname
echo '	rm -f '$libf >> $makname
echo ' ' >> $makname

echo $libf ':' \
 `ls -1 *.{c,cc} | comm -3 -1 $TMPDIR/exclude.sort - | sed -e 's/\.cc/.o/' | sed -e 's/\.c/.o/' | sed -e 's/^/$(SOPHYAOBJP)/'` >> $makname
echo '	$(AR) $(ARFLAGS) $@ $? ' >> $makname
rm -f $TMPDIR/exclude.sort

echo " " >> $makname


#--- Creation des dependances
set inc = `echo $SOPHYABASE/include | sed "s?//?/?g" | sed 's?/$??'`
set nomsys = `uname`

foreach f ( *.cc )
  grep -q '^'$f'$' $exclfile && continue
  gcc -MM -D$nomsys -I$inc $f  \
       | sed -e 's/.*\.o/\$(SOPHYAOBJP)&/'  \
       | sed -e 's?'$inc/'?$(SOPHYAINCP)?g'  >> $makname
  echo '	$(CXXCOMPILELIB) -o $@ $<' >> $makname
  echo ' ' >> $makname
end

foreach f ( *.c )
  grep -q '^'$f'$' $exclfile && continue
  gcc -MM -D$nomsys -I$inc $f  \
       | sed -e 's/.*\.o/\$(SOPHYAOBJP)&/'  \
       | sed -e 's?'$inc/'?$(SOPHYAINCP)?g' >> $makname
  echo '	$(CCOMPILE) -o $@ $<' >> $makname
  echo ' ' >> $makname
end 

foreach f ( *.f )
  grep -q '^'$f'$' $exclfile && continue
  echo '$(SOPHYAOBJP)'$f:r'.o: '$f >> $makname
  echo '	$(FCOMPILE) -o $@ $<' >> $makname
  echo ' ' >> $makname
end 

exit 0
