#!/bin/bash

if [ -z "$SEISCOMP_ROOT" ]; then
    echo "SEISCOMP_ROOT is not defined"
    exit 1
fi

pkgname="slmon"
PKGROOT="$SEISCOMP_ROOT/$pkgname"

export PATH="$PKGROOT/bin:$PATH"

if ! cd "$PKGROOT"; then
    echo "Cannot change directory to $PKGROOT"
    exit 1
fi

if [ ! -r "$SEISCOMP_ROOT/lib/env.sh" ]; then
    echo "Cannot read $SEISCOMP_ROOT/lib/env.sh"
    exit 1
fi

source "$SEISCOMP_ROOT/lib/env.sh"

if [ ! -r "$SEISCOMP_ROOT/lib/keyutils.sh" ]; then
    echo "Cannot read $SEISCOMP_ROOT/lib/keyutils.sh"
    exit 1
fi

source "$SEISCOMP_ROOT/lib/keyutils.sh"

global_cfg="templates/global.cfg"
if [ ! -r "$global_cfg" ]; then
    echo "Cannot read $PKGROOT/$global_cfg!"
    exit 1
fi

station_cfg="templates/station.cfg"
if [ ! -r "$station_cfg" ]; then
    echo "Cannot read $PKGROOT/$station_cfg!"
    exit 1
fi

VERSION="2.5"
KEY_VERSION="2.5"

_init_keys() {
    mkdir -p key

    ORGANIZATION=""
    
    if [ ! -f "$SEISCOMP_ROOT/key/global" ]; then
        echo "Cannot find $SEISCOMP_ROOT/key/global"
        return
    fi

    source "$SEISCOMP_ROOT/key/global"
    
    SEEDLINK="no"
    DIGISERV="no"
    SLMON_TITLE="$ORGANIZATION SeedLink Monitor"
    SLMON_EMAIL="geofon@gfz-potsdam.de"
    SLMON_GROUP="default"
}

_edit_globals() {
    _init_keys

    if [ -f "key/global" ]; then
        source key/global
    fi

    Ask SLMON_TITLE "Title" "$SLMON_TITLE"
    Ask SLMON_EMAIL "E-Mail" "$SLMON_EMAIL"
    
    OutputKeys $global_cfg >key/global
}
 
_edit_keys() {
    _init_keys
    
    if [ -f "$keyfile" ]; then
        source "$keyfile"
    fi

    Ask SLMON_GROUP "SLMon station group" "$SLMON_GROUP"

    OutputKeys $station_cfg >$keyfile
}

_get_keys() {
    pkg="$1"
    profile="$(echo -n "$PACKAGES" | sed -e "s/.*\<$pkg\>\(:\([^ ]*\)\)\?.*/\2/")"
    
    if [ -z "$profile" ]; then
        keyfile="$SEISCOMP_ROOT/$pkg/key/station_${NET}_${STATION}"
    else
        keyfile="$SEISCOMP_ROOT/$pkg/key/profile_$profile"
    fi

    if [ -f "$keyfile" ]; then
        source "$keyfile"
        return 0
    fi

    return 1
}

_write_conf() {
    LoadConfig "$global_cfg" "$station_cfg"

    _init_keys

    if [ ! -f "key/global" ]; then
        echo "Cannot find $PKGROOT/key/global"
        return
    fi

    source "key/global"

    if [ ! -f "$SEISCOMP_ROOT/acquisition/key/global" ]; then
        echo "Cannot find $SEISCOMP_ROOT/acquisition/key/global"
    fi
    
    source "$SEISCOMP_ROOT/acquisition/key/global"
    
    if [ "$DIGISERV" = yes ]; then
        LOCALPORT="$DIGISERV_PORT"
    elif [ "$SEEDLINK" = yes ]; then
        LOCALPORT="$SEEDLINK_PORT"
    else
        echo "You must enable local SeedLink server in order to use slmon!"
        return
    fi
    
    if [ "$SLARCHIVE" = no ]; then
        echo "You must enable slarchive in order to use slmon!"
        return
    fi
    
    SDS_ARCHIVE="$SEISCOMP_ROOT/acquisition/archive"
    
    mkdir -p config

    OutputFile templates/config.tpl >config/config.ini
    :>config/stations.ini

    for n in $SEISCOMP_ROOT/key/network_*; do
        if [ "$n" = "key/network_*" ]; then
            echo "No networks defined"
            break
        fi
        
        NET="${n##*_}"
        source $n

        echo "+ network $NET_DESC"
        
        for s in $SEISCOMP_ROOT/key/station_${NET}_*; do
            if [ "$s" = "key/station_${NET}_*" ]; then
                echo "No stations defined for network $NET"
                break
            fi

            STATION="${s##*_}"
            source $s

            echo "  + station $STAT_DESC"

            if echo $PACKAGES | grep -q "\<$pkgname\>"; then
                if ! _get_keys $pkgname; then
                    echo "    - cannot find $keyfile"
                    continue
                fi
            else
                echo "    - $pkgname not enabled for ${NET}_${STATION}"
                continue
            fi

            if ! echo $PACKAGES | grep -q "\<acquisition\>"; then
                echo "    - acquisition not enabled for ${NET}_${STATION} - disabling slmon"
                continue
            fi

            OutputFile templates/station.tpl >>config/stations.ini
        done
    done
}

action="$1"
shift

case "$action" in
    start|check)
        setsid bin/${pkgname}_start.sh
        exit $?
        ;;
    stop)
        bin/${pkgname}_stop.sh
        exit $?
        ;;
    get_attributes)
        echo "globals,profile,station,advanced"
        exit 0
        ;;
    edit_globals)
        if [ $# -ne 0 ]; then
            break
        fi
        _edit_globals
        exit 0
        ;;
    edit_profile)
        if [ $# -ne 1 ]; then
            break
        fi
        keyfile="key/profile_$1"
        _edit_keys
        exit 0
        ;;
    edit_station)
        if [ $# -ne 2 ]; then
            break
        fi
        keyfile="key/station_$1_$2"
        _edit_keys
        exit 0
        ;;
    write_conf)
        _write_conf
        exit 0
        ;;
    print_crontab)
        exit 0
        ;;
esac

echo "Error: $pkgname config hook called with invalid arguments"
exit 1

