# basic configuration for finding GRASS libraries and header files
# and putting files into the R and src directories
#
COMPILE="no"
PATH="$PATH:/usr/local/bin"
GR="no"
GRDEF="no"
INTERP="no"
while test -n "${1}" ; do
   case ${1} in
      --grass5=*)
         GR=`echo "${1}" | sed -e 's/[^=]*=//'`
	 if test -r ${GR}
	    then
            GRDEF="yes"
	 else
	    echo "${GR}: no such file"
	    exit 1
	 fi
         echo "GRASS5" $GR "from configure arguments"
         ;;
      --zinclude=*)
         ZH=`echo "${1}" | sed -e 's/[^=]*=//'`
	 if test -r ${ZH}/zlib.h
	    then
            ZHDEF="yes"
	 else
	    echo "${ZH}/zlib.h: no such file"
	    exit 1
	 fi ;;
      --zlib=*)
         LZ=`echo "${1}" | sed -e 's/[^=]*=//'`
         if test -r ${LZ}/libz.a
            then
            LZDEF="yes"
         else
            echo "${LZ}/libz.a: no such file"
            exit 1
         fi ;;
      --interp)
         INTERP="yes";;
      *)
         echo "Invalid configure option: ${1}"
         exit 1;;
   esac
   shift
done

if test ${INTERP} = "no"
   then
   if test ${GR} = "no"
      then
      GR=`sh G_which.sh grass5 2> /dev/null`
      if test -r ${GR}
	    then
            GRDEF="yes"
      fi 
   fi


   if test ${GRDEF} = "no"
      then
      echo "Your grass5 is not on your current PATH."
      echo "Either add the directory it is in to your PATH,"
      echo "or run \"R INSTALL --configure-args=--grass5=/.../grass5 GRASS\""
      echo "giving the fully qualified path to and name of your grass5."
      echo "To install the interpreted interface, use:"
      echo "\"R INSTALL --configure-args=--interp GRASS\""
      exit 1
   fi
fi

if test ${GRDEF} = "yes"
   then
   sh ./setup.sh $GR
   if test $? -eq 0
      then
      COMPILE="yes"
   fi
fi
if test $COMPILE = "no"
   then
   echo "GRASS source files not found - installing interpreted interface"
   if test -d src
      then
      rm -f src/*
      rmdir src
   fi

   exit 0
fi

echo "GRASS source files found - installing compiled interface"
if test ! -d src
   then
   mkdir src
fi
cp orig/src/*.[ch] src
cat orig/src/Makefile.in >> orig/src/Makefile
cp orig/src/Makefile src
cp orig/R/zzz.R R

exit 0
