#!/bin/csh 
#  Script de generation des Makefile/smakefile pour modules
#  librairies SOPHYA  
#  C. Magneville / R. Ansari - Mai 2005

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

#  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

set makname = "Makefile"
unset targetdir 

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

if ( ! -f exclude ) touch exclude
if ( ! -f flags_compil ) touch flags_compil
if ( ! -f extlib_list ) touch extlib_list
if ( ! -f extslb_list ) touch extslb_list

if ( ! $?TMPDIR ) set TMPDIR = /tmp
if (-f exclude_dpc ) then 
#  CHECK !!! qque chose a faire ...
#  On utilise alors le fichier exclude_dpc
  echo ' DBG - utilisation du fichier exclude_dpc pour ' $targetdir
  sort exclude_dpc >! $TMPDIR/exclude.sort
  set exclfile = 'exclude_dpc'
else
  sort exclude >! $TMPDIR/exclude.sort
  set exclfile = 'exclude'
endif

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

set inc = `echo $SOPHYABASE/include | sed "s?//?/?g"`
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 '	$(CXXCOMPILE) -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 

exit 0
