#!/bin/csh
#  SOPHYA configuration managment script 
#  C. Magneville / R. Ansari   -      Mai 2005
#     Updated : 2006, Avr/Mai , Oct 2007 , 2010 
# 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 )

set defepiinc = ( /usr/X11R6/include )
set defepilib = ( /usr/X11R6/lib )

#----------------------------------------------------------------------
#### Decodage arguments
set cmdline = "$0 $*" 
set extpath
set extpathinc
set extpathlib
set followlink
set avecEXT 
unset incln
unset mincname
set compopt
unset arch32
unset arch64
unset sasz64
unset ssldble128 
unset nofpic
unset nothsafe
unset boundcheck
unset sodebug
unset usefftw2
unset uselapack2
unset alsofftwfloat
unset slballinone
## variables pour PI (librairie motif, GNU-readline ...)
set epipath
unset wgrdl 
set avecPI

#### boucle de decodage des arguments :
while ( $#argv > 0 )

if( "$1" == "-h" ) then
  echo 'configure [-sbase SOPHYABASE] [-scxx SOPHYACXX] [-incln] '
  echo "  [-minc mymake.inc]  [-compopt 'cc/cxxOptions'] "
  echo '  [-arch64] [-arch32] [-sasz64] [-ldble128] '
  echo '  [-nofpic] [-nothsafe] [-boundcheck] [-sodebug]'
  echo '  [-extp dir1 -extp dir2 ...] [-extip dir1 -extip dir2 ... ]'
  echo '  [-extlp dir1 -extlp dir2 ... ] [-followlink] '
  echo '  [-noextlib] [-noext fits] [-noext fftw] [-noext lapack] [-noext astro]'
  echo '  [-alsofftwfloat] [-usefftw2] [-uselapack2] '
  echo '  [-wgrdl] [-epip mdir1 -motp mdir2 ...]  [-noPI] [-slballinone]'
  echo '  (See SOPHYA manual/web pages for a detailed description of configure options)'
  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" == "-compopt" ) then
  set compopt = ( $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" == "-followlink" ) then
  set followlink = '-L '
endif
if( "$1" == "-noextlib" ) then
  set i = 0
  while ( $i < $#SOPEXTOK )
    @ i += 1
    set SOPEXTOK[$i] = 0
  end  
  unset avecEXT
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" == "-alsofftwfloat" ) then
# On veut la version float de FFTW, en plus de double
  set alsofftwfloat
  set i = 0
  while ( $i < $#EXTCURNAME )
    @ i += 1
    if( "fftw" == "$EXTCURNAME[$i]" ) then
       set ALLEXTLIBS[$i] = '-lfftw3 -lfftw3f'
    endif
  end
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
## Decodage arguments specifique PI
if( "$1" == "-noPI" ) then
  set SOPPIOK = ( 0 0 0 )
  unset avecPI 
endif
## compil piapp avec GNU-readline 
if( "$1" == "-wgrdl" ) then
  set wgrdl
endif
## Chemin motif , readline ...
if( "$1" == "-epip" ) then
  set epipath = ( $epipath $2 )
  shift
endif
if( "$1" == "-slballinone" ) then
# Pour utiliser une seule librairie libsophyaPIext.so au lieu 
#   des 3 libsophya.so libextsophya.so libPI.so
  set slballinone
endif
#  Decodage options pour flags de machdefs.h 
#  Liste de flags : SO_ARCH64 SO_NOFPIC SO_SASIZET64 SO_LDBLE128 SO_BOUNDCHECKING SOPHYA_DEBUG
if( "$1" == "-arch64" ) then
  set arch64
  unset arch32
endif
if( "$1" == "-arch32" ) then
  set arch32
  unset arch64
  unset sasz64
endif
if( "$1" == "-sasz64" ) then
  set sasz64
endif
if( "$1" == "-ldble128" ) then
  set ssldble128
endif
if( "$1" == "-nofpic" ) then
  set nofpic
endif
if( "$1" == "-nothsafe" ) then
  set nothsafe
endif
if( "$1" == "-boundcheck" ) then
  set boundcheck
endif
if( "$1" == "-sodebug" ) then
  set sodebug
endif


shift
end

## ------ Detection linux 64 bits, AIX, IRIX ---
unset linux64
if ( `uname`  == "Linux" ) then 
  if ( `uname -m` == "x86_64" ) then 
    echo ' ... Linux x86_64 (64 bits) ---> -arch64 ' 
    set linux64
    if ( ! $?arch32 ) set arch64
  endif
endif
set sname = `uname`
if ( ( "$sname" == "IRIX64" ) || ( "$sname" == "AIX" ) ) then 
  if ( ! $?arch32 ) set arch64 
endif


#----------------------------------------------------------------------
#### 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

## Chemin de recherche en fonction de 64/32 bits sur linux
if ( $?linux64 ) then 
  if ( $?arch64 ) then 
    set defextlib = ( /usr/lib64 /usr/local/lib )
    set defepilib = ( /usr/X11R6/lib64 )
    echo ' linux 64 , arch64 defextlib = ' $defextlib ' defepilib=' $defepilib 
  else
    echo ' linux 64 , arch32 defextlib = ' $defextlib ' defepilib=' $defepilib 
  endif
endif


#----------------------------------------------------------------------
#### 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 de 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.1/  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 $followlink $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

### recherche des .h Motif et si besoin readline 
set piextinc 
if ( $?avecPI ) then
  echo " "
  echo ">>>>>> 3.2/ searching include files for Motif "
  foreach d ( $epipath $defepiinc $extpathinc $extpath $defextinc )
    set duminc = `find $followlink $d -name Xm -print | head -1 `
    set dumrep = $duminc:h
    if ( "$dumrep" != "" ) then
      set piextinc = "-I$dumrep"
      echo Includes for Motif found in $dumrep
      break
    endif
  end
  if ( "$dumrep" == "" ) then
    echo WARNING: Includes for Motif NOT found
    echo '    expect problems when compiling PI modules, check sophyamake.inc ... '
  endif
  if ( $?wgrdl ) then 
    echo ">>>>>> 3.3/ searching include files for GNU readline "
    foreach d ( $epipath $extpathinc $extpath $defextinc )
      set duminc = `find $followlink $d -name readline -print | head -1 `
      set dumrep2 = $duminc:h
      if ( "$dumrep2" != "" ) then
        if ( "$dumrep2" != "$dumrep" )  set piextinc = "-I$dumrep2 $piextinc"
        echo Includes for readline found in $dumrep2
        break
      endif
    end
    if ( "$dumrep2" == "" ) then
      echo WARNING: Includes for readline NOT found
      echo '    expect problems when compiling piapp, check sophyamake.inc ... '
    endif
  endif
endif 

#----------------------------------------------------------------------
#### 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 $followlink $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

### recherche des librairies Motif et si besoin readline 
set piextlib = '-L/usr/X11R6/lib -lXm'
if ( $?avecPI ) then
  echo " "
  echo ">>>>>> 4.2/ searching library Motif (libXm.a) "
  foreach d ( $epipath $defepilib $extpathlib $extpath $defextlib )
    set dumlib = `find $followlink $d -name libXm.a -print | head -1 `
    set dumrep = $dumlib:h
    if ( "$dumrep" != "" ) then
      set piextlib = "-L$dumrep -lXm"
      echo Motif library found in $dumrep
      break
    endif
    end
    if ( "$dumrep" == "" ) then
      echo WARNING: Motif library NOT found
      echo '    expect problems when for linking with PI, check sophyamake.inc ... '
    endif
  if ( $?wgrdl ) then 
    echo ">>>>>> 4.3/ searching libraries for GNU readline "
  foreach d ( $epipath $extpathlib $extpath $defextlib )
    set dumlib = `find $followlink $d -name libreadline.a -print | head -1 `
    set dumrep2 = $dumlib:h
    if ( "$dumrep2" != "" ) then
      if ( "$dumrep2" != "$dumrep" )  then 
        set piextlib = "$piextlib -L$dumrep2 -lreadline -lhistory "
      else 
        set piextlib = "$piextlib -lreadline -lhistory "
      endif
      echo Libraries for readline found in $dumrep2
      break
    endif
    end
    if ( "$dumrep" == "" ) then
      echo WARNING: libraries for readline NOT found
      echo '    expect problems for linking piapp, check sophyamake.inc ... '
    endif
  endif
endif 

#----------------------------------------------------------------------
#### Identification des librairies archives Sophya
echo " "
echo ">>>>>> 4.4/  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 "PIEXTINC = $piextinc" >> $mfile
echo "PIEXTLIB = $piextlib" >> $mfile
echo "PIEXTINC = $piextinc" 
echo "PIEXTLIB = $piextlib"

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

# --- Gestion des flags modifiants les options de compilation et liste de librairies (par sed)
set sprgfile = $TMPDIR/compop.scom
rm -f $sprgfile
echo '{' > $sprgfile 
set scmd = "/FLAGS/s/CNFPHFLF/ $compopt/g"
if ( $?arch64 ) then
  set a64cf = ' '
  if ( ( "$sname" == "IRIX64" ) && ("$SOPHYACXX" == "CC" ) ) set a64cf = '-64'
  if ( "$sname" == "AIX" ) then 
    if ( ("$SOPHYACXX" == "xlC" ) || ("$SOPHYACXX" == "xlc" ) )  set a64cf = '-q64'
    echo "/ARFLAGS/s/-rcs/-rcs -X 64/" >>  $sprgfile 
  endif
  set scmd = "/FLAGS/s/CNFPHFLF/$a64cf $compopt/g"
endif
if ( $?arch32 ) then
  set a32cf = ' '
  if ( ( "$sname" == "Linux" ) && ("$SOPHYACXX" == "g++" ) ) set a32cf = '-m32'
  if ( "$sname" == "AIX" ) then 
    if ( ("$SOPHYACXX" == "xlC" ) || ("$SOPHYACXX" == "xlc" ) )  set a32cf = '-q32'
  endif
  set scmd = "/FLAGS/s/CNFPHFLF/$a32cf $compopt/g"
endif

echo $scmd >> $sprgfile
if ( $?nofpic ) echo "s/-fPIC/ /g" >>  $sprgfile
if( $?slballinone ) then 
  echo "/SOPHYAALLSLBLIST/s/-lPI -lextsophya -lsophya/-lAsophyaextPI/" >>  $sprgfile 
  echo "/SOPHYAEXTSLBLIST/s/-lextsophya -lsophya/-lAsophyaextPI/" >>  $sprgfile 
endif
if ( ! $?avecEXT ) then 
  echo "/SOPHYAEXTSLBLIST/s/-lextsophya / /"  >>  $sprgfile 
  echo "/SOPHYAALLSLBLIST/s/-lextsophya / /"  >>  $sprgfile 
endif
if ( ! $?avecPI ) then 
  echo "/SOPHYAALLSLBLIST/s/-lPI / /"  >>  $sprgfile 
endif
if ( ( $?linux64 ) && ( $?arch64) ) then
  echo "s?-L/usr/X11R6/lib?-L/usr/X11R6/lib64?g" >>  $sprgfile 
endif
echo '}' >> $sprgfile 
set mftmp = $TMPDIR/sopmake.inc
cp $mfile $mftmp
rm -f $mfile
sed -f $sprgfile $mftmp >   $mfile
rm -f $sprgfile $mftmp
# --- FIN de gestion des flags modifiants les options de compilation (par sed)

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
# Fait avant - set sname = `uname`
echo "#ifndef $sname" >> $mdefname
echo "#define $sname 1 "  >> $mdefname
echo "#endif" >> $mdefname

## Definition de flags de compilation en fonction des options de configure 
if ( $?arch64 )  echo "#define SO_ARCH64  1" >> $mdefname
if ( $?sasz64 )  echo "#define SO_SASIZET64  1" >> $mdefname
if ( $?ssldble128 ) echo "#define SO_LDBLE128  1" >> $mdefname
if ( $?nofpic )  echo "#define SO_NOFPIC  1" >> $mdefname
if ( $?nothsafe )  echo "#define SO_NOTHSAFE  1" >> $mdefname
if ( $?boundcheck )  echo "#define SO_BOUNDCHECKING  1" >> $mdefname
if ( $?sodebug )  echo "#define SOPHYA_DEBUG  1" >> $mdefname

if ( "$sname" == "IRIX64" ) then
  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 "/*------ SOPOHYA/ExtLibsvVersion selection flags ---- */" >> $sinc/sspvflags.h
echo " " >> $sinc/sspvflags.h 
if( $?alsofftwfloat ) then
  echo "/*-- Compile ALSO float version of FFTWServer methods -- */" >> $sinc/sspvflags.h
  echo "#define ALSO_FFTW_FLOAT_EXTSOP" >> $sinc/sspvflags.h
endif
if( $?usefftw2 ) then
  echo "/*-- Use FFTW V2 instead of V3 -- */" >> $sinc/sspvflags.h
  echo "#define FFTW_V2_EXTSOP" >> $sinc/sspvflags.h
endif
if( $?uselapack2 ) then
  echo "/*-- Use LAPACK V2 instead of V3 -- */" >> $sinc/sspvflags.h
  echo "#define LAPACK_V2_EXTSOP" >> $sinc/sspvflags.h
endif
if( $?wgrdl ) then
  echo "/*-- Compile piapp with GNU-readline -- */" >> $sinc/sspvflags.h
  echo "#define PIA_WITH_GNUREADLINE" >> $sinc/sspvflags.h
endif
if( $?slballinone ) then
  echo "/*------ Use of single shared lib for SOPHYA+extlibs+PI ---- */" >> $sinc/sspvflags.h
  echo "#define SO_SLBALLINONE " >> $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
set i = 0
while ( $i < $#SOPPI )
  @ i += 1
  set d = $SOPPI[$i]
  rm -f $sconf/$d.conf
  if( $SOPPIOK[$i] <= 0 ) continue
  if ( ! -d ../$d ) continue
  cat ../$d/objlist.list >> $objlistname
  touch $sconf/$d.conf
end


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