#!/bin/csh
#  SOPHYA configuration managment script 
#  C. Magneville / R. Ansari   -      Mai 2005
# Exemple: ./configure -sbase /laltmp/ansari/sbase/ -scxx cxx -extp /exp/planck/ExtLibs/Include/ \
#              -extp /exp/planck/ExtLibs/OSF1-cxx/

#----------------------------------------------------------------------
#### liste des modules
set SOPMOD = ( BaseTools SysTools SUtils TArray NTools HiStats SkyMap Samba SkyT )
set SOPPI = ( PI PIext PIGcont )

set SOPEXT     = ( FitsIOServer  IFFTW  LinAlg  XAstroPack  MinuitAdapt )
set EXTCURNAME = ( fits          fftw   lapack  astro       minuit      )
set SOPEXTOK   = ( 1             1      1       1           1           )
set EXTINCNAME = ( FitsIO/fitsio.h FFTW/fftw.h  PASDEINC XAstro/astro.h Cern/minuit.h )
set EXTLIBNAME = ( libcfitsio.a libfftw.a liblapack.a libxastro.a libminuit.a )
set ALLEXTLIBS = ( '-lcfitsio' '-lfftw -lrfftw' '-llapack -lblas' '-lxastro' '-lminuit' )

set defextinc = ( ~ /usr/include /usr/local/include )
set defextlib = ( ~ /usr/lib /usr/local/lib )


