#! /bin/sh

. ${GLOBUS_LOCATION}/libexec/globus-script-initializer
. ${GLOBUS_LOCATION}/libexec/globus-sh-tools.sh


PROGRAM_NAME=`echo $0 | ${GLOBUS_SH_SED-sed} -e 's|.*/||g'`
PROGRAM_VERSION=`echo '$Revision: 1.6 $' | ${GLOBUS_SH_CUT-cut} -d' ' -f2`


# Save the values from the environment 
h_save="$GRID_INFO_HOST"
p_save="$GRID_INFO_PORT"
T_save="$GRID_INFO_TIMEOUT"
b_save="$GRID_INFO_BASEDN"



# Read the default config file and fill in missing environment settings
. ${GLOBUS_LOCATION}/etc/grid-info.conf


return_precedent_value() {
    if [ -n "$1" ]; then
        echo "$1"
    else
        echo "$2"
    fi
}



# Set the variables with precedent going to the user environment 
GRID_INFO_HOST=`return_precedent_value "$h_save" "$GRID_INFO_HOST"`
GRID_INFO_PORT=`return_precedent_value "$p_save" "$GRID_INFO_PORT"`
GRID_INFO_TIMEOUT=`return_precedent_value "$T_save" "$GRID_INFO_TIMEOUT"`
GRID_INFO_BASEDN=`return_precedent_value "$b_save" "$GRID_INFO_BASEDN"`


GRID_INFO_CONFIG_FILE="${sysconfdir}/grid-info.conf"


export GRID_INFO_HOST GRID_INFO_PORT GRID_INFO_TIMEOUT GRID_INFO_BASEDN


MDSCOMMAND="${libexecdir}/grid-info-ldapsearch"


# by default, we pass the output through no filter
POST_FILTER="${GLOBUS_SH_CAT-cat}"


SCRIPT_TMP=${tmpdir}/$$_tmp
SCRIPT_ERR=${tmpdir}/$$_err
MDS_INPUT=${tmpdir}/$$_input
MDS_OUTPUT=${tmpdir}/$$_output


cleanup () {
  test -z "$SCRIPT_TMP" || ${GLOBUS_SH_RM-rm} -f $SCRIPT_TMP >/dev/null 2>&1
  test -z "$SCRIPT_ERR" || ${GLOBUS_SH_RM-rm} -f $SCRIPT_ERR >/dev/null 2>&1
  test -z "$MDS_INPUT"  || ${GLOBUS_SH_RM-rm} -f $MDS_INPUT >/dev/null 2>&1
  test -z "$MDS_OUTPUT" || ${GLOBUS_SH_RM-rm} -f $MDS_OUTPUT >/dev/null 2>&1
}



##################################################
##################################################
# USAGE: grid-info-search 
#


short_usage="$PROGRAM_NAME [ options ] <search filter> [attributes]"


long_usage () {
  ${GLOBUS_SH_CAT-cat} >&2 <<EOF
${short_usage}

    Searches the MDS server based on the search filter, where some
    options are:
       -help       Displays this message
       -version    Displays the current version number
       -config file
                   Specifies a different configuration file
                   to obtain MDS defaults.
                   Note that the option overrides the variables
                   set in the users environment and previously
                   listed command line options.
       -mdshost host (-h)
                   The host name on which the MDS server is running
                   the default is $GRID_INFO_HOST
       -mdsport port (-p)
                   The port number on which the MDS server is running
                   The default is port $GRID_INFO_PORT
       -mdsbasedn branch-point (-b)
                   Location in DIT from which to start the search
                   The default is "${GRID_INFO_BASEDN}"
       -mdstimeout seconds (-T)
                   The amount of time (in seconds) one should allow to
                   wait on an MDS request. The default is $GRID_INFO_TIMEOUT
       -anonymous (-x)
                   Use anonymous binding instead of GSSAPI.
       -nowrap     Passes the output through a line-unwrapping filter first.

     $PROGRAM_NAME also supports the full set of flags that are defined in
     the LDAP v3 standard.  Refer to the ldapsearch man page for a full
     description of all available options.
EOF
}


. ${GLOBUS_LOCATION}/libexec/globus-args-parser-header



##################################################
#       READ THE COMMAND LINE


GRID_INFO_AUTH="-Y GSI-GSSAPI"


PASS_ARGS=""


check_args () {
    while [ $# -gt 0 ]
    do
        case "$1" in
            -c | -config )
                GRID_INFO_CONFIG_FILE="$2"
                . "${GRID_INFO_CONFIG_FILE}"
                shift ; shift
                ;;
            -h | -mdshost)
                GRID_INFO_HOST="$2"
                shift ; shift
                ;;
            -p | -mdsport)
                GRID_INFO_PORT="$2"
                shift ; shift
                ;;
            -b | -mdsbasedn)
                GRID_INFO_BASEDN="$2"
                shift ; shift 
                ;;
            -T | -mdstimeout) 
                GRID_INFO_TIMEOUT="$2"
                shift ; shift
                ;;
            -dryrun)
                PASS_ARGS="${PASS_ARGS} -n"
                shift
                ;;
            -anonymous | -x)
                GRID_INFO_AUTH="-x"
                shift
                ;;
            -help | -usage)
                long_usage
                exit 1
                ;;
            -Y)
                PASS_ARGS="${PASS_ARGS} -Y"
                GRID_INFO_AUTH=
                shift
                ;;
            -nowrap)
                POST_FILTER='perl -e "\$_ = join(\"\",<>); s/\n //sg; print;"'
                shift
                ;;
            *)
                arg=`echo "$1" | sed -e 's/"/\\\\"/g'`
                PASS_ARGS="${PASS_ARGS} \"${arg}\""
                shift
                ;;
        esac
    done
}


check_args "$@"


PASS_ARGS="-h \"${GRID_INFO_HOST}\" ${PASS_ARGS}"



PASS_ARGS="-p \"${GRID_INFO_PORT}\" ${PASS_ARGS}"
PASS_ARGS="-b \"${GRID_INFO_BASEDN}\" ${PASS_ARGS}"
PASS_ARGS="-l \"${GRID_INFO_TIMEOUT}\" ${PASS_ARGS}"
PASS_ARGS="${GRID_INFO_AUTH} ${PASS_ARGS}"


# exec search command for efficiency and signalling convenience
eval "$MDSCOMMAND $PASS_ARGS > $MDS_OUTPUT"
rc_save=$?
eval "$POST_FILTER < $MDS_OUTPUT"
test -z "$MDS_OUTPUT" || ${GLOBUS_SH_RM-rm} -f $MDS_OUTPUT >/dev/null 2>&1
exit $rc_save
