#!/bin/bash

# Helper script for the MyProxy service publisher
# Argument is init to set variables
#             status to check the status

case "$1" in
    init)

# These can be overridden by external definitions of the variables,
# e.g. if the advertised host name should be an alias

    MYPROXY_CONF=${MYPROXY_CONF:-/etc/myproxy-server.config}
    MYPROXY_HOST=${MYPROXY_HOST:-`hostname -f`}
    MYPROXY_PORT=${MYPROXY_PORT:-7512}

# Write to stdout - will be imported by the info provider

    echo MYPROXY_CONF=$MYPROXY_CONF
    echo MYPROXY_HOST=$MYPROXY_HOST
    echo MYPROXY_PORT=$MYPROXY_PORT

# myproxy doesn't seem to have a pid file, but the init.d script
# does create and remove this lock file

    echo MYPROXY_PID_FILE=/var/lock/subsys/myproxy-server

    exit
    ;;

    status)

    stat1=`test -f /var/lock/subsys/myproxy-server`
    rc1=$?

    stat2=`netstat -an --inet --inet6 | grep "^tcp .* \(::\|0.0.0.0\):${MYPROXY_PORT:-7512} .*LISTEN"`
    rc2=$?

    echo -n "The MyProxy server is "
    [[ $rc1 != 0 ]] && echo -n "not "
    echo -n "running and is "
    [[ $rc2 != 0 ]] && echo -n "not "
    echo "listening"
    [[ ($rc1 == 0) && ($rc2 == 0) ]]
    exit $?

    ;;
esac

echo "Usage: glite-info-service-myproxy <init|status>"
exit 1
