#! /bin/sh
#

# This script is the front-line automated installation tool for
# Globus.  It runs "before" any autoconf processing.

GLOBUS_INSTALL_PROG=`echo $0 | sed -e 's|.*/\([^/]*\)|\1|g'`

if [ X"$GLOBUS_INSTALL_PROG" = "gsi-install" ]; then
   GLOBUS_INSTALL_GSI="true";
fi
GLOBUS_GSI_INSTALL_ARGS="--without-mpi --without-mpl --without-inx \
                         --without-shm --without-threads --without-ldap \
                         --disable-services"
GLOBUS_GSI_CONFIG_ARGS="--enable-gssapi"

print_usage ()
{
  if [ ! "${GLOBUS_INSTALL_SIMULATE}" = "true" ]
  then
     cat <<EOF

     usage: ${GLOBUS_INSTALL_PROG} [option...] [-- config-opt...]
     
     This script will refuse to run as root.  See Globus documentation.
          See globus-install --help for more information about option and
          config-opt.
EOF

     exit 1
  fi

  GLOBUS_INSTALL_BUILD_ERROR=true
}

bailout ()
{
  echo "abort!"
  echo "${GLOBUS_INSTALL_PROG} interrupted."
  echo "Installation may be incomplete!"
  exit 1
}

trap bailout 1 2 3 6 15 2> /dev/null


# check for required programs
if printf "%s" test > /dev/null 2>&1 ; then
   GLOBUS_INSTALL_PRINT="printf %s"
else
   GLOBUS_INSTALL_PRINT=echo
fi

normalize_patharg()
{
   case $patharg in
     /* )
        patharg=$patharg
        ;;
     * )
        patharg=`pwd`/"$patharg"
        ;;
   esac

   # collapse all sequences of '/'s to a single '/' 
   #
   patharg=`echo $patharg | sed -e 's://*:/:g'`

   # collapse all instances of "/./" to a single '/'
   # note we must loop for all series forms "/././" because
   # the patterns overlap and only half match in each iteration
   #
   while echo $patharg | fgrep '/./' > /dev/null 2>&1
   do
      # this goes faster when we inline a couple iterations
      # into one sed command
      patharg=`echo $patharg | sed -e 's:/\./:/:g' \
                             -e 's:/\./:/:g'`
   done

   while echo $patharg | fgrep '/../' > /dev/null 2>&1
   do
      patharg=`echo $patharg | sed -e 's:^/\.\./:/:g' \
                             -e 's:/[^./][^/]*/\.\./:/:g' \
                             -e 's:/\.[^./][^/]*/\.\./:/:g' \
                             -e 's:/\.\.[^/][^/]*/\.\./:/:g'`
   done

}


# determine the srcdir..
GLOBUS_SRCDIR=`echo $0 \
                | sed -e "s/${GLOBUS_INSTALL_PROG}//g"`

patharg="${GLOBUS_SRCDIR}"
normalize_patharg
GLOBUS_SRCDIR="${patharg}"

# set defaults for platform
GLOBUS_BUILD_PLATFORM=`${GLOBUS_SRCDIR}/Configuration/config.guess`

prompt_user ()
{
   ${GLOBUS_INSTALL_PRINT} "${prompt_message} "
   ${GLOBUS_INSTALL_PRINT} "[${prompt_default}] "
   read prompt_user_response
   if [ "X${prompt_user_response}" = "X" ]
   then
      prompt_user_response="${prompt_default}"
   fi
}


parse_option ()
{
  if echo ${optionstring} | grep '=' > /dev/null 2>&1
  then
     optionval=`echo ${optionstring} \
                  | sed -e 's/[^=]*=//g'`
  else
     optionval=""
  fi
}

GLOBUS_INSTALL_ARGS=""
GLOBUS_CONFIG_ARGS=""

for arg in "$@"; do
 GLOBUS_INSTALL_HAVE_ARGS="true"
 if [ "X${GLOBUS_INSTALL_PAST_DASHDASH}" = "Xtrue" ]
 then
  case "${arg}" in
     --enable-io | --enable-alpha | \
     --enable-nexus | --enable-gram | \
     --enable-gass | --enable-duroc | \
     --enable-dc | --enable-utp | \
     --enable-rsl | --enable-duct | \
     --enable-hbm | --enable-mds | \
     --enable-gloperf)
        echo "Passed configure option \"${arg}\" conflicts with ${GLOBUS_INSTALL
_PROG}!"
        print_usage
        ;;

     *)
        GLOBUS_CONFIG_ARGS="$GLOBUS_CONFIG_ARGS ${arg}"
        ;;
  esac
 else
  case "${arg}" in
     --)
        GLOBUS_INSTALL_PAST_DASHDASH=true
        ;;
     *)
        GLOBUS_INSTALL_ARGS="$GLOBUS_INSTALL_ARGS ${arg}"
        ;;
  esac
 fi
done

eval "${GLOBUS_SRCDIR}"/globus-install \
        "${GLOBUS_INSTALL_ARGS}" "${GLOBUS_GSI_INSTALL_ARGS}" -- \
        "${GLOBUS_CONFIG_ARGS}" "${GLOBUS_GSI_CONFIG_ARGS}"
