#!/bin/bash

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

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

    PLOT_GIF="no"
    PLOT_STREAM="BH"
    PLOT_LOCID=""
    PLOT_FILTER="WWSSSP"
    PLOT_MAGN="50000"
    PLOT_SIZE="1024x780"
    PLOT_KEEP="30"
}

_edit_keys() {
    _init_keys

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

    Ask PLOT_STREAM "Stream code (without component code)" "$PLOT_STREAM"
    Ask PLOT_LOCID "Location code" "$PLOT_LOCID"
    Ask PLOT_FILTER "Filter" "$PLOT_FILTER"
    Ask PLOT_MAGN "Magnification factor" "$PLOT_MAGN"
    AskYN PLOT_GIF "Create GIF files" "$PLOT_GIF"

    if [ "$PLOT_GIF" = yes ]; then
        Ask PLOT_SIZE "GIF-file size" "$PLOT_SIZE"
        Ask PLOT_KEEP "Number of days to keep old GIF-files" "$PLOT_KEEP"
    fi

    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"

    INSTALL_GIF="no"

    _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"
    
    rm -rf config/*
    rm -rf operator/*
    mkdir -p config

    nstat=0
    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

            statext=0
            STATID="$STATION"
            while [ -f config/rc_$STATID ]; do
                statext=$((statext+1))
                STATID="${STATION}${statext}"
            done

            if [ "$LOCALSOURCE" = seedlink ]; then
                SRCADDR=127.0.0.1
                SRCPORT="$SEEDLINK_PORT"
            elif [ "$LOCALSOURCE" = digiserv ]; then
                SRCADDR=127.0.0.1
                SRCPORT="$DIGISERV_PORT"
            fi
    
            if [ -z "$PLOT_LOCID" ]; then
                LOCATION=""
            else
                LOCATION=" location=$PLOT_LOCID"
            fi
            
            if [ "$PLOT_GIF" = yes ]; then
                INSTALL_GIF="yes"
                OutputFile templates/client_slqplot.tpl >> config/clients_$STATID
            fi

            OutputFile templates/slqplot.ini.tpl > config/slqplot_$STATID
            OutputFile templates/rc_station.tpl > config/rc_$STATID
        done
    done

    mkdir -p log
    mkdir -p operator
    mkdir -p status

    OutputFile templates/slqplot.coef > config/slqplot.coef
    OutputFile templates/qplot_ctrl.tpl > operator/qplot_ctrl
    chmod 755 operator/qplot_ctrl

    if [ "$INSTALL_GIF" = yes ]; then
        mkdir -p plotfiles
        OutputFile templates/crontab.tpl > operator/crontab

        OutputFile templates/slqplot2tek2gif.tpl >operator/slqplot2tek2gif
        chmod 755 operator/slqplot2tek2gif

        OutputFile templates/update_plotfiles.tpl >operator/update_plotfiles
        chmod 755 operator/update_plotfiles

        OutputFile templates/purge_plotfiles.tpl >operator/purge_plotfiles
        chmod 755 operator/purge_plotfiles
    fi
}

action="$1"
shift

case "$action" in
    start|stop|check)
        operator/qplot_ctrl $action
        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)
        if [ -f "operator/crontab" ]; then
            cat operator/crontab
        fi
        exit 0
        ;;
esac

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

