# GRX Installer
usage()
{ echo "Usage: 
install.grx [-f] -i # [ force new download ] install GRX
install.grx -h      # help

Note: GRX needs ${LIB_DIR} in search path LD_LIBRARY_PATH
export LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:${LIB_DIR}"

if [ -n "$1" ]; then echo; echo "$1" ; fi
exit 1 ; }

wr()
{ echo $* ; }

check()
{ if [ $1 != 0 ]; then echo "ERROR: RC=$1 $2"; exit 1; fi ; }

cd_make()
{ D=${GRX}/$1
  cd ${D};            check $? "Could not cd ${D}"
  make -f ${MAKE} $2; check $? "make -f ${MAKE} $2 failed in ${D}" ; }

TMP_BSE=/tmp/${USER}
SRC=${TMP_BSE}/install.grx
T=${TMP_BSE}/tmpfile.grx

URL=http://www.gnu.de/software/GRX/download/grx244.tar.gz
DIR=grx244
GRX=${SRC}/${DIR}
CONF=${GRX}/makedefs.grx
PAS=grx244/pascal/bgi/graph.pas
MAKE=makefile.x11

INSTALL_GRX=false
FORCE_DOWNLOAD=false

# --------------- Set scope of installation: USER/ROOT/AFS --------------------
if [ -d /afs ]                    # may not be a good indicator for AFS
  then INSTALLER="A" ; SYS=`sys`  # if AFS is present, get system architecture
  else INSTALLER="L"              # Local filesystem
  fi
if [ "$USER" = "root" ]
  then INSTALLER="${INSTALLER}R"  # Root installs machine wide
  else INSTALLER="${INSTALLER}U"  # User into his home directory
  fi

case $INSTALLER in
 LU) INST_DIR=${HOME}/.gnu       ;; # Users local home dir
 LR) INST_DIR=/usr/gnu           ;; # Host wide
 AU) INST_DIR=${HOME}/.gnu       ;; # Users global AFS home dir
 AR) INST_DIR=/usr/gnu           ;; # Host wide
 AA) INST_DIR=/afs/products/gnu     # AFS admin site wide
     echo "AFS site wide installation not implemented"; exit 98 ;;
  *) echo "Fatal Error"; exit 99 ;;
 esac

#GPC_UNITS_DIR=${INST_DIR}/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/units
GPC_UNITS_DIR=${INST_DIR}/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/units
GRX_UNITS_DIR=${INST_DIR}/units
LIB_DIR=${INST_DIR}/lib

# ---------------- Read command line parameters -------------------------------
while [ $# -gt 0 ]
do
  SWITCH=$1
  case ${SWITCH} in
   -f) FORCE_DOWNLOAD=true ;;
   -h) usage ;;
   -i) INSTALL_GRX=true ;;          # do installation
    *) usage "Unknown switch $1" ;;
  esac
  if [ $# -gt 0 ]; then shift; else usage "Missing parameter for ${SWITCH}"; fi
done
if [ ${INSTALL_GRX} = false ]; then usage; fi

# --------------- Check prerequisits ------------------------------------------
if [ ! -d ${INST_DIR} ]  ; then usage "Missing dir ${INST_DIR}" ; fi
if [ ! -d ${GPC_UNITS_DIR} ] ; then usage "Missing dir ${GPC_UNITS_DIR}"; fi
if [ ! -d ${TMP_BSE} ] ; then mkdir -p ${TMP_BSE} ; fi # New dir if necessary

# ---------------- download and install ---------------------------------------
if [ ! -d ${SRC} ] ; then mkdir -p ${SRC}; fi # New dir if necessary

cd ${SRC}; check $? "Could not cd ${DIR}"

F=`basename ${URL}`
if [ -f ${F} -a ${FORCE_DOWNLOAD} = true ] ; then rm ${F}; fi
if [ ! -f ${F} ]; then wget ${URL} ; check $? "Downloding ${URL}"; fi

if [ -d ${DIR} ]; then rm -r ${DIR} ; fi
gunzip -c ${DIR}.tar.gz | tar -xvf - ; check $? "Failed untaring"

# modify ${DIR}/makedefs.grx
cat ${CONF} | sed "s|INCLUDE_GPC_SUPPORT=n|INCLUDE_GPC_SUPPORT=y|" |\
              sed "s|INSTALLDIR=/usr/local|INSTALLDIR=${INST_DIR}|" > $T
check $? "Failed editing makedefs.grx"
mv $T ${CONF}

cd_make src
cd_make test
cd_make test/bgi
cd_make pascal
cd_make src    install

if [ ! -d ${GRX_UNITS_DIR} ]; then usage "Missing grx units directory"; fi
mv ${GRX_UNITS_DIR}/* ${GPC_UNITS_DIR}
rmdir ${GRX_UNITS_DIR}
wr "GRX installation done, hopefully!"
