#!/bin/bash
#
# Init file for the NorduGrid gridftp-server
#
# chkconfig: 2345 55 25
# description: NorduGrid gridftp-server
#
# config: /etc/sysconfig/globus
# config: /etc/sysconfig/nordugrid
# config: /opt/nordugrid/etc/nordugrid.conf
# config: /etc/nordugrid.conf

# source function library
. /etc/rc.d/init.d/functions

###############  Standard Globus configuration  ######################

if [ -f /etc/sysconfig/globus ]; then
    . /etc/sysconfig/globus
fi

GLOBUS_LOCATION=${GLOBUS_LOCATION:-/opt/globus}

if [ ! -d "$GLOBUS_LOCATION" ]; then
  echo "GLOBUS_LOCATION not found" 
  exit 1
fi
LD_LIBRARY_PATH=$GLOBUS_LOCATION/lib:$LD_LIBRARY_PATH
export GLOBUS_LOCATION LD_LIBRARY_PATH


GRID_FUNCTIONS=${GRID_FUNCTIONS:-/etc/rc.d/init.d/grid-functions}
if [ ! -r "$GRID_FUNCTIONS" ]; then
  echo "grid functions ($GRID_FUNCTIONS) not available"
  exit 1
fi
. $GRID_FUNCTIONS

#  GLOBUS_CONFIG=${GLOBUS_CONFIG:-/etc/globus.conf}
#
#  if [ ! -r "$GLOBUS_CONFIG" ]; then
#    echo "Globus configuration not found (usually /etc/globus.conf)"
#  fi

###############  Standard NorduGrid configuration  ######################

if [ -f /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
fi

NORDUGRID_LOCATION=${NORDUGRID_LOCATION:-/opt/nordugrid}

if [ ! -d "$NORDUGRID_LOCATION" ]; then
  echo "NORDUGRID_LOCATION not found" 
  exit 1
fi
export NORDUGRID_LOCATION

if [ -z "$NORDUGRID_CONFIG" ]; then
  if [ -r $NORDUGRID_LOCATION/etc/nordugrid.conf ]; then
    NORDUGRID_CONFIG=$NORDUGRID_LOCATION/etc/nordugrid.conf
  elif [ -r /etc/nordugrid.conf ]; then
    NORDUGRID_CONFIG=/etc/nordugrid.conf
  fi
fi

if [ ! -r "$NORDUGRID_CONFIG" ]; then

   if [ ! -r $NORDUGRID_LOCATION/etc/gridftp-server.conf ]; then
     echo "NorduGrid configuration not found (usually /etc/nordugrid.conf)"
     exit 1
   else
     echo "NorduGrid common configuration not found (usually /etc/nordugrid.conf)"
     echo "  using low level configuration instead."
   fi
fi

######################################################################

#
# Get NORDUGRID_ETC_LOCATION
#
# Search order:
#  1. Environment
#  2. nordugrid.conf [common] : nordugrid_etc_location
#  3. NORDUGRID_LOCATION/etc
#
unset nordugrid_etc_location
conf_read common
NORDUGRID_ETC_LOCATION=${NORDUGRID_ETC_LOCATION:-$nordugrid_etc_location}
NORDUGRID_ETC_LOCATION=${NORDUGRID_ETC_LOCATION:-$NORDUGRID_LOCATION/etc}

if test -d "$NORDUGRID_ETC_LOCATION"; then
  :
else
  mkdir -p "$NORDUGRID_ETC_LOCATION"
fi

function std_header()
{
    echo '# This file was automatically generated by nordugrid startup script. Do not modify.' > $1
    echo >> $1
}

function create_gridftp_config () {

if [ -r $NORDUGRID_CONFIG ]; then
    pluginpath="$NORDUGRID_LOCATION/lib"

    encryption="no"

    conf_read gridftp-server

    std_header $NORDUGRID_ETC_LOCATION/gridftp-server.conf

    (

    test "x$pluginpath"   = "x" || echo "pluginpath $pluginpath"
    test "x$encryption"   = "x" || echo "encryption $encryption"
    test "x$allowunknown" = "x" || echo "allowunknown $allowunknown"
    
    for i in `conf_entries gridftp-server`; do
    (
        path="/$i"
        plugin=""
        mount=""
        dir=""
	conf_read gridftp-server/$i
	if [ -n "$path" -a -n "$plugin" ]; then
          echo
          echo "# '`basename $i`' plugin"
          echo "plugin $path $plugin"
      	  [ -n "$mount" ] && echo "  mount $mount"

          i=0
          tempvar=$dir
          while [ ! -z "$tempvar" ]; do
            echo "  dir $tempvar"
            i=$[$i+1]
            tempvar=`eval echo '$dir'_$i`
          done

          echo "end"
        fi                
    )
    done

    ) >> $NORDUGRID_ETC_LOCATION/gridftp-server.conf

fi

}

lockfile=/var/lock/subsys/gridftp-server

start()
{
    if [ -r "$NORDUGRID_CONFIG" ]; then
	conf_init
	conf_open $NORDUGRID_CONFIG
    fi

    if conf_match gridftp-server; then
      create_gridftp_config

      # Defaults

      user=""
      port="2811"
      connections=""
      config="$NORDUGRID_ETC_LOCATION/gridftp-server.conf"

      conf_read gridftp-server
      
      GFS_USER="$user"
      GFS_PORT="$port"
      GFS_CONNECTIONS="$connections"
      GFS_CONFIG="$config"

      $NORDUGRID_LOCATION/sbin/gridftp-server start
      RETVAL=$?
      if [ $RETVAL -ne 0 ]; then
        failure "gridftp-server startup"
        conf_close
        return $RETVAL
      fi
      touch $lockfile

    fi
    conf_close
}

stop()
{
    if [ -x $NORDUGRID_LOCATION/sbin/gridftp-server ]; then
      $NORDUGRID_LOCATION/sbin/gridftp-server stop
      RETVAL=$?
    fi
    rm -f $lockfile

    return $RETVAL
}

restart()
{
    stop
    start start
    RETVAL=$?
    return $RETVAL
}

# See how we were called.
case "$1" in
  start|lightcleanstart|cleanstart|distcleanstart)
        start $1
        ;;
  stop)
        stop
        ;;
  status)
	status gridftp-server
        ;;
  restart|reload)
        restart
        ;;
  condrestart)
        [ -f $lockfile ] && restart
        ;;
  *)
        echo "Usage: $0 {start|stop|status|restart|reload|condrestart}"
        exit 1
esac

exit $?
