#!/bin/bash

TEMPLATES="seiscomp seisgram qplot"
NOLINKS="run_with_lock trylock"

export SEISCOMP_ROOT="$(pwd)"
echo "SeisComP home directory: $SEISCOMP_ROOT"

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

source "lib/env.sh"

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

source "lib/keyutils.sh"

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

LoadConfig $global_cfg

for f in $TEMPLATES; do
    echo "Creating $SEISCOMP_ROOT/bin/$f"

    OutputFile templates/$f.tpl >bin/$f
    chmod a+x bin/$f
done

echo "Trying to create symbolic links in ~/bin"

mkdir -p ~/bin
if cd ~/bin; then
    for f in "$SEISCOMP_ROOT"/bin/*; do
        if echo "$NOLINKS" | grep -vq "\<${f##*/}\>"; then
            ln -s "$f" .
        fi
    done
fi

echo

