#!/bin/sh

# Script for show current tasks and task status for ALT Linux build system
# (c) 2014 Andrey Cherepanov <cas@altlinux.org>

ver="1.0"

# Usage: girar-show [<task num>]
# <task num> can contains suffix @ for subtasks list or @<subtask> to specified subtask log 

# Note: you should have access to git.alt to show you own tasks

source="$1"

# Regular expressions
re_errors="^\(E:\|error:\|.*: error:\|.*: NEW unmet\|[[:alnum:]-]\+\#[[:alnum:].-]*\|.* FAILED --\|Can't locate [[:alnum:]]\+.pm in @INC\|Package .* has broken dep on\)[[:space:]].*$"
re_subtasks="^#[0-7]\+ .*$"


list="$(expr match "$source" ".*\(@\)")"
subtask="${source/*@}"
task="${source/@*}"

filter_list=""
if [ -n "$list" ] ; then
	filter_list="$re_subtasks"
else
	subtask=""
fi

# Highlight by pattern and specified ANSI color code
hl() {
  pattern="$1"
  shift
  color="$1"
  shift
  esc=$(printf "\033")
  sed 's"'"${pattern}"'"'$esc'['${color}'m&'$esc'[0m"g' "$@"
}

# Highlight all output in red (treat string as error)
hlerror() {
	echo "$1" | hl '^.*$' '31' >&2
}

# Show complete task list without any arguments
if [ -z "$task" ]; then
	ssh git.alt task ls | while read l
	do
		status="$(echo $l | cut -f2 -d ' ')"
		case "$status" in
			BUILDING|COMMITTING)
				echo -e "\033[33m$l\033[0m"
				;;
			AWAITING|POSTPONED)
				echo -e "\033[36m$l\033[0m"
				;;
			TESTED)
				echo -e "\033[32m$l\033[0m"
				;;
			FAILED)
				echo -e "\033[35m$l\033[0m"
				;;
			*)
				echo "$l"
				;;
		esac
	done
	exit
fi

# Check supported argements
if [ -z "$(echo "$task" | egrep '^(-h|--help|--version|[0-9]+@?[0-9]*)$')" ]; then
	hlerror "Error: bad argument $task"
	task="-h"
fi

# Show help usage
if [ "$task" = "--help" -o "$task" = "-h" ]; then
	prg_name="${0##*/}"
	cat << _USAGE_
Usage: $prg_name [<source>]

Supported sources:
 without source      Show all active user tasks (requries access to git.alt)
 <task num> 	     Show task brief log
 <task num@>	     Show subtask list
 <task num@!>	     Show statistics and possible errors in task log
 <task num@subtask>  Show specified subtask log
 --help or -h	     Show usage and exit
 --version	     Show version
_USAGE_
	exit
fi

# Show version
if [ "$task" = "--version" ]; then
	echo "$ver"
	exit
fi


# Show specifies task content
if [ -n "$task" ]; then

	if [ "$subtask" = '!' ]; then
		# Show only possible errors, subtask headers and common subtask statistics (contains ::)
		filter_list="${re_errors/\)/|#[0-7]\+\|.*::\)}"
		subtask=""
	fi

	if [ -n "$subtask" ] ; then
		# Show complete subtask log
		target="http://git.altlinux.org/tasks/$task/build/$subtask/i586/log"
		filter_list=""
	else
		# Show task log
		log="$(curl -s "http://git.altlinux.org/tasks/$task/" | grep '^<h2>Status' | cut -f2 -d '"')"
		if [ -z "$log" ] ; then
			hlerror "Unable to show log for task #$task"
			exit 1
		fi
		target="http://git.altlinux.org/tasks/$task/$log"
	fi

	# Test url for availability
	if [ "$(curl --write-out %{http_code} -s "$target" -o /dev/null)" != "200" ]; then
		hlerror "Unknown source $source"
		exit 1
	fi

	curl -s "$target" | grep --color=never "$filter_list" | \
		hl "$re_subtasks" '33' | \
		hl "$re_errors"   '31'
	exit
fi
