#!/bin/bash

abort() {
    echo -e "$1"
    exit 1
}

[ $# -gt 0 ] || abort "no service specified"

service=$(echo "$1" | sed -E 's#/?service/##' )

matches=$(ls /service/ | grep "$service")
n_matches=$(ls /service/ | grep "$service" | wc -l)

[ "$n_matches" -gt 0 ] || abort "no service matching $service"
[ "$n_matches" -lt 2 ] || abort "multiple services matching $service\n$matches"

match=$matches

log_run="/service/$match/log/run"
log_path=$(grep -o '/var/log/[^ ]*' "$log_run" 2> /dev/null )
log_file="$log_path/current"

[ -e "$log_file" ] || abort "$log_file does not exist"

echo "showing $log_file"
echo "showing $log_file" | sed 's/./=/g'

#tail -F "$log_file" 2> /dev/null | cut -f 2- -d ' '
tail -n 50 -F "$log_file" 2> /dev/null | tai64nlocal | sed -E 's/\........../      /'