#!/bin/bash
#
# Stupid little wrapper script to view the list of
# or tail the log files on the pmbs.
#
# Author: Pascal Bleser <guru@unixtech.be>
#

SCRIPTNAME=${0##*/}

PROJECT=${PWD##*/}
SPEC_MATCHES=$(echo *.spec | wc -w)
SPEC_MATCHES=$(echo $SPEC_MATCHES)
if [ "$SPEC_MATCHES" = "1" ]; then
	SPEC=$(echo *.spec)
	PROJECT=${SPEC%.spec}
fi

ssh_args="-ttqXa"
scp_args=""

function die { echo "ERROR: $*" >&2; exit 1; }
function show_help {
cat<<EOF
Usage: $SCRIPTNAME [-C] [-f] [<version> <arch>|build|configlog|cl|clean]

Options:
    -C: enable ssh compression
    -f: use fast encryption algorithm (blowfish)

Arguments:
$SCRIPTNAME can either be invoked in the form
    $SCRIPTNAME [-C] [-f]
(without arguments) and it will list the log files
for the current project ($PROJECT) that are available
on the server.

To actually tail a log file, it must be used in the
following form:
    $SCRIPTNAME [-C] [-f] <version> <arch>
where:
    <version> is the distribution version (10.2, 10.1, ...)
    <arch> is the architecture or a shorthand notation:
        i, 5, i5 = i586
        6, i6 = i686
        x = x86_64
        n = noarch

When invoked with the argument \"clean\", it will remove
the log files for the current project on the server:
    $SCRIPTNAME [-C] [-f] clean

When invoked with \"build\", it will cat the content of
the build.log file for the current project:
    $SCRIPTNAME [-C] [-f] build

When invoked with \"configlog\" or \"cl\", it will copy
the config.log.gz file for the current project into
your local working directory:
    $SCRIPTNAME [-C] [-f] cl

Environment:
    Options may also be set in the environment variable
    PM_LOG_OPTIONS, e.g.:
        export PM_LOG_OPTIONS="-C -f"

Examples:
    $SCRIPTNAME
        will list the logfiles for the current project

    $SCRIPTNAME -C -f 10.2 x
        will tail the log file for openSUSE 10.2 on x86_64,
        using SSH compression and blowfish encryption

EOF
}

if [ -e "$PM_LOG_OPTIONS" ]; then
	for opt in $PM_LOG_OPTIONS; do
		case $opt in
			-C) ssh_args="$ssh_args -C"; scp_args="$scp_args -C" ;;
			-f) ssh_args="$ssh_args -c blowfish"; scp_args="$scp_args -c blowfish" ;;
			*) echo "warning: unknown flag $opt in environment variable PM_LOG_OPTIONS (ignored)" >&2 ;;
		esac
	done
fi

lines=20

OPTERR=0
while getopts ':Cfhl:' opt; do
	case $opt in
		C) ssh_args="$ssh_args -C"; scp_args="$scp_args -C" ;;
		f) ssh_args="$ssh_args -c blowfish"; scp_args="$scp_args -c blowfish" ;;
		h) show_help; exit 0 ;;
		l) lines=$OPTARG ;;
		*)
		case $OPTARG in
			[0123456789]*) lines=${OPTARG} ;;
			*) die "unsupported switch -$OPTARG" ;;
		esac
		;;
	esac
done
shift $(( $OPTIND - 1 ))

tail_cmd="tail --retry --follow=name --lines=$lines --verbose"

if [ -n "$1" ]; then
	if [ "$1" = "clean" ]; then
		exec ssh $ssh_args pmbs /bin/rm -v "/srv/pmbs/logs/$PROJECT/*"
	fi
	if [ "$1" = "build" ]; then
		exec ssh $ssh_args pmbs $tail_cmd "/srv/pmbs/logs/$PROJECT/build.log"
	fi
	if [ "$1" = "server" -o "$1" = "pmbs" ]; then
		exec ssh $ssh_args pmbs $tail_cmd "/srv/pmbs/logs/pmbs_scheduler.log"
	fi
	if [ "$1" = "configlog" -o "$1" = "cl" ]; then
		exec scp $scp_args "pmbs:/srv/pmbs/logs/$PROJECT/config.log.gz" .
	fi

	ARCH=$2
	[ -n "$ARCH" ] || die "must specify arch as 2nd parameter"
	case $ARCH in
		x) ARCH=x86_64 ;;
		i|5|i5) ARCH=i586 ;;
		i6|6) ARCH=i686 ;;
		n) ARCH=noarch ;;
		i586|i686|i386|x86_64|noarch) ;;
		*) die "unsupported architecture $ARCH" ;;
	esac

	case $1 in
		9.*|10.0|10.1) LOG=SuSE-$1-$ARCH.log ;;
		10.*|11.*) LOG=openSUSE-$1-$ARCH.log ;;
		*) die "unsupported version $1" ;;
	esac
	exec ssh $ssh_args pmbs $tail_cmd "/srv/pmbs/logs/$PROJECT/$LOG"
else
	exec ssh $ssh_args pmbs 'TZ=CET /bin/ls -l "/srv/pmbs/logs/'"$PROJECT"'/"'
fi