#----------------------------------------------------------------------
#### Decodage arguments
set cmdline = "$0 $*" 
set extpath
set extpathinc
set extpathlib
unset incln
unset mincname
while ( $#argv > 0 )

if( "$1" == "-h" ) then
  echo 'configure [-sbase SOPHYABASE] [-scxx SOPHYACXX] [-incln] [-minc mymake.inc] '
  echo '  [-extp dir1 -extp dir2 ...] [-extip dir1 -extip dir2 ... ] [-extlp dir1 -extlp dir2 ... ]'
  echo '  [-noext fits -noext fftw -noext lapack -noext astro -noext minuit]'
  exit -1
endif
if( "$1" == "-sbase" ) then
  setenv SOPHYABASE $2
  shift
endif
if( "$1" == "-scxx" ) then
  setenv SOPHYACXX $2
  shift
endif
if( "$1" == "-incln" ) then
  set incln
endif
if( "$1" == "-minc" ) then
  set mincname = ( $2 )
  shift
endif
if( "$1" == "-extp" ) then
  set extpath = ( $extpath $2 )
  shift
endif
if( "$1" == "-extip" ) then
  set extpathinc = ( $extpathinc $2 )
  shift
endif
if( "$1" == "-extlp" ) then
  set extpathlib = ( $extpathlib $2 )
  shift
endif
if( "$1" == "-noext" ) then
  set i = 0
  while ( $i < $#EXTCURNAME )
    @ i += 1
    if( "$2" == "$EXTCURNAME[$i]" ) set SOPEXTOK[$i] = 0
  end
  shift
endif

shift
end


#----------------------------------------------------------------------
#### Verification generale
if( ! $?SOPHYABASE ) then
  echo ERROR: define SOPHYABASE or use script arguments
  exit -2
endif
if( ! $?SOPHYACXX ) then
  echo ERROR: define SOPHYACXX or use script arguments
  echo "   SOPHYACXX define the name of the C++ compiler (ex: g++ cxx ...)"
  exit -2
endif
if( ! $?TMPDIR ) setenv TMPDIR /tmp
touch $TMPDIR/tmp_test
if( $status != 0 ) then
  echo ERROR: no write acces in temporary directory: $TMPDIR
  echo '----> define TMPDIR environment variable: "setenv TMPDIR ..."'
  exit -2
endif
rm -f $TMPDIR/tmp_test
if( ! $?mincname ) then
  set mincname = `uname`_${SOPHYACXX}_make.inc
endif
if( ! -e $mincname ) then
  echo ERROR: file $mincname NOT found
  exit -2
endif
if( ! -e ../BaseTools/machdefs_mkmf.h ) then
  echo ERROR: file machdefs_mkmf.h NOT found in BaseTools
  exit -2
endif
echo SOPEXTOK: $SOPEXTOK

#----------------------------------------------------------------------
#### creation des variables chemins sophya core
echo " "
echo ">>>>>> 1/ Creating directory tree under $SOPHYABASE  "
set sinc = $SOPHYABASE/include
set slib = $SOPHYABASE/lib
set sslb = $SOPHYABASE/slb
set sobj = $SOPHYABASE/obj
set sexe = $SOPHYABASE/exe
set sconf = $sinc/SophyaConfInfo
foreach d ( $sinc $slib $sslb $sobj $sexe $sconf )
  if( ! -d $d ) then
    mkdir $d
    if( $status ) then
      echo ERROR: creation of $d failed
      exit -3
    endif
    echo Directory $d created
  endif
  touch $d/tmp_test
  if( $status ) then
    echo ERROR: no write acces to $d
    exit -4
  endif
  rm -f $d/tmp_test
end


#----------------------------------------------------------------------
#### Fichier confinfo - log e configure
set conflog = $sconf/conf.log
rm -f $conflog ; touch $conflog 
echo ' --- Log of SOPHYA configure script - Date= ' `date` >> $conflog
echo "$cmdline"  >> $conflog
set inclistf = $sconf/include.list
rm -f $inclistf ; touch $inclistf


#----------------------------------------------------------------------
#### liens ou copie des *.h
echo " "
echo ">>>>>> 2/ Copying include files to $sinc "
cd ../  > /dev/null
set srcd = `pwd`
set cpln = "cp -p "
if( $?incln ) set cpln = "ln -s -f  "
foreach d ( $SOPMOD $SOPEXT $SOPPI )
  if ( -d $srcd/$d ) then
    echo Copying include files for module $d
    cd $srcd/$d;
    set file = ( *.h )
    cd $sinc;  if( $status == 0 ) rm -f $file
    # on vire le .h de $file si .h dans excludeinc du module
    cd $srcd/$d
    if( -e excludeinc ) then
      if( `cat excludeinc | wc -l` > 0 ) then
        set file
        foreach f ( *.h )
          grep "$f" excludeinc > /dev/null
          if( $status != 0 ) set file = ( $file $f )
        end
      endif
    endif
    # on copie ou link dans $sinc
    if( `echo $file | wc -w` > 0 ) then
      foreach f ( $file )
        $cpln $srcd/$d/$f  $sinc/$f
      end
    endif
    echo $file >> $inclistf
  endif
end
cd $srcd/BuildMgr/.


#----------------------------------------------------------------------
#### lien  des .h des librairies externes
echo " "
echo ">>>>>> 3/  Include files for external libraries "
set i = 0
while ( $i < $#SOPEXT )
  @ i += 1
  if( "$EXTINCNAME[$i]" == "PASDEINC" ) continue
  if( $SOPEXTOK[$i] <= 0 ) continue
  set nom = $EXTINCNAME[$i]:t
  set rac = $EXTINCNAME[$i]:h
  if( -d ../$SOPEXT[$i] ) then
    echo "Searching include file for $SOPEXT[$i] ..."
    foreach d ( $extpathinc $extpath $defextinc )
      set duminc = `find $d -name $nom -print | head -1 `
      set dumrep = $duminc:h
      if ( "$dumrep" != "" ) then
        rm -f $sinc/$rac
        ln -f -s $dumrep $sinc/$rac
        echo Includes for $SOPEXT[$i] found in $dumrep
        break
      endif
    end
    if ( "$dumrep" == "" ) then
      echo WARNING: Includes for $SOPEXT[$i] NOT found
      echo "    Compilation of module  $SOPEXT[$i] disabled"
      set SOPEXTOK[$i] = 0
    endif
  endif
end


#----------------------------------------------------------------------
#### liens des librairies externes
echo " "
echo ">>>>>> 4/  External libraries "
set fpath = $TMPDIR/fpath
rm -f $fpath; touch $fpath
set extliblib
set i = 0
while ( $i < $#SOPEXT )
  @ i += 1
  if( "$EXTLIBNAME[$i]" == "PASDELIB" ) continue
  if( $SOPEXTOK[$i] <= 0 ) continue
  set nom = $EXTLIBNAME[$i]
  if( -d ../$SOPEXT[$i] ) then
    echo "Searching libraries path for $SOPEXT[$i] ..."
    foreach d ( $extpathlib $extpath $defextlib )
      set dumlib = `find $d -name $nom -print | head -1 `
      if ( "$dumlib" != "" ) then
        echo "-L$dumlib:h" >> $fpath
        set extliblib = ( $extliblib $ALLEXTLIBS[$i] )
        break
      endif
    end
    if ( "$dumlib" == "" ) then
      echo WARNING: Libraries for $SOPEXT[$i] NOT found
      echo "    Check the generated make.inc file"
    endif
  endif
end
set extlibpath = `cat $fpath | sort -u`
echo extlibpath= $extlibpath
echo extliblib= $extliblib
rm -f $fpath


#----------------------------------------------------------------------
#### Generation de sophyamake.inc
#  On le cree dans le repertoire temporaire d'abord 
set mfile = $sinc/sophyamake.inc
rm -f $mfile
echo " "
echo ">>>>>> 5/  Creating $mfile from $mincname "
echo "# -----------------------------------------------" > $mfile
echo "#  File make.inc - generated by SOPHYA configure" >> $mfile
echo "#  Date" `date` >> $mfile 
echo "#  Seed file : $mincname " >> $mfile 
echo "# -----------------------------------------------" >> $mfile
echo " " >> $mfile 
echo "# SOPHYA Base installation directory " >> $mfile 
echo "SOPHYABASE = $SOPHYABASE" >> $mfile
echo " " >> $mfile 
echo 'SOPHYAINCP = $(SOPHYABASE)/include/' >> $mfile
echo 'SOPHYAOBJP = $(SOPHYABASE)/obj/' >> $mfile
echo 'SOPHYALIBP = $(SOPHYABASE)/lib/' >> $mfile
echo 'SOPHYASLBP = $(SOPHYABASE)/slb/' >> $mfile
echo 'SOPHYAEXEP = $(SOPHYABASE)/exe/' >> $mfile

echo " " >> $mfile 
echo "# External libraries PATH  " >> $mfile
echo "SOPEXTLIBP = $extlibpath"  >> $mfile
echo "SOPEXTLIBLIST = $extliblib" >> $mfile

echo " " >> $mfile 
cat $mincname >> $mfile  

rm -f $mfile:t
ln -s $mfile $mfile:t


#----------------------------------------------------------------------
#### Copie de machdefs.h
set mdefname = $TMPDIR/machdefs.h
echo " "
echo ">>>>>> 6/  Creating  machdefs.h from BaseTools/machdefs_mkmf.h"
rm -f $mdefname; touch $mdefname
echo "/*** machdefs.h generated by SOPHYA configure ***/" >> $mdefname
echo "#ifndef MACHDEFS_SEEN" >> $mdefname
echo "#define MACHDEFS_SEEN" >> $mdefname
echo "/*------ Code generation switch ---- */" >> $mdefname
echo "/*   System selector (uname) */" >> $mdefname
set sname = `uname`
echo "#ifndef $sname" >> $mdefname
echo "#define $sname 1 "  >> $mdefname
echo "#endif" >> $mdefname
echo "/* #define SO_BOUNDCHECKING 1 */" >> $mdefname

echo " " >> $mdefname 
echo "/*------ External libraries no-use flag ---- */" >> $mdefname
set i = 0
while ( $i < $#SOPEXTOK )
  @ i += 1
  if( $SOPEXTOK[$i] > 0 ) continue
  echo "#define SOPHYA_NO_$SOPEXT[$i]" >> $mdefname
end

echo " " >> $mdefname 
grep -v -e '#ifndef *MACHDEFS_SEEN' -e '#define *MACHDEFS_SEEN' ../BaseTools/machdefs_mkmf.h >> $mdefname
#  On ne copie que si machdefs.h est different 
if (-e $sinc/machdefs.h ) then 
  diff $mdefname $sinc/machdefs.h >& /dev/null
  if ($status != 0) then 
    cp -f -p $mdefname $sinc/machdefs.h 
    echo " file $sinc/machdefs.h changed -> replaced"    
  else 
    echo " file $sinc/machdefs.h has not changed -> kept"
  endif
else
  cp -f -p $mdefname $sinc/machdefs.h  
  echo " file $sinc/machdefs.h created"    
endif
rm -f $mdefname


#----------------------------------------------------------------------
#### Creation des fichiers contenant les listes d'objets et d'includes
echo " "
echo ">>>>>> 7/  Creating object list files for shared library creation "
set objlistname = $sconf/libsophya.objlist
echo " 7.a/ Creating $objlistname "
rm -f $objlistname ; touch $objlistname
foreach d ( $SOPMOD ) 
  if ( -d ../$d ) then 
    cat ../$d/objlist.list >> $objlistname
  endif
end

set objlistname = $sconf/libextsophya.objlist
echo " 7.b/ Creating $objlistname "
rm -f $objlistname ; touch $objlistname
foreach d ( $SOPEXT ) 
  if ( -d ../$d ) then 
    cat ../$d/objlist.list >> $objlistname
  endif
end

set objlistname = $sconf/libPI.objlist
echo " 7.c/ Creating $objlistname "
rm -f $objlistname ; touch $objlistname
foreach d ($SOPPI )
  if ( -d ../$d ) then 
    cat ../$d/objlist.list >> $objlistname
  endif
end


#----------------------------------------------------------------------
exit 0
