#!/bin/bash

add_library_path() {
    location="$1"
    if [ ! "x$location" = "x" ] ; then
	if [ ! "$location" = "/usr" ] ; then
	    libdir="$location/lib"
	    libdir64="$location/lib64"
	    if [ -d "$libdir64" ] ; then
		if [ "x$LD_LIBRARY_PATH" = "x" ]; then
		    LD_LIBRARY_PATH="$libdir64"
		else
		    LD_LIBRARY_PATH="$libdir64:$LD_LIBRARY_PATH"
		fi
	    fi
	    if [ -d "$libdir" ] ; then
		if [ "x$LD_LIBRARY_PATH" = "x" ]; then
		    LD_LIBRARY_PATH="$libdir"
		else
		    LD_LIBRARY_PATH="$libdir:$LD_LIBRARY_PATH"
		fi
	    fi
	fi
    fi
}

prog=gridftpd
RUN=yes

# sysconfig files
if [ -r /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
elif [ -r /etc/default/nordugrid ]; then
    . /etc/default/nordugrid
fi
if [ -r /etc/sysconfig/${prog} ]; then
    . /etc/sysconfig/${prog}
elif [ -r /etc/default/${prog} ]; then
    . /etc/default/${prog}
fi

if [ "$RUN" != "yes" ] ; then
    echo "$prog disabled, please adjust the configuration to your"
    echo "needs and then set RUN to 'yes' in /etc/default/$prog to enable it."
    exit 0
fi

# GLOBUS_LOCATION
GLOBUS_LOCATION=${GLOBUS_LOCATION:-/usr}
if [ ! -d "$GLOBUS_LOCATION" ]; then
    echo "GLOBUS_LOCATION ($GLOBUS_LOCATION) not found"
    exit 1
fi
export GLOBUS_LOCATION

# ARC_LOCATION
ARC_LOCATION=${ARC_LOCATION:-/usr}
if [ ! -d "$ARC_LOCATION" ]; then
    echo "ARC_LOCATION ($ARC_LOCATION) not found"
    exit 1
fi
export ARC_LOCATION

readconfigvar() {
    fname=$1
    if [ ! -r "$fname" ]; then
	return
    fi
    bname="[$2]"
    vname=$3
    value=
    cat "$fname" | grep -e '^\[' -e "^${vname}=" | {
	while true; do
	    read line
	    if [ ! $? = 0 ] ; then
		return
	    fi
	    if [ "$line" = "$bname" ] ; then
		while true ; do
		    read line
		    if [ ! $? = 0 ] ; then
			return
		    fi
		    lstart=`echo "$line" | head -c 1`
		    if [ "$lstart" = '[' ] ; then
			return
		    fi
		    vlname=`echo "$line" | sed 's/=.*//;t;s/.*//'`
		    if [ "$vlname" = "$vname" ] ; then
			val=`echo "$line" | sed 's/[^=]*=//'`
			eval "echo $val"
			return
		    fi
		done
	    fi
	done
    }
}

check_cert() {
  X509_USER_CERT=`readconfigvar "$ARC_CONFIG" gridftpd x509_user_cert`
  X509_USER_KEY=`readconfigvar "$ARC_CONFIG" gridftpd x509_user_key`
  if [ -z "$X509_USER_CERT" ] ; then
    X509_USER_CERT=`readconfigvar "$ARC_CONFIG" common x509_user_cert`
  fi
  if [ -z "$X509_USER_KEY" ] ; then
    X509_USER_KEY=`readconfigvar "$ARC_CONFIG" common x509_user_key`
  fi
  if [ -z "$X509_USER_CERT" ] ; then
    X509_USER_CERT=/etc/grid-security/hostcert.pem
  fi
  if [ -z "$X509_USER_KEY" ] ; then
    X509_USER_KEY=/etc/grid-security/hostkey.pem
  fi
  if [ ! -f "$X509_USER_CERT" ] ; then
    echo "Host certificate not found"
    exit 1
  fi
  if [ ! -f "$X509_USER_KEY" ] ; then
    echo "Host key not found"
    exit 1
  fi
  # check permissions on key
  perms=`stat -L -c %a "$X509_USER_KEY"`
  if [ "$perms" != "600" ] && [ "$perms" != "400" ] ; then
    echo "Host key must be readable only by user"
    exit 1
  fi
}

CMD="$ARC_LOCATION/sbin/$prog"
if [ ! -x "$CMD" ]; then
    echo "Missing executable"
    exit 1
fi

# ARC_CONFIG
if [ "x$ARC_CONFIG" = "x" ]; then
    if [ -r $ARC_LOCATION/etc/arc.conf ]; then
	ARC_CONFIG=$ARC_LOCATION/etc/arc.conf
    elif [ -r /etc/arc.conf ]; then
	ARC_CONFIG=/etc/arc.conf
    fi
fi

if [ ! -r "$ARC_CONFIG" ]; then
    echo "ARC configuration not found (usually /etc/arc.conf)"
    exit 1
fi

CMD="$CMD -c '$ARC_CONFIG'"

# VOMS_LOCATION
VOMS_LOCATION=${VOMS_LOCATION:-@DEFAULT_VOMS_LOCATION@}

add_library_path "$VOMS_LOCATION"
add_library_path "$GLOBUS_LOCATION"
if [ "x$LD_LIBRARY_PATH" = "x" ]; then
    LD_LIBRARY_PATH=$ARC_LOCATION/lib64
else
    LD_LIBRARY_PATH=$ARC_LOCATION/lib64:$LD_LIBRARY_PATH
fi
export LD_LIBRARY_PATH

PID_FILE=`readconfigvar "$ARC_CONFIG" gridftpd pidfile`

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

logfile=`readconfigvar "$ARC_CONFIG" gridftpd logfile`
if [ "x$logfile" = "x" ]; then
    logfile=/var/log/arc/gridftpd.log
fi
if [ ! -d `dirname $logfile` ]; then
    mkdir -p `dirname $logfile`
fi

CMD="$CMD -P '$PID_FILE'"

check_cert

exec "$CMD"
