#!/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=arched
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-datadelivery-service ]; then
    . /etc/sysconfig/arc-datadelivery-service
elif [ -r /etc/default/arc-datadelivery-service ]; then
    . /etc/default/arc-datadelivery-service
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/lib/arc/arcconfig-parser --config "$1" -b "$2" -o "$3" 2>/dev/null`
    if [ $? -eq 0 ] ; then
        echo "$value"
        exit 0
    else
        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

PID_FILE=`readorigconfigvar "$ARC_CONFIG" datadelivery-service pidfile`
if [ "x$PID_FILE" = "x" ]; then
    # Missing default value for pidfile means no service block is present
    log_failure_msg "ARC configuration is missing [datadelivery-service] block"
    exit 1
fi

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

LOG_FILE=`readorigconfigvar "$ARC_CONFIG" datadelivery-service logfile`
if [ "x$LOG_FILE" = "x" ]; then
    log_failure_msg "Log file could not be found in [datadelivery-service] block"
    exit 1
fi
if [ ! -d `dirname "$LOGFILE"` ]; then
    mkdir -p `dirname "$LOGFILE"`
fi

prepare() {

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

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

    # check that if service is insecure no allowed_dns are defined
    SECURE=`readorigconfigvar "$ARC_CONFIG" datadelivery-service secure`
    ALLOWEDDN=`readorigconfigvar "$ARC_CONFIG" datadelivery-service allowed_dn`
    if [ "$SECURE" = "no" ]; then
        if [ "x$ALLOWEDDN" != "x" ]; then
            log_failure_msg "allowed_dn cannot be used with secure=no"
            exit 1
        fi
    fi

    # Assuming ini style config
    CMD="$CMD -i $ARC_CONFIG -p $PID_FILE -l $LOG_FILE"

    add_library_path "$GLOBUS_LOCATION"
    if [ "x$LD_LIBRARY_PATH" = "x" ]; then
        LD_LIBRARY_PATH=$ARC_LOCATION/lib
    else
        LD_LIBRARY_PATH=$ARC_LOCATION/lib:$LD_LIBRARY_PATH
    fi
    export LD_LIBRARY_PATH
    cd /
}

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

prepare

exec $CMD "$@"
