#! /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'`

print_usage ()
{

#
#          -with-shm                    force SHM  (override default)
#          -without-shm                 ignore SHM (override default)
#          (by default SHM flavors are not built)


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

     usage: ${GLOBUS_INSTALL_PROG} [option...] [-- config-opt...]
        or: ${GLOBUS_INSTALL_PROG} --dryrun [option...] [-- config-opt...]
        or: ${GLOBUS_INSTALL_PROG} --help
     
     This script will refuse to run as root.  See Globus documentation.
     
       available options:
          -enable-64bit                build for 64 bit target on IRIX
          (by default n32 libs are built)
          -with-make=make-prog         use specific 'make' program
          (by default the MAKE environment variable is used if set,
           or 'make' is searched for in the user's path)
          -with-umask=octal-umask      use specific umask permission defaults
          (by default the existing umask is used if it gives read access,
           or the user is prompted to use or override the default 022)
          -prompt                      verify before each major step
          -separate-logs               separate error/output logs
          -dryrun                      don't really build
          -help                        print this usage message
     
       these values will be prompted if not provided:
          -prefix=dir                  install Globus under dir
          -with-ssl-path=dir           use SSL installation under dir
          -enable-parallel-build       build whole components in parallel
          -with-ldap-path=dir          use LDAP installation under dir
     
       these option flags skip default build steps:
          -disable-services            don't install system services
          -disable-tools               don't install user tools
          -disable-development         don't install development files
     
       expert options:
          -with-threads                only build threaded libs
          -with-threads=package[,pack] same as above, set thread package(s)
          -without-threads             only build non-threaded libs
          (by default both non-threaded and threaded libs are built)

	  -with-tool-threads[=package] force Globus tools to use threads
     
          -with-domain-name=domain     supply domain name for this host
          (useful for hosts where automatic detection fails)

          -without-standard            skip standard protocol flavor
          -with-mpi                    force MPI  (override detection)
          -with-mpl                    force MPL  (override detection)
          -with-inx                    force INX  (override detection)
          -without-mpi                 ignore MPI (override detection)
          -without-mpl                 ignore MPL (override detection)
          -without-inx                 ignore INX (override detection)
          (by default libs for all applicable message systems are built)
     
     
          -disable-debug               build performance (non-debug) libs
          (by default debug libs are built)
     
          -parallel-make[=flag]        compile in parallel (w/ make flag)
          -development-tests           extra files in development tree
          -builddirs-persist           don't clean up build space
          -without-ldap                suppress normal ldap checks
	  -with-insure                 build with the insure++ memory checker
          (these options are intended for Globus developers)
     
       configure options:
          --  config-opt...             control underlying configure command
          After the '--' option, experts can specify options they
          wish to have passed to the underlying configure of all builds.
          These options should NEVER include any of the above-mentioned
          option flags as conflicts will surely occur!
     
EOF

     exit 1
  fi

  GLOBUS_INSTALL_BUILD_ERROR=true
}

bailout ()
{
  echo "abort!"

  echo "${GLOBUS_INSTALL_PROG} interrupted."
  echo "Installation may be incomplete!"
  # kill all children
  trap "" 1 2 3 6 15 2> /dev/null
  kill -15 0
  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_dir()
{
        if [ -d "$1" ]
        then
           (cd "$1" > /dev/null 2>&1 && /bin/sh -c pwd)
        else
           case "$1" in
              /*)
                 echo "$1"
              ;;
              *)
                 echo "`/bin/sh -c pwd`/$1"
              ;;
           esac
        fi
}

normalize_patharg()
{
   patharg="`normalize_dir "$patharg"`"
}


# 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_SIMULATE=false
GLOBUS_INSTALL_PROMPT=false
GLOBUS_INSTALL_DEVEL_TESTS=false

GLOBUS_OPT_STD=true
GLOBUS_OPT_DEBUG=true

GLOBUS_OPT_CONFIG_OPTS=""

with_domain_name_opt=""

parallel_make=false
parallel_make_flag=""

GLOBUS_SSL_PATH=/usr/local/ssl
GLOBUS_LDAP_PATH=/usr/local/ldap

# process user arguments
for arg in "$@"; do

 if [ "X${GLOBUS_INSTALL_PAST_DASHDASH}" = "Xtrue" ]
 then
  case "${arg}" in
     --prefix* | --disable-services | --disable-tools | \
      -prefix* |  -disable-services |  -disable-tools | \
     --disable-development | --with-threads* | --without-threads | \
      -disable-development |  -with-threads* |  -without-threads | \
     --without-mpi | --without-inx | --without-mpl | --with-mpi | \
      -without-mpi |  -without-inx |  -without-mpl |  -with-mpi | \
     --with-inx | --with-mpl | --enable-64bit | --enable-debug | \
      -with-inx |  -with-mpl |  -enable-64bit |  -enable-debug | \
     --with-shm | --without-shm | \
      -with-shm |  -without-shm | \
     --disable-debug | --with-ssl-path* | \
      -disable-debug |  -with-ssl-path* | \
     --bindir* | --sbindir* | --libexecdir* | --datadir* | \
      -bindir* |  -sbindir* |  -libexecdir* |  -datadir* | \
     --sysconfdir* | --sharedstatedir* | --localstatedir* | \
      -sysconfdir* |  -sharedstatedir* |  -localstatedir* | \
     --libdir* | --includedir* | --oldincludedir* | --infodir* | \
      -libdir* |  -includedir* |  -oldincludedir* |  -infodir* | \
     --mandir* | --srcdir* | \
      -mandir* |  -srcdir* | \
     --with-globus-prefix* | --with-globus-tools-prefix* | \
      -with-globus-prefix* |  -with-globus-tools-prefix* | \
     --with-globus-services-prefix* | \
      -with-globus-services-prefix* )
        echo "Passed configure option \"${arg}\" conflicts with ${GLOBUS_INSTALL_PROG}!"
        print_usage
        ;;

     *)
        GLOBUS_OPT_CONFIG_OPTS="${GLOBUS_OPT_CONFIG_OPTS} ${arg}"
        ;;
  esac
 else
  case "${arg}" in

     --bindir* | --sbindir* | --libexecdir* | --datadir* | \
      -bindir* |  -sbindir* |  -libexecdir* |  -datadir* | \
     --sysconfdir* | --sharedstatedir* | --localstatedir* | \
      -sysconfdir* |  -sharedstatedir* |  -localstatedir* | \
     --libdir* | --includedir* | --oldincludedir* | --infodir* | \
      -libdir* |  -includedir* |  -oldincludedir* |  -infodir* | \
     --mandir* | --srcdir* | \
      -mandir* |  -srcdir* | \
     --with-globus-prefix* | --with-globus-tools-prefix* | \
      -with-globus-prefix* |  -with-globus-tools-prefix* | \
     --with-globus-services-prefix* | \
      -with-globus-services-prefix* )
        echo "Passed option \"${arg}\" not supported via ${GLOBUS_INSTALL_PROG}!"
        print_usage
        ;;

     --help | -help)
       # disable simulate mode, print usage and bail..
       GLOBUS_INSTALL_SIMULATE=false
       print_usage
       ;;

     --dryrun | -dryrun)
       GLOBUS_INSTALL_SIMULATE=true
       ;;

     --prompt | -prompt)
       GLOBUS_INSTALL_PROMPT=true
       ;;

     --separate-logs | -separate-logs)
       GLOBUS_INSTALL_SPLIT_LOGS=true
       ;;

     --enable-parallel-build | -enable-parallel-build)
       parallel_build=true
       ;;

     --disable-parallel-build | -disable-parallel-build)
       parallel_build=false
       ;;

     --parallel-make* | -parallel-make*)
       optionstring=${arg}
       parse_option
       parallel_make_flag=${optionval}
       if [ "X$parallel_make_flag" = "X" ]
       then
          parallel_make_flag="-j"
       fi
       ;;

     --builddirs-persist | -builddirs-persist)
       GLOBUS_INSTALL_BUILDDIRS_PERSIST=true
       ;;

     --development-tests | -development-tests)
       GLOBUS_INSTALL_DEVEL_TESTS=true
       ;;

     --with-make* | -with-make*)
        optionstring=${arg}
	parse_option
        MAKE=${optionval}
        ;;

     --with-umask* | -with-umask*)
        optionstring=${arg}
        parse_option
        USER_UMASK=${optionval}
        ;;

     --prefix* | -prefix*)
        optionstring=${arg}
        parse_option
        GLOBUS_INSTALL_PREFIX=${optionval}
        ;;

     --with-domain-name | -with-domain-name | \
     --with-domain-name= | -with-domain-name= )
        echo "Option \"--with-domain-name\" requires a non-empty argument!"
        print_usage
        ;;

     --with-domain-name* | -with-domain-name* )
        with_domain_name_opt=${arg}
        ;;

     --disable-services | -disable-services )
        GLOBUS_INSTALL_SERVICES=false
        ;;

     --disable-tools | -disable-tools )
        GLOBUS_INSTALL_TOOLS=false
        ;;

     --disable-development | -disable-development )
        GLOBUS_INSTALL_DEVELOPMENT=false
        ;;

     --with-threads* | -with-threads*)
        optionstring=${arg}
        parse_option
        GLOBUS_OPT_THREADS=${optionval}
	if [ "X${GLOBUS_OPT_THREADS}" = "X" ]  
	then
	    GLOBUS_OPT_THREADS=threads_only
	fi
        ;;

     --with-tool-threads* | -with-tool-threads*)
        optionstring=${arg}
        parse_option
        GLOBUS_OPT_TOOL_THREADS=${optionval}
        case ${GLOBUS_INSTALL_ARCH} in
           *solaris*)
              GLOBUS_OPT_TOOL_THREADS=solaristhreads
              ;;
           *)
              GLOBUS_OPT_TOOL_THREADS=pthreads
              ;;
        esac
        ;;

     --without-threads | -without-threads)
        GLOBUS_OPT_THREADS="lite"
        ;;

     --without-standard | -without-standard)
        GLOBUS_OPT_STD=false
        ;;

     --without-mpi | -without-mpi)
        GLOBUS_OPT_MPI=false
        ;;

     --without-inx | -without-inx)
        GLOBUS_OPT_INX=false
        ;;

     --without-shm | -without-shm)
        GLOBUS_OPT_SHM=false
        ;;

     --without-mpl | -without-mpl) 
        GLOBUS_OPT_MPL=false
        ;;

     --with-standard | -with-standard)
        GLOBUS_OPT_STD=true
        ;;

     --with-mpi | -with-mpi)
        GLOBUS_OPT_MPI=true
        ;;

     --with-inx | -with-inx)
        GLOBUS_OPT_INX=true
        ;;

     --with-shm | -with-shm)
        GLOBUS_OPT_SHM=false
        ;;

     --with-mpl | -with-mpl)
        GLOBUS_OPT_MPL=true
        ;;

     --enable-64bit | -enable-64bit)
        GLOBUS_OPT_64BIT=true
        ;;

     --enable-debug | -enable-debug)
        GLOBUS_OPT_DEBUG=true
        ;;

     --disable-debug | -disable-debug)
        GLOBUS_OPT_DEBUG=false
        ;;

     --with-ssl-path* | -with-ssl-path*)
        optionstring=${arg}
        parse_option
        GLOBUS_SSL_PATH=${optionval}
        ;;

     --with-ldap-path* | -with-ldap-path*)
        optionstring=${arg}
        parse_option
        GLOBUS_LDAP_PATH=${optionval}
        ;;

     --without-ldap | -without-ldap)
        optionstring=${arg}
        parse_option
        GLOBUS_LDAP_SKIP=true
        ;;
     --with-insure | -with-insure)
        optionstring=${arg}
	parse_option
	INSURE_CMD=true
	;;

     --)
        GLOBUS_INSTALL_PAST_DASHDASH=true
        ;;

     *)
        echo "Unrecognized option \"${arg}\"!"
        print_usage
        ;;

  esac
 fi
done

case ${GLOBUS_BUILD_PLATFORM} in
   *irix6.*)
     if [ "X${GLOBUS_OPT_64BIT}" = "Xtrue" ]
     then
        GLOBUS_INSTALL_ARCH=${GLOBUS_BUILD_PLATFORM}-64
     else
        GLOBUS_INSTALL_ARCH=${GLOBUS_BUILD_PLATFORM}-n32
     fi
     ;;

   *)
     GLOBUS_INSTALL_ARCH=${GLOBUS_BUILD_PLATFORM}
     ;;
esac


if [ X`whoami` = "Xroot" ]
then
   echo "Do not perform globus builds as root!  See Globus documents for more information."
   print_usage
fi

if [ "X${USER_UMASK}" = "X" ]
then
  USER_UMASK=`umask`

  case "${USER_UMASK}" in
    000 | 002 | 020 | 022 | 2 | 20 | 22 | \
    200 | 202 | 220 | 222 )
      # these umasks all give enough read/execute access so we'll
      # go along with it.
      :
      ;;
    *)
      echo ""
      echo "Globus is normally installed to be used by multiple people!"
      echo "Please accept the suggested default or override it."

      prompt_message="Enter default permission mask (umask) in octal"
      prompt_default="022"
      prompt_user
      USER_UMASK="${prompt_user_response}"
      ;;
  esac
fi

umask "${USER_UMASK}" > /dev/null 2>&1
while [ $? -ne 0 ]
do
   echo "Failed to set umask!"
   prompt_message="Enter default permission mask (umask) in octal"
   prompt_default="022"
   prompt_user

   umask "${prompt_user_response}" > /dev/null 2>&1
done


if [ "X${with_domain_name_opt}" = "X" ]
then
  hostname=`hostname`

  case ${hostname} in 
     *.*)
        # this name has at least one dot, so accept as FQDN
        :
        ;;
     *)
        # this name has no dots, can't be FQDN

        echo ""

        echo "Domain name not returned by 'hostname' command!"

        prompt_message="Enter domain name of this host"
        prompt_default=""
        prompt_user

        while [ "X" = "X${prompt_user_response}" ]
        do
           echo "Domain name must be non-empty!"
           prompt_user
        done

        with_domain_name_opt="--with-domain-name=${prompt_user_response}"
        ;;
  esac
fi

if [ "X${GLOBUS_SSL_PATH}" = "X" ]
then
   GLOBUS_SSL_PATH=/usr/local/ssl
fi

while { [ ! -r ${GLOBUS_SSL_PATH}/include/ssl.h ] && \
        [ ! -r ${GLOBUS_SSL_PATH}/include/openssl/ssl.h ] ; } || \
      { echo ${GLOBUS_INSTALL_ARCH} | grep irix6 > /dev/null 2>&1 && \
        { [ "X${GLOBUS_OPT_64BIT}" = "Xtrue" ] && \
          [ 0 -eq `file ${GLOBUS_SSL_PATH}/lib/libssl.a \
            | grep -c '64.bit objects'` ] ; \
        } ; \
      } || \
      { echo ${GLOBUS_INSTALL_ARCH} | grep irix6 > /dev/null 2>&1 && \
        { [ ! "X${GLOBUS_OPT_64BIT}" = "Xtrue" ] && \
          [ 0 -ne `file ${GLOBUS_SSL_PATH}/lib/libssl.a \
            | grep -c '64.bit objects'` ] ; \
        } ; \
      } ; do

   echo ""

   if [ -r ${GLOBUS_SSL_PATH}/lib/libssl.a ]
   then
     if { echo ${GLOBUS_INSTALL_ARCH} | grep irix6 > /dev/null 2>&1 && \
          { [ "X${GLOBUS_OPT_64BIT}" = "Xtrue" ] && \
            [ 0 -eq `file ${GLOBUS_SSL_PATH}/lib/libssl.a \
              | grep -c '64.bit objects'` ] ; \
          } ; \
        } 
     then
        echo "Cannot use 32-bit SSL library when compiling in 64-bit mode!"
     elif { echo ${GLOBUS_INSTALL_ARCH} | grep irix6 > /dev/null 2>&1 && \
            { [ ! "X${GLOBUS_OPT_64BIT}" = "Xtrue" ] && \
              [ 0 -ne `file ${GLOBUS_SSL_PATH}/lib/libssl.a \
                | grep -c '64.bit objects'` ] ; \
            } ; \
          }
     then
        echo "Cannot use 64-bit SSL library when compiling in 32-bit mode!"
     fi
   fi

   echo "Cannot find suitable SSL files under \"${GLOBUS_SSL_PATH}\"!"
   prompt_message="Enter path to SSL files"
   prompt_default=""
   prompt_user
   GLOBUS_SSL_PATH=${prompt_user_response}
done

patharg="${GLOBUS_SSL_PATH}"
normalize_patharg
GLOBUS_SSL_PATH="${patharg}"

if [ ! "X$GLOBUS_LDAP_SKIP" = "Xtrue" ]
then
 while [ ! -r ${GLOBUS_LDAP_PATH}/include/ldap.h ] || \
      { echo ${GLOBUS_INSTALL_ARCH} | grep irix6 > /dev/null 2>&1 && \
        { [ "X${GLOBUS_OPT_64BIT}" = "Xtrue" ] && \
          [ 0 -eq `file ${GLOBUS_LDAP_PATH}/lib/libldap.a \
            | grep -c '64.bit objects'` ] ; \
        } ; \
      } || \
      { echo ${GLOBUS_INSTALL_ARCH} | grep irix6 > /dev/null 2>&1 && \
        { [ ! "X${GLOBUS_OPT_64BIT}" = "Xtrue" ] && \
          [ 0 -ne `file ${GLOBUS_LDAP_PATH}/lib/libldap.a \
            | grep -c '64.bit objects'` ] ; \
        } ; \
      } ; do

   echo ""

   if [ -r ${GLOBUS_LDAP_PATH}/lib/libldap.a ]
   then
     if { echo ${GLOBUS_INSTALL_ARCH} | grep irix6 > /dev/null 2>&1 && \
          { [ "X${GLOBUS_OPT_64BIT}" = "Xtrue" ] && \
            [ 0 -eq `file ${GLOBUS_LDAP_PATH}/lib/libldap.a \
              | grep -c '64.bit objects'` ] ; \
          } ; \
        } 
     then
        echo "Cannot use 32-bit LDAP library when compiling in 64-bit mode!"
     elif { echo ${GLOBUS_INSTALL_ARCH} | grep irix6 > /dev/null 2>&1 && \
            { [ ! "X${GLOBUS_OPT_64BIT}" = "Xtrue" ] && \
              [ 0 -ne `file ${GLOBUS_LDAP_PATH}/lib/libldap.a \
                | grep -c '64.bit objects'` ] ; \
            } ; \
          }
     then
        echo "Cannot use 64-bit LDAP library when compiling in 32-bit mode!"
     fi
   fi

   echo "Cannot find suitable LDAP files under \"${GLOBUS_LDAP_PATH}\"!"
   prompt_message="Enter path to LDAP files"
   prompt_default=""
   prompt_user
   GLOBUS_LDAP_PATH=${prompt_user_response}
 done

 patharg="${GLOBUS_LDAP_PATH}"
 normalize_patharg
 GLOBUS_LDAP_PATH="${patharg}"
fi

if test "$GLOBUS_INSTALL_SERVICES" != "false" ; then
    if [ ! -x ${GLOBUS_LDAP_PATH}/libexec/slapd ]
    then
       	echo "Warning: Found no useable ${GLOBUS_LDAP_PATH}/libexec/slapd!," 
       	echo "         and without it, some MDS features may not work!"
       	prompt_message="Proceed with build anyway? (yes/no)"
       	prompt_default="no"
       	prompt_user
       	proceed_anyway=${prompt_user_response}
       	if [ ! "X${proceed_anyway}" = "Xyes" ]
       	then
       	    echo "Build aborted."
       	    exit 1
       	fi
    fi
fi

while [ "X${GLOBUS_INSTALL_PREFIX}" = "X" ] || \
      { { [ ! -w ${GLOBUS_INSTALL_PREFIX} ] || \
          [ ! -d ${GLOBUS_INSTALL_PREFIX} ] ; \
        } && \
        mkdir -p ${GLOBUS_INSTALL_PREFIX} 2> /dev/null ; \
        { [ ! -w ${GLOBUS_INSTALL_PREFIX} ] || \
          [ ! -d ${GLOBUS_INSTALL_PREFIX} ] ; \
        } ; \
      } ; do

  echo ""

   if [ ! "X${GLOBUS_INSTALL_PREFIX}" = "X" ] && \
      [ ! -w ${GLOBUS_INSTALL_PREFIX} ]
   then
      echo "Globus install prefix \"${GLOBUS_INSTALL_PREFIX}\" is not writeable!"
   elif [ ! "X${GLOBUS_INSTALL_PREFIX}" = "X" ] && \
        [ ! -d ${GLOBUS_INSTALL_PREFIX} ]
   then
      echo "Globus install prefix \"${GLOBUS_INSTALL_PREFIX}\" is not a directory!"
   fi

   prompt_message="Enter the Globus install prefix"
   prompt_default="/usr/local/globus"
   prompt_user
   GLOBUS_INSTALL_PREFIX="${prompt_user_response}"
done

patharg="${GLOBUS_INSTALL_PREFIX}"
normalize_patharg
GLOBUS_INSTALL_PREFIX="${patharg}"

while [ "X${parallel_build}" = "X" ]
do

  prompt_message="Allow parallel builds (bad for slow CPUs or small RAM)?"
  prompt_default="no"
  prompt_user
  parallel_build="${prompt_user_response}"

  case ${parallel_build} in
     yes )
       parallel_build=true
       ;;
     no )
       parallel_build=false
       ;;
     * )
       echo ""
       echo "Please answer \"yes\" or \"no\"."
       parallel_build=""
       ;;
  esac
done

globus_prefix_opt="--with-globus-prefix=${GLOBUS_INSTALL_PREFIX}"
globus_tools_prefix_opt="--with-globus-tools-prefix=${GLOBUS_INSTALL_PREFIX}/tools/${GLOBUS_BUILD_PLATFORM}"
globus_services_prefix_opt="--with-globus-services-prefix=${GLOBUS_INSTALL_PREFIX}/services/${GLOBUS_BUILD_PLATFORM}"


if [ "X${GLOBUS_INSTALL_SERVICES}" = "X" ]
then
   GLOBUS_INSTALL_SERVICES=true
fi

if [ "X${GLOBUS_INSTALL_TOOLS}" = "X" ]
then
   GLOBUS_INSTALL_TOOLS=true
fi

if [ "X${GLOBUS_INSTALL_DEVELOPMENT}" = "X" ]
then
   GLOBUS_INSTALL_DEVELOPMENT=true
fi


dump_status_script ()
{
REBUILD_COMMAND="${GLOBUS_SRCDIR}/${GLOBUS_INSTALL_PROG} \
--prefix=${GLOBUS_INSTALL_PREFIX} \
--with-ssl-path=${GLOBUS_SSL_PATH} \
--with-ldap-path=${GLOBUS_LDAP_PATH} \
--with-umask=${USER_UMASK}"

if [ ! "X${MAKE}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --with-make=${MAKE}"
fi

if [ ! "X${INSURE_CMD}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --with-insure"
fi

if [ ! "X${with_domain_name_opt}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} ${with_domain_name_opt}"
fi

if [ "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --dryrun"
fi

if [ "X${GLOBUS_INSTALL_PROMPT}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --prompt"
fi

if [ "X${parallel_build}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --enable-parallel-build"
else
  REBUILD_COMMAND="${REBUILD_COMMAND} --disable-parallel-build"
fi

if [ ! "X${parallel_make_flag}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --parallel-make=${parallel_make_flag}"
fi

if [ "X${GLOBUS_INSTALL_SPLIT_LOGS}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --separate-logs"
fi

if [ "X${GLOBUS_INSTALL_BUILDDIRS_PERSIST}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --builddirs-persist"
fi

if [ "X${GLOBUS_INSTALL_DEVEL_TESTS}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --development-tests"
fi

if [ "X${GLOBUS_OPT_THREADS}" = "Xlite" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --without-threads"
elif [ ! "X${GLOBUS_OPT_THREADS}" = "X" ] && \
     [ ! "X${GLOBUS_OPT_THREADS}" = "Xthreads_only" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --with-threads=${GLOBUS_OPT_THREADS}"
fi

if [ "X${GLOBUS_OPT_MPI}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --with-mpi"
elif [ ! "X${GLOBUS_OPT_MPI}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --without-mpi"
fi
if [ "X${GLOBUS_OPT_MPL}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --with-mpl"
elif [ ! "X${GLOBUS_OPT_MPL}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --without-mpl"
fi
if [ "X${GLOBUS_OPT_INX}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --with-inx"
elif [ ! "X${GLOBUS_OPT_INX}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --without-inx"
fi
if [ "X${GLOBUS_OPT_STD}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --with-standard"
elif [ ! "X${GLOBUS_OPT_STD}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --without-standard"
fi
if [ "X${GLOBUS_OPT_SHM}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --with-shm"
elif [ ! "X${GLOBUS_OPT_SHM}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --without-shm"
fi

if [ "X${GLOBUS_OPT_64BIT}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --enable-64bit"
elif [ ! "X${GLOBUS_OPT_64BIT}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --disable-64bit"
fi

if [ "X${GLOBUS_OPT_DEBUG}" = "Xtrue" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --enable-debug"
elif [ ! "X${GLOBUS_OPT_DEBUG}" = "X" ]
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --disable-debug"
fi

if [ "X${GLOBUS_INSTALL_SERVICES}" = "Xfalse" ] 
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --disable-services"
fi
if [ "X${GLOBUS_INSTALL_TOOLS}" = "Xfalse" ] 
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --disable-tools"
fi
if [ "X${GLOBUS_INSTALL_DEVELOPMENT}" = "Xfalse" ] 
then
  REBUILD_COMMAND="${REBUILD_COMMAND} --disable-development"
fi

REBUILD_COMMAND="${REBUILD_COMMAND} -- ${GLOBUS_OPT_CONFIG_OPTS}"

cat <<EOF
#! /bin/sh
#

print_usage ()
{
echo ""
echo "usage: \$0 [--help|--rebuild]"
echo ""
echo "This script contains a final summary of the options"
echo "being used by the '${GLOBUS_INSTALL_PROG}' program."
echo ""
echo "Final command summary:"
echo "'${REBUILD_COMMAND}'"
echo ""
echo "Running this script with the --rebuild option"
echo "will reinitiate the entire build process, and is useful"
echo "if the previous build was interrupted.  Doing so will destroy"
echo "and rebuild the affected installation files from scratch, so"
echo "USE THIS SCRIPT WITH CAUTION!"
echo ""
}

case \$1 in
   --rebuild)
      eval "${REBUILD_COMMAND}"
      ;;
   *)
      print_usage
      ;;
esac

EOF
}

mkdir -p globus-build && \
rm -f globus-build/${GLOBUS_INSTALL_PROG}.status.$$ && \
dump_status_script > globus-build/${GLOBUS_INSTALL_PROG}.status.$$ && \
chmod a+x globus-build/${GLOBUS_INSTALL_PROG}.status.$$ && \
echo "" && \
echo "All options saved in checkpoint file \"./globus-build/${GLOBUS_INSTALL_PROG}.status.$$\"."
echo "Run \"./globus-build/${GLOBUS_INSTALL_PROG}.status.$$ --help\" for more info." && \
echo ""

pre_build ()
{
     rm -f ${prefix}/globus-software-summary
     rm -rf ${build_dir} 2> /dev/null && \
     mkdir -p ${build_dir} && \
     touch ${build_log} &&  \
     touch ${build_err_log}
}


build_configure ()
{
   if [ "X${GLOBUS_OPT_64BIT}" = "Xtrue" ]
   then
      bit_opt="--enable-64bit"
   else
      bit_opt=""
   fi

CONFIG_COMMAND_OPT_STRING="--with-ssl-path=${GLOBUS_SSL_PATH}\
 --with-ldap-path=${GLOBUS_LDAP_PATH}\
 --prefix=${prefix}\
 --exec_prefix=${exec_prefix}\
 ${with_domain_name_opt}\
 ${globus_prefix_opt}\
 ${globus_tools_prefix_opt} ${globus_services_prefix_opt}\
 ${jm_opt} ${threads_opt} ${bit_opt}\
 ${shm_opt} ${protocol_opt} ${debug_opt}\
 ${GLOBUS_OPT_CONFIG_OPTS}"

CONFIG_COMMAND_STRING="${GLOBUS_SRCDIR}/configure\
 ${CONFIG_COMMAND_OPT_STRING}"

   if [ ! "${GLOBUS_INSTALL_SIMULATE}" = "true" ] 
   then
     if ( cd ${build_dir} && \
          eval "${CONFIG_COMMAND_STRING}" ) \
        >> ${build_log} 2>> ${build_err_log}
     then
        return 0
     else 
        return 1
     fi
   else
      echo "simulate configure" >> ${build_log} 2>&1
      echo "(" cd ${build_dir} "&&" ${CONFIG_COMMAND_STRING} ")" >> ${build_log} 2>&1 
      return 0
   fi
}

build_make ()
{
   if [ ! "X${MAKE}" = "X" ]
   then
      make="${MAKE}"
   else
      make=make
   fi

   if [ ! "X${INSURE_CMD}" = "X" ]
   then
      insure="CC=insure LD=insure"
   else
      insure=""
   fi

   if [ ! "${GLOBUS_INSTALL_SIMULATE}" = "true" ]
   then
      if ( cd ${build_dir} && \
           { for target in ${build_make_target}
             do
                ${make} ${insure} ${parallel_make_flag} ${target}
		if [ ! "$?" = "0" ]
		then
		   return 1
		fi
             done ;
           } ;  ) >> ${build_log} 2>> ${build_err_log}
      then
         return 0
      else 
         return 1
      fi
   else
      echo "simulate make" >> ${build_log} 2>&1
      echo "( cd ${build_dir} && \
           { for target in ${build_make_target}\
             do\
                ${make} ${insure} ${parallel_make_flag} ${target}\
             done ;\
           } ;  )" >> ${build_log} 2>&1
      return 0
   fi
}

build_install ()
{
   if [ ! "X${MAKE}" = "X" ]
   then
      make="${MAKE}"
   else
      make=make
   fi

   if [ ! "X${INSURE_CMD}" = "X" ]
   then
      insure="CC=insure LD=insure"
   else
      insure=""
   fi

   if [ ! "${GLOBUS_INSTALL_SIMULATE}" = "true" ]
   then
      if ( cd ${build_dir} && \
           { for target in ${build_install_target}
             do
                ${make} ${insure} ${parallel_make_flag} ${target}
		if [ ! "$?" = "0" ]
		then
		   return 1
		fi
             done ;
           } ; ) >> ${build_log} 2>> ${build_err_log}
      then
         return 0
      else 
         return 1
      fi
   else
      echo "simulate install" >> ${build_log} 2>&1
      echo "( cd ${build_dir} && \
           { for target in ${build_install_target}\
             do\
                ${make} ${insure} ${parallel_make_flag} ${target}\
             done ;\
           } ; )" >> ${build_log} 2>&1
      return 0
   fi
}

post_build ()
{
  if [ "X${GLOBUS_INSTALL_BUILDDIRS_PERSIST}" = "Xtrue" ]
  then
     echo "Letting build space persist."
  else
     echo "Cleaning temporary build space... "
     rm -rf ${build_dir}
  fi
}

services_finalize ()
{
  if [ "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]
  then
     echo "Dryrun mode--skipping finalize."
  else
     ${GLOBUS_INSTALL_PRINT} "Finalizing installed files... "
     emit_globus_local_deploy
     emit_globus_setup
     emit_globus_path_programs
     ( cd ${exec_prefix}/bin && \
       rm -f \
           globus-duroc-request globus-duroc-test-app \
           globus-gass-get globus-gass-put \
           globus-gass-server gsh ; ) \
        >> ${build_log} 2>> ${build_err_log}
     ( cd ${exec_prefix}/libexec && \
       rm -f globus-make-rsl-string ; ) \
       >> ${build_log} 2>> ${build_err_log}
     ( cd ${exec_prefix} && \
       rm -rf lib include ; ) \
        >> ${build_log} 2>> ${build_err_log}
     ( cd ${prefix}/etc && \
       rm -f makefile_header Makefile.mpich && \
       if [ -f globus-gatekeeper.conf ] ; then \
         mv globus-gatekeeper.conf globus-gatekeepers.conf ; \
       fi ; ) \
        >> ${build_log} 2>> ${build_err_log}
     echo "done."
  fi
}

tools_finalize ()
{
  if [ "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]
  then
     echo "Dryrun mode--skipping finalize."
  else
     ${GLOBUS_INSTALL_PRINT} "Finalizing installed files... "
     emit_globus_setup
     emit_globus_path_programs
     ( cd ${exec_prefix}/bin && \
       rm -f gloperfd \
	     globus-duroc-request globus-duroc-test-app \
             globus-god2cldif globus-god2html globus-god2java  \
             globus-god2scheme globus-god2table ; ) \
         >> ${build_log} 2>> ${build_err_log}
     ( cd ${prefix}/etc && \
       rm -f makefile_header Makefile.mpich ; ) \
         >> ${build_log} 2>> ${build_err_log}
     ( cd ${exec_prefix}/libexec && \
       rm -f globus-gram-reporter \
             globus-job-manager-* globus-k5 \
             globus-script-* ; ) \
         >> ${build_log} 2>> ${build_err_log}
     ( cd ${exec_prefix}/sbin && \
       rm -f SXXglobus SXXglobus-server \
             globus-daemon-master globus-daemon-monitor \
             globus-daemons-controller globus-gatekeeper-controller \
             globus-gatekeeper-register globus-local-deploy \
             grid-info-build-site grid-info-restart \
             globus-system-controller globus-gatekeeper \
             globus-hbm-datacollector globus-hbm-localmonitor ; ) \
         >> ${build_log} 2>> ${build_err_log}
     ( cd ${exec_prefix} && \
       rm -rf lib include ; ) \
        >> ${build_log} 2>> ${build_err_log}
     echo "done."
  fi
}

development_finalize ()
{
    if [ "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]; then
	echo "Dryrun mode--skipping finalize."
    else
	tools_prefix="../../tools/${GLOBUS_BUILD_PLATFORM}"
	( cd ${prefix} && \
	  ln -s ${tools_prefix} tools ; ) \
		>> ${build_log} 2>> ${build_err_log}

	emit_config_guess
	emit_globus_path_programs

	if [ "X${GLOBUS_INSTALL_DEVEL_TESTS}" = "Xtrue" ]; then
	    echo "Development test mode--skipping finalize."
	else
	    ${GLOBUS_INSTALL_PRINT} "Finalizing installed files... "
	    (   cd ${prefix}/etc && \
		rm -f globus-daemons.conf globus-gatekeeper.conf \
		    grid-info-hosts.conf \
		    grid-info-passwd globus-ssleay.conf \
		    globus-server-daemons.conf grid-mapfile \
		    globus-hbm-datacollector.conf \
		    ldapfilter.conf ldapfriendly \
		    ldapsearchprefs.conf ldaptemplates.conf ; ) \
		>> ${build_log} 2>> ${build_err_log}
	    for dir in com bin libexec sbin share tmp var ; do
		rm -rf "${prefix}/${dir}"
	    done >> ${build_log} 2>> ${build_err_log}
	    echo "done."
	fi
    fi
}

build_worker () # finalize
{
   code=0

      [ -d ${build_dir} ] && \
      echo "    clearing existing build space... "

      if pre_build
      then
        echo "    configuring build space... "
        if build_configure
        then
           echo "    making files... "
           if build_make
           then
               echo "    installing files... "
               if build_install
               then
                   # run finalize script
                   eval "$1"
                   echo "complete."
               else
                   code=4
               fi
           else 
               code=3
           fi
        else
            code=2
        fi
      else
        code=1
      fi

  post_build

   if [ "X${code}" = "X0" ] && \
      [ ! "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]
   then
       if echo "configure: ${CONFIG_COMMAND_OPT_STRING}" \
         > ${exec_prefix}/globus-software-summary
       then
          :
       else
          code=5
       fi
   elif [ "X${code}" = "X0" ] && \
        [ "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]
   then
        echo "configure: ${CONFIG_COMMAND_OPT_STRING}" \
         > ${exec_prefix}/globus-software-summary.dryrun
   fi

   if [ ! "X${code}" = "X0" ]
   then
       GLOBUS_INSTALL_BUILD_ERROR=true
       echo "Installation of ${build_label} is not complete."
       return 1
   else
       return 0
   fi
  return $code
}

do_build ()  # finalize
{
     build_log=${build_log_base}.out.log
     if [ "X${GLOBUS_INSTALL_SPLIT_LOGS}" = "Xtrue" ]
     then
        build_err_log=${build_log_base}.err.log
     else
        build_err_log=${build_log}
     fi

 if [ "X${GLOBUS_INSTALL_PROMPT}" = "Xtrue" ]
 then 
   prompt_message="Install ${build_label} components?"
   prompt_default="yes"
   prompt_user
   build_now="${prompt_user_response}"
 else
   build_now=yes
 fi

 if [ "X${build_now}" = "Xyes" ]
 then
   if { [ -d ${prefix} ] || mkdir -p ${prefix} ; } && \
      { [ -d ${exec_prefix} ] || mkdir -p ${exec_prefix} ; } 
   then

      GLOBUS_INSTALL_ATTEMPTS="${GLOBUS_INSTALL_ATTEMPTS} ${exec_prefix}"

      if [ "X$parallel_build" = "Xtrue" ]
      then
         echo "Spawning ${build_label} build."
         echo "You can follow our progress by watching the following logfile:"
         echo "\"${build_log}\""

         ( build_worker "$1" 2>&1 > /dev/null ; ) &
      else
         echo "Installing ${build_label}."
         echo "You can follow our progress by watching the following logfile:"
         echo "\"${build_log}\""

         if ( build_worker "$1" ; )
         then
           :
         else
           return $?
         fi
      fi

   else
      echo "Cannot create target directory \"${prefix}\"!"
      return 8
   fi

 else
   echo "Skipping installation of ${build_label}."
 fi

 return 0
}

emit_globus_path_programs ()
{
    if [ ! -f ${GLOBUS_INSTALL_PREFIX}/bin/globus-development-path ]; then

	[ -d ${GLOBUS_INSTALL_PREFIX}/bin ] || \
	mkdir ${GLOBUS_INSTALL_PREFIX}/bin 2> /dev/null || \
	mkdir -p ${GLOBUS_INSTALL_PREFIX}/bin

	path_programs="globus-development-path \
		       globus-tools-path \
		       globus-services-path"

	for _file in $path_programs ; do
	    { \
	        [ -f ${exec_prefix}/bin/${_file} ] && \
		cp ${exec_prefix}/bin/${_file} \
		    ${GLOBUS_INSTALL_PREFIX}/bin/${_file}  && \
		chmod a+x ${GLOBUS_INSTALL_PREFIX}/bin/${_file} ; \
	    }  || \
	    GLOBUS_INSTALL_BUILD_ERROR=true ;
	done
    fi	
}


emit_globus_local_deploy ()
{
  ####
  if [ ! -f ${GLOBUS_INSTALL_PREFIX}/sbin/globus-local-deploy ] 
  then

    [ -d ${GLOBUS_INSTALL_PREFIX}/sbin ] || \
    mkdir ${GLOBUS_INSTALL_PREFIX}/sbin 2> /dev/null || \
    mkdir -p ${GLOBUS_INSTALL_PREFIX}/sbin 

     { \
       { \
         cat > ${GLOBUS_INSTALL_PREFIX}/sbin/globus-local-deploy && \
         chmod a+x ${GLOBUS_INSTALL_PREFIX}/sbin/globus-local-deploy \
           < /dev/null ; \
       } || \
       GLOBUS_INSTALL_BUILD_ERROR=true ; \
     } <<EOF
#! /bin/sh
#
# This script calls the appropriate globus-local-deploy script
# located in the service/\${arch}/sbin/  directory


prefix="${GLOBUS_INSTALL_PREFIX}"

arch=\`\${prefix}/sbin/config.guess\`

if [ ! -f \${prefix}/services/\${arch}/sbin/globus-local-deploy ] ; then
   echo "Globus has not been installed for the current architecture"
   exit 1
fi

\${prefix}/services/\${arch}/sbin/globus-local-deploy "\$@" 
EOF
    chmod a+x ${GLOBUS_INSTALL_PREFIX}/sbin/globus-local-deploy || \
    GLOBUS_INSTALL_BUILD_ERROR=true
fi
  ####
}


emit_config_guess ()
{
    if [ ! -f ${GLOBUS_INSTALL_PREFIX}/sbin/config.guess ]; then

	[ -d ${GLOBUS_INSTALL_PREFIX}/sbin ] || \
	mkdir ${GLOBUS_INSTALL_PREFIX}/sbin 2> /dev/null || \
	mkdir -p ${GLOBUS_INSTALL_PREFIX}/sbin 

	{ \
	    cp ${GLOBUS_SRCDIR}/Configuration/config.guess \
	       ${GLOBUS_INSTALL_PREFIX}/sbin/config.guess  && \
	    chmod a+rx ${GLOBUS_INSTALL_PREFIX}/sbin/config.guess ; \
	} || \
	GLOBUS_INSTALL_BUILD_ERROR=true ;
    fi
}


emit_globus_setup () 
{
  ###########################
  if [ ! -f ${GLOBUS_INSTALL_PREFIX}/sbin/globus-setup ]
  then

    emit_config_guess

     { \
       { \
         cat > ${GLOBUS_INSTALL_PREFIX}/sbin/globus-setup      && \
         chmod a+x ${GLOBUS_INSTALL_PREFIX}/sbin/globus-setup < /dev/null ; \
       } || \
       GLOBUS_INSTALL_BUILD_ERROR=true ; \
     } <<EOF
#! /bin/sh
#

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

GLOBUS_SETUP_PROG=\`echo \$0 | sed -e 's|.*/\([^/]*\)|\1|g'\`
install_prefix=${GLOBUS_INSTALL_PREFIX}

GLOBUS_SETP_BUILD_ERROR=false

print_usage ()
{
     echo ""
     echo "usage: \${GLOBUS_SETUP_PROG} [-classic]"
     echo ""
     echo "This script performs final site-setup operations after installation."
     echo ""
     echo "The -classic flag can be specified to force the MDS system to"
     echo " function as it did in the 1.1.2 release."
     echo ""
     exit 1

  GLOBUS_SETUP_BUILD_ERROR=true
}

bailout ()
{
  echo "abort!"
  echo "\${GLOBUS_SETUP_PROG} interrupted."
  echo "Setup may be incomplete!"
  exit 1
}

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

# set defaults for platform
if [ -x \${install_prefix}/sbin/config.guess ]
then
  GLOBUS_BUILD_PLATFORM=\`\${install_prefix}/sbin/config.guess\`
else
  echo "Cannot find file \"\${install_prefix}/sbin/config.guess\"!"
  echo "Cannot perform site setup for this installation."
  GLOBUS_BUILD_PLATFORM=""
  GLOBUS_SETUP_BUILD_ERROR=true
fi

# process user arguments
if [ "\$#" -gt 1 ]; then
    # print usage and bail..
    print_usage
fi

if [ X\`whoami\` = "Xroot" ]
then
   echo "Do not perform globus setup as root!  See Globus documents for more information."
   print_usage
fi

# find setup bindirs for this architecture
if [ ! "X\${GLOBUS_SETUP_BUILD_ERROR}" = "Xtrue" ]
then
  if [ -d \${install_prefix}/services/\${GLOBUS_BUILD_PLATFORM}/sbin ]
  then
    GLOBUS_SETUP_SBINDIR=\${install_prefix}/services/\${GLOBUS_BUILD_PLATFORM}/sbin
  elif [ -d \${install_prefix}/tools/\${GLOBUS_BUILD_PLATFORM}/sbin ]
  then 
    GLOBUS_SETUP_SBINDIR=\${install_prefix}/tools/\${GLOBUS_BUILD_PLATFORM}/sbin
  else
    GLOBUS_SETUP_SBINDIR=""
    echo "No tools or services installed for this architecture!"
    echo "Cannot perform site setup from this architecture."
    GLOBUS_SETUP_BUILD_ERROR=true
  fi
fi

if [ ! "X\${GLOBUS_SETUP_BUILD_ERROR}" = "Xtrue" ]
then
  if [ -x \${GLOBUS_SETUP_SBINDIR}/grid-info-config ]
  then
    if \${GLOBUS_SETUP_SBINDIR}/grid-info-config "\$@"
    then
      :
    else
      GLOBUS_SETUP_BUILD_ERROR=true
    fi
  else
    echo "Cannot find file \"\${GLOBUS_SETUP_SBINDIR}/grid-info-config\"!"
    echo "Cannot perform site setup from this architecture."
    GLOBUS_SETUP_BUILD_ERROR=true
  fi
fi

if [ ! "X\${GLOBUS_SETUP_BUILD_ERROR}" = "Xtrue" ]
then
  if [ -x \${GLOBUS_SETUP_SBINDIR}/grid-security-config ]
  then
    if \${GLOBUS_SETUP_SBINDIR}/grid-security-config "\$@"
    then
      :
    else
      GLOBUS_SETUP_BUILD_ERROR=true
    fi
  else
    echo "Cannot find file \"\${GLOBUS_SETUP_SBINDIR}/grid-security-config\"!"
    echo "Cannot perform site setup from this architecture."
    GLOBUS_SETUP_BUILD_ERROR=true
  fi
fi

if [ "X\${GLOBUS_SETUP_BUILD_ERROR}" = "Xtrue" ]
then
      trap - 1 2 3 6 15 2> /dev/null
      exit 1
else
      echo "The setup is complete."
      trap - 1 2 3 6 15 2> /dev/null
      exit 0
fi

EOF
  fi
  ###########################
}

#
# first let's build the services trees
#
if [ "X${GLOBUS_INSTALL_SERVICES}" = "Xtrue" ]
then
	
	echo ""
	
	build_label="services/${GLOBUS_BUILD_PLATFORM}"
	build_log_base=globus-build/services/${GLOBUS_BUILD_PLATFORM}
	build_dir=globus-build/services/${GLOBUS_BUILD_PLATFORM}
	
	build_make_target="headers libraries executables"
	build_install_target="install"
	
	prefix=${GLOBUS_INSTALL_PREFIX}
        # this is supposed to be BUILD_PLATFORM... we don't distinguish
        # bitness in services
        exec_prefix=${GLOBUS_INSTALL_PREFIX}/services/${GLOBUS_BUILD_PLATFORM}
	debug_opt="--enable-debug"
	threads_opt="--without-threads"
	protocol_opt=""
	shm_opt=""
	
	save_parallel_build=$parallel_build
	parallel_build=false
	do_build services_finalize
	parallel_build=$save_parallel_build
fi

#
# now let's build the tools tree
#
if [ "X${GLOBUS_INSTALL_TOOLS}" = "Xtrue" ]
then

     echo ""

     build_label="tools/${GLOBUS_BUILD_PLATFORM}"
     build_log_base=globus-build/tools/${GLOBUS_BUILD_PLATFORM}
     build_dir=globus-build/tools/${GLOBUS_BUILD_PLATFORM}

     build_make_target="headers libraries executables"
     build_install_target="install"

     prefix=${GLOBUS_INSTALL_PREFIX}
     # this is supposed to be BUILD_PLATFORM
     # we don't distinguish bitness in tools
     exec_prefix=${GLOBUS_INSTALL_PREFIX}/tools/${GLOBUS_BUILD_PLATFORM}
     jm_opt=""
     debug_opt="--enable-debug"

     case ${GLOBUS_OPT_TOOL_THREADS} in
        solaristhreads)
           threads_opt="--with-threads=solaristhreads"
           ;;
        pthreads)
           threads_opt="--with-threads=pthreads"
           ;;
        sproc)
           threads_opt="--with-threads=sproc"
           ;;
        *)
           threads_opt="--without-threads"
           ;;
     esac


     protocol_opt=""
     shm_opt=""

     do_build tools_finalize
fi


#   GLOBUS_OPT_THREADS:     lite, pthreads, solaristhreads, ""
#
#   GLOBUS_OPT_MPL
#   GLOBUS_OPT_MPI
#   GLOBUS_OPT_SHM
#   GLOBUS_OPT_INX 
#                           true if specific protocol modules requested
#
#   GLOBUS_OPT_DEBUG        true if debug requested

#  flavor naming:
#  ${GLOBUS_INSTALL_PREFIX}/development/${GLOBUS_INSTALL_ARCH}_threads_protocol_debug
#

build_proto_mp_flavs ()
{
  # incoming vars:
  #   threads_opt     -use for config
  #   prefix          -append proto_debug suffix

  if [ "X${GLOBUS_OPT_DEBUG}" = "Xtrue" ]
  then
     debug_opt="--enable-debug"
     debug_suff="_debug"
  else
     debug_opt=""
     debug_suff=""
  fi
  if [ "X${INSURE_CMD}" = "Xtrue" ]
  then
     debug_suff="${debug_suff}_insure"
  fi

  # conditionally build standard flavor
  if [ "X${GLOBUS_OPT_STD}" = "Xtrue" ] || \
     [ "X${GLOBUS_OPT_STD}" = "X" ]
  then
     build_standard=true
  else
     build_standard=false
  fi

  if [ "X${build_standard}" = "Xtrue" ]
  then
     flavor=${prefix_base}standard${debug_suff}
     prefix=${GLOBUS_INSTALL_PREFIX}/${flavor}
     exec_prefix="${prefix}"
     build_label="${flavor}"
     build_log_base=globus-build/${flavor}
     build_dir=globus-build/${flavor}
 
     if [ "X${GLOBUS_INSTALL_DEVEL_TESTS}" = "Xtrue" ]
     then
       build_make_target="all"
       build_install_target="install"
     else
       build_make_target="headers libraries"
       build_install_target="install_headers_all install_libraries_all"
     fi

     jm_opt=""
     protocol_opt=""

     do_build development_finalize
  fi
  # end build of standard flavor

  # conditionally build MPI
  if [ "X${GLOBUS_OPT_MPI}" = "Xtrue" ] || \
     [ "X${GLOBUS_OPT_MPI}" = "X" ]
  then
     case "${GLOBUS_INSTALL_ARCH}" in
        *-ibm-aix* )
            poepackage="`/usr/bin/lslpp -clq | grep '/usr/lib/objrepos.*Parallel Operating Environment' | wc -l`"
            case "$poepackage" in
                *1 )
                    build_mpi=true;
                ;;
                * )
                    if [ "X${GLOBUS_OPT_MPI}" = "Xtrue" ]
                    then
                       echo "Detection of AIX MPI development tools failed, skipping MPI build"
                    fi
                    build_mpi=false
                ;;
            esac
        ;;
        mips-sgi-irix* )
	    if versions -I -b mpi | grep mpi >/dev/null 2>&1; then
		build_mpi=true
	    elif which mpirun >/dev/null 2>&1; then
		mpi_basedir=`which mpirun | sed "s/\/usr\/bin\/mpirun//"`
		versions_stdout=`versions -r $mpi_basedir -I -b mpi 2>/dev/null | grep " mpi "`
		if [ "X${versions_stdout}" = "X" ]; then
		    build_mpi=false
		else
		    build_mpi=true
		fi
	    else
               if [ "X${GLOBUS_OPT_MPI}" = "Xtrue" ]
               then
                 echo "Detection of Irix MPI development tools failed, skipping MPI build"
               fi
		build_mpi=false
	    fi
        ;;
        alpha*-cray-unicosmk* )
            build_mpi=true
        ;;
        *-hp-hpux10* | *-hp-hpux11* )
            build_mpi=true
        ;;
        i860-intel-osf* )
            build_mpi=true
        ;;
        * )
           if [ "X${GLOBUS_OPT_MPI}" = "Xtrue" ]
           then
              echo "No support for MPI exists for this platform!"
              echo "Skipping MPI build"
           fi
            build_mpi=false
        ;;
     esac
  else
     build_mpi=false
  fi

  case "${GLOBUS_INSTALL_ARCH}" in
        *-ibm-aix* )
	    if [ "$threads_opt" != "--without-threads" ] && \
               [ "$build_mpi" = "true" ] && \
               [ ! -x /usr/bin/mpcc_r ]; then
                echo "Thread-safe MPI libraries not available, skipping threaded MPI build"
                build_mpi=false
            fi
            ;;
	*-hp* )
	    if [ "$threads_opt" != "--without-threads" ] && \
	       [ "$build_mpi" = "true" ] ; then
	        echo "Thread-safe MPI compiler not available, skipping threaded MPI build"
                build_mpi=false
	    fi
	    ;;
  esac

  if [ "${build_mpi}" = "true" ]
  then
     echo ""

     flavor=${prefix_base}mpi${debug_suff}
     prefix=${GLOBUS_INSTALL_PREFIX}/${flavor}
     exec_prefix="${prefix}"
     build_label="${flavor}"
     build_log_base=globus-build/${flavor}
     build_dir=globus-build/${flavor}
 
     if [ "X${GLOBUS_INSTALL_DEVEL_TESTS}" = "Xtrue" ]
     then
       build_make_target="all"
       build_install_target="install"
     else
       build_make_target="headers libraries"
       build_install_target="install_headers_all install_libraries_all"
     fi

     jm_opt=""
     protocol_opt="--with-mpi"

     do_build development_finalize
  fi
  # end build of MPI

  # conditionally build MPL
  if [ "X${GLOBUS_OPT_MPL}" = "Xtrue" ] || \
     [ "X${GLOBUS_OPT_MPL}" = "X" ]
  then
     case "${GLOBUS_INSTALL_ARCH}" in
        *-ibm-aix* )
            poepackage="`/usr/bin/lslpp -clq | grep '/usr/lib/objrepos.*Parallel Operating Environment' | wc -l`"
            case "$poepackage" in
                *1 )
                    build_mpl=true
                ;;
                * )
           if [ "X${GLOBUS_OPT_MPL}" = "Xtrue" ]
           then
              echo "Detection of AIX MPL development tools failed, skipping MPL build"
           fi
                    build_mpl=false
                ;;
            esac
        ;;
        * )
           if [ "X${GLOBUS_OPT_MPL}" = "Xtrue" ]
           then
              echo "No support for MPL exists for this platform!"
              echo "Skipping MPL build"
           fi
            build_mpl=false
        ;;
     esac
  else
     build_mpl=false
  fi

  case "${GLOBUS_INSTALL_ARCH}" in
        *-ibm-aix* )
	    if [ "$threads_opt" != "--without-threads" ] && \
               [ "$build_mpl" ]; then
		build_mpl=false
            fi
        ;;
  esac

  if [ "${build_mpl}" = "true" ]
  then
     echo ""

     flavor=${prefix_base}mpl${debug_suff}
     prefix=${GLOBUS_INSTALL_PREFIX}/${flavor}
     exec_prefix="${prefix}"
     build_label="${flavor}"
     build_log_base=globus-build/${flavor}
     build_dir=globus-build/${flavor}
 
     if [ "X${GLOBUS_INSTALL_DEVEL_TESTS}" = "Xtrue" ]
     then
       build_make_target="all"
       build_install_target="install"
     else
       build_make_target="headers libraries"
       build_install_target="install_headers_all install_libraries_all"
     fi

     jm_opt=""
     protocol_opt="--with-mpl"

     do_build development_finalize
  fi
  # end build of MPL

  # conditionally build INX
  if [ "X${GLOBUS_OPT_INX}" = "Xtrue" ] || \
     [ "X${GLOBUS_OPT_INX}" = "X" ]
  then
     case "$target" in
        i860-intel-osf* )
            build_inx=true
        ;;
        * )
           if [ "X${GLOBUS_OPT_INX}" = "Xtrue" ]
           then
              echo "No support for INX exists for this platform!"
              echo "Skipping INX build"
           fi
            build_inx=false
        ;;
     esac
  else
     build_inx=false
  fi

  if [ "${build_inx}" = "true" ]
  then
     echo ""

     flavor=${prefix_base}inx${debug_suff}
     prefix=${GLOBUS_INSTALL_PREFIX}/${flavor}
     exec_prefix="${prefix}"
     build_label="${flavor}"
     build_log_base=globus-build/${flavor}
     build_dir=globus-build/${flavor}
  
     if [ "X${GLOBUS_INSTALL_DEVEL_TESTS}" = "Xtrue" ]
     then
       build_make_target="all"
       build_install_target="install"
     else
       build_make_target="headers libraries"
       build_install_target="install_headers_all install_libraries_all"
     fi

     jm_opt=""
     protocol_opt="--with-inx"

     do_build development_finalize
  fi
  # end build of INX

}

build_proto_flavs ()
{

  # build MP proto flavs
  shm_opt=""

  build_proto_mp_flavs

  echo ""

  if [ "X${GLOBUS_OPT_SHM}" = "Xtrue" ]
  then
     # build second set of SHM+MP proto flavs
     prefix_base=${prefix_base}shm-
     shm_opt="--with-shm"

     build_proto_mp_flavs
  fi

}

#
# now let's build the devel tree
#
if [ "X${GLOBUS_INSTALL_DEVELOPMENT}" = "Xtrue" ]
then


 if [ "X${GLOBUS_INSTALL_PROMPT}" = "Xtrue" ]
 then 
   echo ""

   prompt_message="Install application development files?"
   prompt_default="yes"
   prompt_user
   build_now=${prompt_user_response}
 else
   build_now=yes
 fi

 if [ "X${build_now}" = "Xyes" ]
 then

   # conditionally build w/ threads
   if [ "X${GLOBUS_OPT_THREADS}" = "X" ] || \
      [ ! "X${GLOBUS_OPT_THREADS}" = "Xlite" ]
   then

     if [ "X${GLOBUS_OPT_THREADS}" = "X" ] || \
        [ "X${GLOBUS_OPT_THREADS}" = "Xthreads_only" ]
     then
        case ${GLOBUS_INSTALL_ARCH} in
	   *solaris2.5.1 | *solaris2.6* | *solaris2.7*)
	      threads_flavors=solaristhreads,pthreads
              ;;
           *solaris*)
              threads_flavors=solaristhreads
              ;;
	   *irix*)
	      threads_flavors=pthreads,sproc
              ;;
           *)
              threads_flavors=pthreads
              ;;
        esac
     else
        threads_flavors="${GLOBUS_OPT_THREADS}"
     fi

     # change IFS to parse comma-separated threads list
     save_IFS="${IFS}"
     IFS=", "
     for threads_flavor in ${threads_flavors}
     do
        IFS="${save_IFS}"

        echo ""

        threads_opt="--with-threads=${threads_flavor}"
        prefix_base=development/${GLOBUS_INSTALL_ARCH}_${threads_flavor}_

        # SHM currently not available w/ threads
        shm_opt=""

        build_proto_mp_flavs

        IFS=", "
     done
     IFS="${save_IFS}"
   fi

   # conditionally build w/o threads
   if [ "X${GLOBUS_OPT_THREADS}" = "X" ] || \
      [ "X${GLOBUS_OPT_THREADS}" = "Xlite" ]
   then

     echo ""

     threads_opt="--without-threads"
     prefix_base=development/${GLOBUS_INSTALL_ARCH}_nothreads_

     build_proto_flavs

   fi
 else
   echo "Skipping installation of application development files."
 fi

fi

# wait for any background tasks...
wait

echo ""
echo "Globus auto install exiting..."

GLOBUS_INSTALL_BUILD_ERROR=false

for attempt in ${GLOBUS_INSTALL_ATTEMPTS}
do
   if [ "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]
   then
      if [ ! -r ${attempt}/globus-software-summary.dryrun ]
      then
         echo "Dryrun ${attempt} failed"
         GLOBUS_INSTALL_BUILD_ERROR=true
      fi
   else
      if [ ! -r ${attempt}/globus-software-summary ]
      then
         echo "Build ${attempt} failed"
         GLOBUS_INSTALL_BUILD_ERROR=true
      fi
   fi
done


if [ "X${GLOBUS_INSTALL_BUILD_ERROR}" = "Xtrue" ]
then
   if [ "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]
   then
      echo "There were errors in the dryrun.  A real build would have failed."
   else
      echo "There were errors, so your installation may not be complete."
   fi
   exit 1
else
   if [ "X${GLOBUS_INSTALL_SIMULATE}" = "Xtrue" ]
   then
      echo "The dryrun is complete."
   else
      echo "The installation is complete."
      trap - 1 2 3 6 15 2> /dev/null
   fi
   exit 0
fi


