#!/bin/bash

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

pkgname="autopick"
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

    SEEDLINK="no"
    DIGISERV="no"
    DETEC_STREAM="BH"
    DETEC_LOCID=""
    DETEC_FILTER="bandpass 0.7 2.0 4"
    GAIN="0.6"
    STA_LEN="2"
    LTA_LEN="100"
    TRIG_ON="3"
    TRIG_OFF="1"
}

_edit_keys() {
    _init_keys
    
    if [ -f "$keyfile" ]; then
        source "$keyfile"
    fi

    echo "Selecting data stream for autopick"
    Ask DETEC_STREAM "Stream code (without component code)" "$DETEC_STREAM"
    Ask DETEC_LOCID "Location code" "$DETEC_LOCID"
    Ask DETEC_FILTER "Filter" "$DETEC_FILTER"
    Ask GAIN "Gain in counts/(nm/sec)" "$GAIN"
    Ask STA_LEN "STA length" "$STA_LEN"
    Ask LTA_LEN "LTA length" "$LTA_LEN"
    Ask TRIG_ON "Trigger on level" "$TRIG_ON"
    Ask TRIG_OFF "Trigger off level" "$TRIG_OFF"

    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 "$SEISCOMP_ROOT/key/global" ]; then
        echo "Cannot find $SEISCOMP_ROOT/key/global"
        return
    fi

    source "$SEISCOMP_ROOT/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 autopick!"
        return
    fi
    
    mkdir -p config

    OutputFile templates/config.tpl >config/config.ini
    OutputFile templates/trigger_head.tpl >config/trigger.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 autopick"
                continue
            fi

            if ! echo $PACKAGES | grep -q "\<autoloc\>"; then
                echo "    - autoloc not enabled for ${NET}_${STATION} - using autopick for triggering only"
                continue
            fi

            OutputFile templates/trigger_station.tpl >>config/trigger.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 "profile,station,advanced"
        exit 0
        ;;
    edit_globals)
        if [ $# -ne 0 ]; then
            break
        fi
        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

