#!/bin/ksh
echo "Supported systems are: "
echo "  hpux aix aix90 convex osf1 solaris sunos irix32 irix64 ultrix linux-intel linux-pmac"
read mach?"Enter operating system : "
case $mach in
	HPUX)
		GAG_SYS="HPUX" 
		GILDAS_MOTIF="YES" ;;
	hpux)
		GAG_SYS="hpux" 
		GILDAS_MOTIF="YES" ;;
	aix)
		GAG_SYS="aix" 
		GILDAS_MOTIF="YES" ;;
	aix90)
		GAG_SYS="aix90" 
		GILDAS_MOTIF="YES" ;;
	osf1)
		GAG_SYS="osf1" 
		GILDAS_MOTIF="YES" ;;
	ultrix)
		GAG_SYS="ultrix" 
		GILDAS_MOTIF="NO" ;;
	sunos)
		GAG_SYS="sunos" 
		GILDAS_MOTIF="NO" ;;
	solaris)
		GAG_SYS="solaris" 
		GILDAS_MOTIF="NO" ;;
	convex)
		GAG_SYS="convex" 
		GILDAS_MOTIF="NO" ;;
        irix)
                GAG_SYS="irix"
                GILDAS_MOTIF="YES" ;;
	linux-intel)
		GAG_SYS="linux-intel" 
		GILDAS_MOTIF="YES" ;;
	linux-intel-f90)
		GAG_SYS="linux-intel-f90" 
		GILDAS_MOTIF="YES" ;;
	linux-pmac)
		GAG_SYS="linux-pmac" 
		GILDAS_MOTIF="YES" ;;
	vax)
		echo "Unsupported system VAX" 
		exit -1;;
	*)
		echo "Unsupported system $mach" 
		exit -1;;
esac
echo $GAG_SYS
#
read dire?"Enter top directory of GILDAS software : "
echo "Checking export directory from $dire"
cd $dire; DIRE=$PWD; cd $OLDPWD; echo $DIRE;
if [[ -d $DIRE/export ]] then
    echo "Found directory $DIRE/export"
else
    echo "E-GILDAS, $DIRE/export does not exist"
    exit -1 
fi
if [[ -d $DIRE/$GAG_SYS ]] then
    echo "Found directory $DIRE/$GAG_SYS"
elif [[ -a $DIRE/$GAG_SYS ]] then
    echo "E-GILDAS, $DIRE/$GAG_SYS is not a directory"
    exit -1
else
    echo "Creating directory $DIRE/$GAG_SYS"
    mkdir $DIRE/$GAG_SYS
fi
if [[ -d $DIRE/sou ]] then
    echo "Found directory $DIRE/sou"
elif [[ -a $DIRE/sou ]] then
    echo "E-GILDAS, $DIRE/sou is not a directory"
    exit -1
else
    echo "Creating directory $DIRE/sou"
    mkdir $DIRE/sou
fi
GAG_ROOT=$DIRE/$GAG_SYS
GAG_TOP=$DIRE
#
cp $DIRE/export/Makefile .
#
read paper?"Enter paper size on printer (A4 or US_LETTER) : "
case $paper in
        A4)
                GAG_PAPER_SIZE="A4" ;;
        US_LETTER)
                GAG_PAPER_SIZE="US_LETTER" ;;
        *)
                echo "Unsupported paper size $paper"
                exit -1;;
esac

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
#sed -e "s?/users/gag/exe?$GAG_ROOT?g" \
#    -e "s?/users/gag?$DIRE?g"  \
#    -e "s?local_paper_size?$GAG_PAPER_SIZE?g"    \
#    $DIRE/export/make-macros.$GAG_SYS > make.tmp
#
echo "LAPACK is used for CLIC"
read dire?"Enter FULL name of LAPACK Library (/dev/null if not available) : "
if [[ -a $dire ]] then
    echo "Found LAPACK library $dire "
    DIRL=$dire
else
    echo "W-GILDAS, no LAPACK library"
    DIRL="/dev/null"    
fi
#
echo "SLATEC is needed for task uv_fit-s"
read dire?"Enter FULL name of SLATEC Library (/dev/null if not available) : "
if [[ -a $dire ]] then
    echo "Found SLATEC library $dire "
    DIRS=$dire
else
    echo "W-GILDAS, no SLATEC library"
    DIRS="/dev/null"    
fi
#
echo "NAG is now only necessary for NIC"
read dire?"Enter FULL name of NAG Library (/dev/null if not available) : "
if [[ -a $dire ]] then
    echo "Found NAG library $dire "
    DIRN=$dire
else
    echo "W-GILDAS, no NAG library"
    DIRN="/dev/null"    
fi
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
#sed -e "s?/users/nag?$DIRN?g"  \
#    -e "s?/users/lapack?$DIRL?g"  \
#    -e "s?/users/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 " "
PATH=$GAG_ROOT/bin:$PATH
echo " "
echo "Ready to rebuild $GAG_SYS version in $GAG_ROOT"
echo " "
echo "with Path $PATH"
echo " "
export GAG_ROOT GAG_SYS PATH GILDAS_MOTIF
rm make.tmp
# On change la protection des *,v. En effet l'experience montre que
# rebuild crashe souvent sur une erreur et ne change pas la protection des
# fichiers RCS, ce qui conduit a une nouvelle erreur, inattendue, au rebuild
# suivant (GD).
find $GAG_TOP -name '*,v' -perm 444 -exec chmod u+w {} \; 
find $GAG_TOP -name 'Makefile*' -exec chmod u+w {} \; 

read mach?"Enter Make choice (basic, gag, iram... or quit) "
case $mach in
        basic)
		time make basic ;;
	iram)
		time make iramsoft ;;
	gag)
		time make gagsofts ;;
	quit)
		echo "Quitting..." 
		exit -1 ;;
	*)
		time make $mach ;;
esac
echo " "
echo "Cleaning source directory"
time (
find $GAG_TOP -name '*,v' -perm 444 -exec chmod u+w {} \; ;\
find $GAG_TOP -name 'Makefile*' -exec chmod u+w {} \; )
make links
echo " "
echo "GILDAS software ($mach) rebuild"






