#!/bin/bash

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

pkgname="analysis"
PKGROOT="$SEISCOMP_ROOT/$pkgname"

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

# SHM scripts need reverse slashes
PKGROOT_R="$(echo "$PKGROOT" | sed -e 's/\//\\\\/g')"

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

VERSION="2.5"
KEY_VERSION="2.5"

_init_keys() {
    mkdir -p key

    SFDLENGTH="10"
    REFSTAT=""
}

_edit_globals() {
    _init_keys

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

    Ask SFDLENGTH "Length of SFD index file in days" "$SFDLENGTH"
    Ask REFSTAT "Reference stations (up to 6) separated by ','" "$REFSTAT"

    OutputKeys $global_cfg >key/global
}

_write_conf() {
    LoadConfig "$global_cfg" 

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

    source "$SEISCOMP_ROOT/key/global"
    
    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 [ "$SLARCHIVE" = no ]; then
        echo "You must enable slarchive in order to use analysis!"
        return
    fi
    
    SDS_ARCHIVE="$SEISCOMP_ROOT/acquisition/archive"

    mkdir -p operator
    mkdir -p private
    mkdir -p sh/setup
    mkdir -p sh/util
    mkdir -p sh/source/seed_io/inputs
    mkdir -p sh/globals
    mkdir -p sh/inputs
    
    OutputFile templates/shsetup.tpl >sh/setup/shsetup
    OutputFile templates/user_startup.tpl >private/SHM_USER_STARTUP.SHC
    OutputFile templates/crontab.tpl >operator/crontab

    REFSTAT="$(echo $REFSTAT | sed -e 's/[[:space:]]//g;s/,/\\n/g')"
    OutputFile templates/refstation_list.tpl >sh/globals/refstation_list.txt

    cp contrib/make_sfd.csh sh/util/
    cp contrib/make_sfd_today.csh sh/util/
    cp contrib/sfdlist.csh sh/source/seed_io/
    cp contrib/seed_setup.txt sh/source/seed_io/inputs/

    for d in contrib/plugins/*; do (
        plgtar="$PKGROOT/sh/plugins/$(basename "$d").tar"
        cd "$d"
        tar -cf "$plgtar" *
        cd "$PKGROOT/private"
        tar --exclude '*.plg' -xf "$plgtar"
    ); done

    contrib/make_cfg_shm.pl "$SEISCOMP_ROOT/config/net.tab"
    
    echo
    echo "To activate the SHM environment use \"source $PKGROOT/sh/setup/shsetup\""
    echo
    echo "Don't forget to add \"source $PKGROOT/sh/setup/shsetup\" to .cshrc also!"
    echo
}

action="$1"
shift

case "$action" in
    start|stop|check)
        exit 0
        ;;
    get_attributes)
        echo "globals,advanced"
        exit 0
        ;;
    edit_globals)
        if [ $# -ne 0 ]; then
            break
        fi
        _edit_globals
        exit 0
        ;;
    edit_profile)
        if [ $# -ne 1 ]; then
            break
        fi
        exit 0
        ;;
    edit_station)
        if [ $# -ne 2 ]; then
            break
        fi
        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

