#!/bin/csh
#  Script de generation des Makefile/smakefile pour modules
#  programmes SOPHYA  
#  C. Magneville / R. Ansari - Mai 2005
#--- Cf commentaires dans mkmflib

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

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

while ( $#argv > 0 )
  if( "$1" == "-h" ) then
    echo ' Usage: mkmfprog [-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: (mkmfprog) define SOPHYABASE or use script arguments'
  exit -2
endif

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

cd ../$targetdir

if ( ! -f programs ) touch programs
if ( ! -f usedslibs ) touch usedslibs

echo "mkmfprog: Creating $targetdir/$makname" 

#--- La liste des programmes a compiler et de tous les .o
set prog = ( `cat programs | awk '{print $1}'` )
set obj = 
foreach f ( $prog )
  set obj = ( `grep ^$f programs | awk -F"$f " '{print $2}'` $obj )
end

#--- Creation de smakefile
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

set dum = 'PROGS = '
foreach f ( $prog )
  set dum = ( $dum  '$(SOPHYAEXEP)'$f )
end
echo $dum >> $makname
echo ' ' >> $makname

echo 'all: $(PROGS)' >> $makname
echo ' ' >> $makname

set dum = 
foreach f ( $obj )
  set dum = ( $dum '$(SOPHYAOBJP)'$f )
end
echo 'clean:' >> $makname
echo '	rm -f $(PROGS)' >> $makname
echo '	rm -f '$dum >> $makname
echo ' ' >> $makname

#--- Les programmes
unset sopok extok piok
grep -q ^sophya usedslibs
if( $status == 0 ) set sopok
grep -q ^extsophya usedslibs
if( $status == 0 ) set extok
grep -q ^PI usedslibs
if( $status == 0 ) set piok
set lib = '$(SOPHYAALLSLBLIST)'
if( $?sopok ) set lib = '$(SOPHYASLBLIST)'
if( $?extok ) set lib = '$(SOPHYAEXTSLBLIST)'
if( $?piok )  set lib = '$(SOPHYAALLSLBLIST)'

foreach f ( $prog )
  echo $f': $(SOPHYAEXEP)'$f >> $makname
  echo '	echo $@ " done"' >> $makname
  set oo = ( `grep ^$f programs | awk -F"$f " '{print $2}'` )
  set dum = '$(SOPHYAEXEP)'$f':'
  foreach fo ( $oo )
    set dum = " $dum "'$(SOPHYAOBJP)'$fo
  end
  echo $dum  >> $makname
  echo '	$(CXXLINK) -o $@ $? '$lib >> $makname
  echo ' ' >> $makname
end

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

foreach f ( *.cc )
  grep -q "$f:r\.o" programs
  if( $status != 0 ) 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:r\.o" programs
  if( $status != 0 ) 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:r\.o" programs
  if( $status != 0 ) continue
  echo '$(SOPHYAOBJP)'$f:r'.o: '$f >> $makname
  echo '	$(FCOMPILE) -o $@ $<' >> $makname
  echo ' ' >> $makname
end 

exit 0
