#!/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

send_systemd_notify() {
    # return if no systemd-notify found
    type systemd-notify >/dev/null 2>&1 || return
    systemd-notify "$@"
}

log_failure_msg() {
    send_systemd_notify --status "Error: $@"
    echo $@
}

# 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/arc-${prog} ]; then
    . /etc/sysconfig/arc-${prog}
elif [ -r /etc/default/arc-${prog} ]; then
    . /etc/default/arc-${prog}
fi

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

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

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

readorigconfigvar() {
    value=`$ARC_LOCATION/libexec/arc/arcconfig-parser --config "$1" -b "$3" -o "$2" 2>/dev/null`
    if [ $? -eq 0 ] ; then
        echo "$value"
        exit 0
    else
        exit 1
    fi
}

readconfigvar() {
    fname="$1"
    optname="$2"
    blocks=""
    while [ ! -z "$3" ] ; do
        blocks="$blocks -b $3"
        shift
    done
    value=`$ARC_LOCATION/libexec/arc/arcconfig-parser --runconfig "$fname" --load $blocks -o "$optname" 2>/dev/null`
    if [ $? -eq 0 ] ; then
        echo "$value"
        exit 0
    else
        exit 1
    fi
}

check_cert() {
  X509_USER_CERT=`readconfigvar "$ARC_RUNTIME_CONFIG" x509_host_cert gridftpd common`
  X509_USER_KEY=`readconfigvar "$ARC_RUNTIME_CONFIG" x509_host_key gridftpd common`
  if [ ! -f "$X509_USER_CERT" ] ; then
    log_failure_msg "Host certificate $X509_USER_CERT is not found"
    exit 1
  fi
  if [ ! -f "$X509_USER_KEY" ] ; then
    log_failure_msg "Host key $X509_USER_KEY is not found"
    exit 1
  fi
  # check permissions on key
  perms=`stat -L -c %a "$X509_USER_KEY"`
  if [ "$perms" != "600" ] && [ "$perms" != "400" ] ; then
    log_failure_msg "Host key must be readable only by user"
    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
    log_failure_msg "ARC configuration not found (usually /etc/arc.conf)"
    exit 1
fi

# 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
PID_FILE=`readorigconfigvar "$ARC_CONFIG" pidfile gridftpd`
if [ "x$PID_FILE" = "x" ]; then
    # Missing default value for pidfile means no service block is present
    log_failure_msg "ARC configuration is missing [gridftpd] block"
    exit 1
fi

if [ "$1" = "--getpidfile" ] ; then
    echo $PID_FILE
    exit 0
fi

ARC_RUNTIME_CONFIG=`echo "$PID_FILE" | sed 's#\([^\./]*\)\.[^\./]*$#\1#'`
ARC_RUNTIME_CONFIG="${ARC_RUNTIME_CONFIG}.cfg"

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

# Pre-process configuration
$ARC_LOCATION/libexec/arc/arcconfig-parser --config "$ARC_CONFIG" --runconfig "$ARC_RUNTIME_CONFIG" --save 2>/dev/null
if [ $? -ne 0 ] ; then
    log_failure_msg "ARC configuration processing failed"
    exit 1
fi

CMD="$CMD -c $ARC_RUNTIME_CONFIG"
CMD="$CMD -P $PID_FILE"

LOGFILE=`readconfigvar "$ARC_RUNTIME_CONFIG" logfile gridftpd`
USERNAME=`readconfigvar "$ARC_RUNTIME_CONFIG" user gridftpd`
if [ ! -d `dirname "$LOGFILE"` ]; then
    mkdir -p `dirname "$LOGFILE"`
    if [ ! -z "$USERNAME" ] ; then
        chown "$USERNAME" `dirname "$LOGFILE"`
    fi
fi
if [ ! -z "$USERNAME" ] ; then
    if [ -f "$LOGFILE" ] ; then
        chown "$USERNAME" "$LOGFILE"
    fi
fi

check_cert

exec $CMD "$@"
