#!/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 SOPMODOK = ( 1 1 1 1 1 1 1 1 1 )
set SOPMOD = ( BaseTools SysTools SUtils TArray NTools HiStats SkyMap Samba SkyT )
set ALLSOPLIBS = ( '-lBaseTools' '-lSysTools' '-lSUtils' '-lTArray' '-lNTools' '-lHiStats' '-lSkyMap' '-lSamba' '-lSkyT' )

set SOPPIOK = ( 1 1 1 )
set SOPPI = ( PI PIext PIGcont )
set ALLPILIBS = ( '-lPI' '-lPIext' '-lPIGcont' )

set SOPEXT     = ( FitsIOServer  IFFTW  LinAlg  XAstroPack )
set EXTCURNAME = ( fits          fftw   lapack  astro      )
set SOPEXTOK   = ( 1             1      1       1          )
set EXTINCNAME = ( FitsIO/fitsio.h FFTW/fftw3.h  PASDEINC XAstro/astro.h )
set EXTLIBNAME = ( libcfitsio.a libfftw3.a liblapack.a libxastro.a )
set ALLEXTLIBS = ( '-lcfitsio' '-lfftw3' '-llapack -lblas' '-lxastro' )

#FFTW2: set EXTINCNAME = ( FitsIO/fitsio.h FFTW/fftw.h  PASDEINC XAstro/astro.h )
#FFTW2: set EXTLIBNAME = ( libcfitsio.a libfftw.a liblapack.a libxastro.a )
#FFTW2: set ALLEXTLIBS = ( '-lcfitsio' '-lfftw -lrfftw' '-llapack -lblas' '-lxastro' )

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
unset arch64
unset sasz64
unset nofpic
unset boundcheck
unset sodebug
unset usefftw2
unset uselapack2
unset singleslb
while ( $#argv > 0 )

if( "$1" == "-h" ) then
  echo 'configure [-sbase SOPHYABASE] [-scxx SOPHYACXX] [-incln] [-minc mymake.inc] '
  echo '  [-arch64] [-sasz64] [-nofpic] [-boundcheck] [-sodebug]'
  echo '  [-extp dir1 -extp dir2 ...] [-extip dir1 -extip dir2 ... ] [-extlp dir1 -extlp dir2 ... ]'
  echo '  [-noextlib -noext fits -noext fftw -noext lapack -noext astro]'
  echo '  [-usefftw2 -uselapack2] [-singleslb]'
  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" == "-noextlib" ) then
  set i = 0
  while ( $i < $#SOPEXTOK )
    @ i += 1
    set SOPEXTOK[$i] = 0
  end  
endif
if( "$1" == "-noext" ) then
  set i = 0
  while ( $i < $#EXTCURNAME )
    @ i += 1
    if( "$2" == "$EXTCURNAME[$i]" ) set SOPEXTOK[$i] = 0
  end
  shift
endif
if( "$1" == "-usefftw2" ) then
# version FFTW 2 au lieu de V3
  set usefftw2
  set i = 0
  while ( $i < $#EXTCURNAME )
    @ i += 1
    if( "fftw" == "$EXTCURNAME[$i]" ) then
       set EXTINCNAME[$i] = 'FFTW/fftw.h'
       set EXTLIBNAME[$i] = 'libfftw.a'
       set ALLEXTLIBS[$i] = '-lfftw -lrfftw'
    endif
  end
endif
if( "$1" == "-uselapack2" ) then
# version LAPACK 2 au lieu de V3
  set uselapack2
endif
if( "$1" == "-singleslb" ) then
# Pour utiliser une seule librairie libsophyaPIext.so au lieu 
#   des 3 libsophya.so libextsophya.so libPI.so
  set singleslb
endif
#  Decodage options pour flags de machdefs.h 
#  Liste de flags : SO_ARCH64 SO_NOFPIC SO_SASIZET64 SO_BOUNDCHECKING SOPHYA_DEBUG
if( "$1" == "-arch64" ) then
  set arch64
endif
if( "$1" == "-sasz64" ) then
  set sasz64
endif
if( "$1" == "-nofpic" ) then
  set nofpic
endif
if( "$1" == "-boundcheck" ) then
  set boundcheck
endif
if( "$1" == "-sodebug" ) then
  set sodebug
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 " "
if( $?incln ) then
  echo ">>>>>> 2/ Creating symbolic link for include files in $sinc "
else
  echo ">>>>>> 2/ Copying include files to $sinc "
endif

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
    if ( $?incln ) then 
      echo Creating link to include files for module $d
    else 
      echo Copying include files for module $d
    endif

    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 -q "$f:r\.h" excludeinc
          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
rm -f $sinc/machdefs_ac.h $sinc/machdefs_mkmf.h 
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 -follow | 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.1/  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 -follow | 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


#----------------------------------------------------------------------
#### Identification des librairies archives Sophya
echo " "
echo ">>>>>> 4.2/  Sophya libraries archives "
set sopmodlib
set i = 0
while ( $i < $#SOPMOD )
  @ i += 1
  if( $SOPMODOK[$i] <= 0 ) continue
  if( -d ../$SOPMOD[$i] ) then
    set sopmodlib = ( $sopmodlib $ALLSOPLIBS[$i] )
  else
    echo "WARNING: module "$SOPMOD[$i]" not found"
  endif
end
echo sopmodlib= $sopmodlib

echo " "
echo ">>>>>> 4.3/  PI-Sophya libraries archives "
set soppilib
set i = 0
while ( $i < $#SOPPI )
  @ i += 1
  if( $SOPPIOK[$i] <= 0 ) continue
  if( -d ../$SOPPI[$i] ) then
    set soppilib = ( $soppilib $ALLPILIBS[$i] )
  else
    echo "WARNING: module "$SOPPI[$i]" not found"
  endif
end
echo soppilib= $soppilib

#----------------------------------------------------------------------
#### 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 "# module libraries  " >> $mfile
echo "SOPMODLIBLIST = $sopmodlib" >> $mfile

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

echo " " >> $mfile 
echo "# PI libraries  " >> $mfile
echo "SOPPILIBLIST = $soppilib" >> $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.a/  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

if ( $?arch64 )  echo "#define SO_ARCH64  1" >> $mdefname
if ( $?nofpic )  echo "#define SO_NOFPIC  1" >> $mdefname
if ( $?sasz64 )  echo "#define SO_SASIZET64  1" >> $mdefname
if ( $?boundcheck )  echo "#define SO_BOUNDCHECKING  1" >> $mdefname
if ( $?sodebug )  echo "#define SOPHYA_DEBUG  1" >> $mdefname

if ( "$sname" == "IRIX64" ) then
#  SGI_ARCH64 : compile avec -64 (en 64 bits)
  echo "#define SGI_ARCH64 1 /* Compile avec -64 */" >> $mdefname
  if ( "$SOPHYACXX" == "CC" ) then
#  __SGICC__ : flag indiquant que c'est le compilo CC de SGI 
    echo "#define __SGICC__ 1" >> $mdefname
  endif
endif


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 de sspvflags.h
echo " "
echo ">>>>>> 6.b/  Creating  $sinc/sspvflags.h "
rm -f $sinc/sspvflags.h; touch $sinc/sspvflags.h

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

echo " " >> $sinc/sspvflags.h 
echo "/*------ Version selection flags ---- */" >> $sinc/sspvflags.h
if( $?usefftw2 ) then
  echo "#define FFTW_V2_EXTSOP" >> $sinc/sspvflags.h
endif
if( $?uselapack2 ) then
  echo "#define LAPACK_V2_EXTSOP" >> $sinc/sspvflags.h
endif
echo " " >> $sinc/sspvflags.h 
echo "/*------ Use of single shared lib for SOPHYA+extlibs+PI ---- */" >> $sinc/sspvflags.h
if( $?singleslb ) then
  echo "#define SINGLESLB_SOPHYA_PI_EXT" >> $sinc/sspvflags.h
endif
echo " " >> $sinc/sspvflags.h 
echo "#endif " >> $sinc/sspvflags.h 


#----------------------------------------------------------------------
#### 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 )
  rm -f $sconf/$d.conf
  if ( ! -d ../$d ) continue 
  cat ../$d/objlist.list >> $objlistname
  touch $sconf/$d.conf
end

set objlistname = $sconf/libextsophya.objlist
echo " 7.b/ Creating $objlistname "
rm -f $objlistname ; touch $objlistname
set i = 0
while ( $i < $#SOPEXT )
  @ i += 1
  set d = $SOPEXT[$i]
  rm -f $sconf/$d.conf
  if( $SOPEXTOK[$i] <= 0 ) continue
  if ( ! -d ../$d ) continue
  cat ../$d/objlist.list >> $objlistname
  touch $sconf/$d.conf
end  

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


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