!/bin/bash

if [ -z ${SEISCOMP_ROOT+x} ]; then
        echo "Environment variable SEISCOMP_ROOT is not set."
        echo "Either use 'seiscomp exec [script]' or set SEISCOMP_ROOT to the installation "
     exit 1
     echo "path of your SeisComP installation."
fi

grep -A 2 ^station $SEISCOMP_ROOT/var/lib/seedlink/seedlink.ini | while read a b c; do
    if [ "$a" = station -a "$b" != .dummy ]; then
             id=$b
             sta=""
             net=""
             while read a b c; do
                     case $a in
                             --) break;;
                             name) eval sta=$c;;
                             network) eval net=$c;;
                     esac
             done
             if [ -z "$id" -o -z "$sta" -o -z "$net" ]; then
                     echo "Error parsing seedlink.ini"
                     break
             fi

             if [ "$id" != "$net.$sta" ]; then
                     mv -v "$SEISCOMP_ROOT/var/lib/seedlink/buffer/$id" "$SEISCOMP_ROOT/var/lib/seedlink/buffer/$net.$sta"
             else
                     echo "$id: No renaming required"
             fi
     fi
done
