#!/bin/csh
echo "Supported systems are : HPUX aix aix90 convex osf1 solaris sunos ultrix linux-pmac linux-intel"
echo "Enter operating system : "
set mach=($<)
switch ( $mach )
case	HPUX:
	setenv	GAG_SYS "HPUX" 
	setenv  GILDAS_MOTIF "YES"
		breaksw
case	aix:
	setenv	GAG_SYS "aix" 
	setenv  GILDAS_MOTIF "YES"
		breaksw
case	aix90:
	setenv	GAG_SYS "aix90" 
	setenv  GILDAS_MOTIF "YES"
		breaksw
case	linux-pmac:
	setenv	GAG_SYS "linux-pmac"
	setenv  GILDAS_MOTIF "YES"
		breaksw

case	linux-intel:
	setenv	GAG_SYS "linux-intel"
	setenv  GILDAS_MOTIF "YES"
	breaksw

case	linux-intel-f90:
	setenv	GAG_SYS "linux-intel-f90"
	setenv  GILDAS_MOTIF "YES"
	breaksw

case	osf1:
	setenv	GAG_SYS "osf1" 
	setenv  GILDAS_MOTIF "YES"
		breaksw
case	ultrix:
	setenv	GAG_SYS "ultrix"
	setenv  GILDAS_MOTIF "NO"
		breaksw
case	sunos:
	setenv	GAG_SYS "sunos" 
	setenv  GILDAS_MOTIF "NO"
		breaksw
case	solaris:
	setenv	GAG_SYS "solaris" 
	setenv  GILDAS_MOTIF "NO"
		breaksw
case	convex:
	setenv	GAG_SYS "convex" 
	setenv  GILDAS_MOTIF "NO"
		breaksw
case	vax:
		echo "Unsupported system VAX" 
		exit -1
		breaksw
default:
		echo "Unsupported system $mach" 
		exit -1
		breaksw
endsw
echo $GAG_SYS
#
echo "Enter top directory of GILDAS software : "
set dire=($<)
setenv OLDPWD $PWD; cd $dire; setenv GAG_TOP $PWD; cd $OLDPWD; echo $GAG_TOP;
echo "Checking export directory from $dire ($GAG_TOP)"
#
# Check subdirectories of install root
if ( -d $GAG_TOP/export ) then
    echo "Found directory $GAG_TOP/export"
else
    echo "E-GILDAS, $GAG_TOP/export does not exist"
    exit -1 
endif 
if ( -d $GAG_TOP/$GAG_SYS ) then
    echo "Found directory $GAG_TOP/$GAG_SYS"
else if ( -e $GAG_TOP/$GAG_SYS ) then
    echo "E-GILDAS, $GAG_TOP/$GAG_SYS is not a directory"
    exit -1
else
    echo "Creating directory $GAG_TOP/$GAG_SYS"
    mkdir $GAG_TOP/$GAG_SYS
endif
if ( -d $GAG_TOP/sou ) then
    echo "Found directory $GAG_TOP/sou"
else if ( -e $GAG_TOP/sou ) then
    echo "E-GILDAS, $GAG_TOP/sou is not a directory"
    exit -1
else
    echo "Creating directory $GAG_TOP/sou"
    mkdir $GAG_TOP/sou
endif
#
setenv GAG_ROOT $GAG_TOP/$GAG_SYS
#
cp $GAG_TOP/export/Makefile .
#
echo "Enter paper size on printer (A4 or US_LETTER) : "
set paper=($<)
switch ( $paper )
case    A4:
        setenv GAG_PAPER_SIZE "A4" 
		breaksw
case    US_LETTER:
        setenv GAG_PAPER_SIZE "US_LETTER" 
		breaksw
default:
        echo "Unsupported paper size $paper"
                exit -1
endsw
#
sed -e "s|DEST.*\#|DEST   =$GAG_ROOT\#|g"  \
    -e "s|TARDIR.*\#|TARDIR =$GAG_TOP/export\#|g"  \
    -e "s|MKDIR.*\#|MKDIR  =$GAG_TOP/export\#|g"  \
    -e "s|REF.*\#|REF    =$GAG_TOP/sou\#|g"  \
    -e "s|PAPER_SIZE.*|PAPER_SIZE = $GAG_PAPER_SIZE|g"    \
    $GAG_TOP/export/make-macros.$GAG_SYS > make.tmp
#
echo "LAPACK is used for CLIC"
echo "Enter FULL name of LAPACK Library (/dev/null if none) : "
set dire=($<)
setenv DIRL $dire
if ( -e $DIRL ) then
      echo "Found LAPACK library $DIRL "
   else
      echo "W-GILDAS, no LAPACK library"
      setenv DIRL "/dev/null"    
   endif
endif
#
echo "SLATEC is needed for task uv_fit-s"
echo "Enter FULL name of SLATEC Library (/dev/null if none) : "
set dire=($<)
setenv DIRS $dire
if ( -e $DIRS ) then
      echo "Found SLATEC library $DIRS "
   else
      echo "W-GILDAS, no SLATEC library"
      setenv DIRS "/dev/null"    
   endif
endif
#
echo "NAG is now only necessary for NIC"
echo "Enter FULL name of NAG Library (/dev/null if none) : "
set dire=($<)
setenv DIRN $dire
if ( -e $DIRN ) then
      echo "Found NAG library $DIRN "
   else
      echo "W-GILDAS, no NAG library"
      setenv DIRN "/dev/null"    
   endif
endif
sed -e "s|LAPACK.*\#|LAPACK =$DIRL\#|g"  \
    -e "s|NAG.*\#|NAG    =$DIRN\#|g"     \
    -e "s|SLATEC.*\#|SLATEC =$DIRS\#|g"  \
    make.tmp > make-macros.$GAG_SYS
#
echo " "
echo "Here is the  make-macros.$GAG_SYS ready for your system"
echo "-------------------------------------------------------"
cat make-macros.$GAG_SYS
echo " "
setenv PATH $GAG_ROOT/bin:$PATH
echo " "
echo "Ready to rebuild $GAG_SYS version in $GAG_ROOT"
echo " "
echo "with Path $PATH"
echo " "
rm make.tmp
echo "Enter Make choice (basic, iram, gag, or quit) "
set mach=($<)
switch ( $mach )
case    basic:
		time make basic 
		breaksw
case	iram:
		time make iramsoft 
		breaksw
case	gag:
		time make gagsofts 
		breaksw
case	quit:
		echo "Quitting..." 
		exit -1 
		breaksw
default:
		time make $mach 
		breaksw
endsw
echo " "
echo "Cleaning source directory"
time find $GAG_TOP -name '*,v' -perm 444 -exec chmod u+w {} \; 
time find $GAG_ROOT -name Makefile -exec chmod u+w {} \;
echo " "
make links
echo "GILDAS software ($mach) rebuild"

