#!/bin/bash

# Environment variables:
#
# GRID_FUNCTIONS
# NORDUGRID_CONFIG
# NORDUGRID_LOCATION
#

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

NORDUGRID_CONFIG=${NORDUGRID_CONFIG:-/etc/nordugrid.conf}

if ! test -r "$NORDUGRID_CONFIG"; then
  echo "Can not read $NORDUGRID_CONFIG"
  exit 1
fi

if test "x$1" = "x"; then
  conf_init
  conf_open $NORDUGRID_CONFIG
else
  conf_tmpdir=$1
fi

server=httpsd

# Defaults

user=""
debug=""

# Read the common section
conf_read common

# Server defaults
logfile="/var/log/$server.log"
port=""
gsiport=""
sslport=""

# Read the main server entry
conf_read $server

if [ `id -u` = 0 ] ; then
  pidfile="/var/run/$server.pid"
else
  pidfile="$HOME/$server.pid"
fi

cat <<-EOF
	# This file was automatically generated by nordugrid startup script. Do not modify.

	EOF

test "x$user"         = "x" || echo "user $user"
test "x$pidfile"      = "x" || echo "pidfile $pidfile"
test "x$logfile"      = "x" || echo "logfile $logfile"
test "x$debug"        = "x" || echo "debug $debug"

test "x$port"         = "x" || echo "port $port"
test "x$gsiport"      = "x" || echo "gsiport $gsiport"
test "x$sslport"      = "x" || echo "sslport $sslport"

for i in `conf_entries $server`; do
(
	storage="/$i"
	service=""
	dir=""
	conf_read $server/$i
	if [ -n "$path" -a -n "$service" ]; then
	    echo
	    echo "# '`basename $i`' service"
	    echo "service $service $storage"
            if test "x$service" = "se"; then
              test "x$path" = "x" || echo "path=$path"
              test "x$ns"   = "x" || echo "ns=$ns"
            elif test "x$service" = "logger"; then
              test "x$acl_read"   = "x" || echo "acl_read=$acl_read"
              test "x$acl_write"  = "x" || echo "acl_write=$acl_write"
              test "x$sqlcontact" = "x" || echo "sqlcontact=$sqlcontact"
              test "x$sqlcontactsource" = "x" || echo "sqlcontactsource=$sqlcontactsource"
            fi
	    echo "end"
	fi                
)
done

# Only close if we opened the config file
if test "x$1" = "x"; then
  conf_close
fi
