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

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

# Needed to get pid file
readconfigvar() {
    fname=$1
    if [ ! -r "$fname" ]; then
        return
    fi
    bname="[$2]"
    vname=$3
    value=
    cat "$fname" | grep -e '^\[' -e "^${vname}[[:space:]]*=" | {
        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/.*//'`
                    vlname=`eval "echo $vlname"`
                    if [ "$vlname" = "$vname" ] ; then
                        val=`echo "$line" | sed 's/[^=]*=//'`
                        eval "echo $val"
                        return
                    fi
                done
            fi
        done
    }
}

# 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=`readconfigvar "$ARC_CONFIG" common pidfile`

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

LOG_FILE=`readconfigvar "$ARC_CONFIG" common logfile`
if [ "x$LOG_FILE" = "x" ]; then
    LOG_FILE=/var/log/arc/datadelivery-service.log
fi

prepare() {

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

    if [ ! -r "$ARC_CONFIG" ]; then
        echo "ARC configuration not found (usually /etc/arc.conf)"
        exit 1
    fi
    
    # check that at least service is defined
    grep -e '^\[datadelivery-service\]' $ARC_CONFIG 1>/dev/null 2>&1
    if [ $? -ne 0 ] ; then
        echo "Data delivery service not defined in configuration"
        exit 1
    fi
    
    # check that if service is insecure no allowed_dns are defined
    SECURE=`readconfigvar "$ARC_CONFIG" common secure`
    ALLOWEDDN=`readconfigvar "$ARC_CONFIG" datadelivery-service allowed_dn`
    if [ "$SECURE" = "no" ]; then
        if [ "x$ALLOWEDDN" != "x" ]; then
            echo "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
}

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."
    return 0
fi

prepare

exec "$CMD"
