#!/bin/csh

# ################## ArchTOIPipe-With-Sophya #################### 
# ##### LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA #####
#  Script for ArchTOIPipe library module makefile generation
#  Usage: ./mkmflib lib_module_name
# ###############################################################  

cd $1

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
sort exclude >! $TMPDIR/exclude.sort

##############################################
########### fichier de la liste des .o #######
##############################################

set liblist = $1.o.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 GNUmakefile ##########
##############################################

set libf='$(MYLIB)lib'$1'.a'

#set hfcxxlibs = '-L$(LIB)' 
#set i = 1
#while ($i <= $#argv)
#  set hfcxxlibs = "$hfcxxlibs -l$argv[$i]"
#  @ i++
#end

#set extlib_list = `cat extlib_list`
#set hfcxxlibs = ($hfcxxlibs '-L$(EXTLIBPATH)' $extlib_list)

rm -f GNUmakefile
touch GNUmakefile

cat flags_compil >> GNUmakefile 
echo 'include ../SMakefile.h' >> GNUmakefile 

echo " " >> GNUmakefile
echo 'all:' $libf  >> GNUmakefile 

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

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/^/$(OBJ)/'` >> GNUmakefile
echo '	$(ARCXX) $(ARCXXFLAGS) $@ $($(ARARGS))' >> GNUmakefile
#  Pour faire compiler les instantiations automatique de cxx (DEC)
echo 'ifeq ($(CXX),cxx)' >> GNUmakefile
#echo '	$(CXX) $? $(CPPFLAGS) $(CXXFLAGS)' $hfcxxlibs '-o $(OBJ)xx.x -Hf' >> GNUmakefile
echo '	$(CXX) $? $(CPPFLAGS) $(CXXFLAGS) -o $(OBJ)xx.x -Hf' >> GNUmakefile
echo 'endif ' >> GNUmakefile

rm -f $TMPDIR/exclude.sort

echo " " >> GNUmakefile

set inc = $DPCBASEREP/Include
# set incxx = $inc/CxxInc
set incext = $EXTLIBDIR/Include
set incfitsio = $EXTLIBDIR/Include/FitsIO
set ainc = $ARCHPDEVREP/Include
set nomsys = `uname`

foreach f ( *.cc )
  grep -q '^'$f'$' exclude && continue
  gcc -MM -D$nomsys -I$ainc -I$inc -I$incext -I$incfitsio $f  \
#       | grep -v -e $inc -e $incfitsio -e $incext \
       | sed -e 's/.*\.o/\$(OBJ)&/'  >> GNUmakefile
#       | sed -e 's?'$inc/'?$(AINC)?g' \
#       | sed -e 's?'$incext/'?$(EXTINCPATH)?g' >> GNUmakefile
end

foreach f ( *.c )
  grep -q '^'$f'$' exclude && continue
  gcc -MM -D$nomsys -I$ainc -I$inc -I$incext -I$incfitsio $f  \
       | sed -e 's/.*\.o/\$(OBJ)&/'  >> GNUmakefile
#       | sed -e 's?'$ainc/'?$(AINC)?g' >> GNUmakefile
#       | sed -e 's?'$incext/'?$(EXTINCPATH)?g' >> GNUmakefile
end 




