#!/bin/sh -ef
export LC_ALL=C

#	1	2,9	3,10	4,11	5,12	6,13	7,14	8,15
#	bug_id	compone	severit	status 	resolut	reporte assigne	descrip
columns="bug_id,component,bug_severity,bug_status,resolution,reporter,assigned_to,short_desc"

dump()
{
	cd "${workdir:?}"
#	wget -nv --tries=1 --timeout=60 -O wget.out "$@" &>wget.sucks ||
#		{ echo "wget sucks:"; cat wget.sucks; return 1; } >&2
	GET "$@" >wget.out || GET "$@" >wget.out || { head wget.out; return 2; } >&2
	head -1 wget.out |grep -qs ^bug_id, ||
		{ echo "bad CSV header:"; head -1 wget.out; return 1; } >&2
	sed 1d wget.out |csv2tab -n "$(echo "$columns" |awk -F, '{print NF}')" -r-
	cd - >/dev/null
}

yabb()
{
	echo "$columns" |awk -F, \
'{	for (i=1;i<=NF;i++)
		sub(/^bug_/,"",$i)
	printf "{\t%s=$1\n", $1
	for (i=2;i<=NF;i++)
		printf "\t%s=%s1=%s2=\"\"\n",$i,$i,$i	
	print "\tresolved=resolved1=resolved2=\"\""
	printf "\tif(NF==%d) {\n", NF
	for (i=2;i<=NF;i++)
		printf "\t\t%s=$%d\n",$i,i
	print "\t\tresolved=f_resolved(status,resolution)"
	printf "\t} else if (NF==%d) {\n", 2*NF-1
	for (i=2;i<=NF;i++)
		printf "\t\t%s1=$%d;%s=%s2=$%d\n",$i,i,$i,$i,i+NF-1
	print "\t\tresolved1=f_resolved(status1,resolution1)"
	print "\t\tresolved=resolved2=f_resolved(status2,resolution2)"
	print "	} else {"
	print "	\tprintf \"bwk: error: NF=%d\\n\", NF > \"/dev/stderr\""
	print "	\texit 1"
	print "	}"
	print "}"
}'
}

#echo yabb output:
#echo "$(yabb)"
#exit 1

libbwk='function f_resolved(status,resolution) {
		return (status ~/CLOSED|RESOLVED|VERIFIED/ &&
		resolution ~/FIXED|INVALID|WONTFIX|WORKSFORME|DUPLICATE/) }
	function fmt() { printf "#%s\t%-16s\t%-8s\t%s\n%s\n\n",
		id, component, severity, resolution, short_desc }
	function filter(cond) { if (cond) print; else print >"/dev/fd/3" }
'

bwk()
{
	local e="$1"; shift
	awk -F'\t' "BEGIN{OFS=FS} $libbwk $(yabb) $e" ${1+"$@"}
}

expr=
while getopts e:h opt; do
	case "$opt" in
		e) expr="${OPTARG:?}"; readonly expr ;;
		h) pod2usage --exit=0 "$0"; exit 0 ;;
		*) pod2usage --exit=2 "$0"; exit 2 ;;
	esac
done
shift "$((OPTIND-1))"

if [ -n "$expr" ]; then
	bwk "$expr" ${1+"$@"}
	exit 0
fi

if [ -z "$workdir" ]; then
	. tmpdir.sh
	workdir=$TMPDIR
fi

if [ -n "$*" ]; then
	bugzilla="$1"; shift
	params="$(perl -le '$,="&",print@ARGV' "$@")"
else
	bugzilla="https://bugzilla.altlinux.org"
	params="product=ALT+Linux+Sisyphus"
fi

URL="$bugzilla/buglist.cgi?ctype=csv&columnlist=$columns${params:+&$params}"
dump "$URL"

# the rest is for qa-robot
noun=bug
opt_join=1

bwk_filter() { local e="$1"; shift; bwk "filter($e)" ${1+"$@"}; }
alias filter='bwk_filter 3>/dev/null'

xfmt1() { sort -n |bwk 'fmt()'; }
xfmt2() { sort -n |bwk 'fmt()'; }

fmt_new()
{
	filter 'resolved' >new-resolved.bugs 3>new.bugs
	if [ -s new-resolved.bugs ]; then
		n=`wc -l <new-resolved.bugs`
		echo "	$n NEW $(count "$n" "$noun") quickly RESOLVED"; echo
		xfmt1 <new-resolved.bugs
	fi
	if [ -s new.bugs ]; then
		n=`wc -l <new.bugs`
		subj="$subj +$n"
		echo "	$n NEW $(count "$n" "$noun")"; echo
		xfmt1 <new.bugs
	fi
}

fmt_old()
{
	echo "	$1 OLD $2 unexpectedly removed from the database"; echo
	xfmt1
}

fmt_updated()
{
	filter '!resolved1&& resolved2' >resolved.bugs 3>save
	filter ' resolved1&&!resolved2' <save >reopened.bugs
	if [ -s resolved.bugs ]; then
		n=`wc -l <resolved.bugs`
		subj="$subj -$n"
		echo "	$n RESOLVED $(count "$n" "$noun")"; echo
		xfmt2 <resolved.bugs
	fi
	if [ -s reopened.bugs ]; then
		n=`wc -l <reopened.bugs`
		echo "	$n REOPENED $(count "$n" "$noun")"; echo
		xfmt2 <reopened.bugs
	fi

}

fmt_total()
{
	filter '!resolved' >pending.bugs
	filter 'severity~/blocker|critical|major/' <pending.bugs >bad.bugs
	echo "	10 RANDOM bugs"; echo
	# boost bad bugs
	shuffle() { perl -MList::Util=shuffle -e 'print shuffle(<>)' ${1+"$@"}; }
	shuffle {bad,pending}.bugs |awk 'NR<=20' |sort -u |shuffle |head -10 |xfmt1
	n1=`wc -l <pending.bugs`
	n2=`wc -l <bad.bugs`
	subj="$subj ($n1)"
	echo "Total $n1 pending bugs ($n2 blocker/critical/major bugs)."
	echo "$bugzilla"
}

: <<'__EOF__'

=head1	NAME

bugs - grab bugs from bugzilla

=head1	SYNOPSIS

B<bugs> I<URL> [I<param>=I<val>...]

B<bugs> B<-e> I<expr> [I<FILE>...]

=head1	AUTHOR

Written by Alexey Tourbin <at@altlinux.org>.

=head1	COPYING

Copyright (c) 2005 Alexey Tourbin, ALT Linux Team.

This is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

=cut

__EOF__
