#!/bin/sh
# What's new?
# Show changes for SliTaz Rolling ISO
# Aleksei Bobylev <al.bobylev@gmail.com>, 2015

# Single directory to work
WORKING="/home/tux/revs"

mkdir -p $WORKING

# Main (but uncomplete) list of SliTaz Core packages
if [ ! -e $WORKING/packages.list ]; then
	echo "Get packages.list..."
	busybox wget -q -O $WORKING/packages.list http://hg.slitaz.org/flavors/raw-file/tip/core/packages.list
fi

# Main TazPkg file with information about all packages
if [ ! -e $WORKING/packages.info ]; then
	echo "Get packages.info..."
	busybox wget -q -O $WORKING/packages.info.lzma http://mirror1.slitaz.org/packages/cooking/packages.info.lzma
	lzcat d $WORKING/packages.info.lzma $WORKING/packages.info
	rm -f $WORKING/packages.info.lzma
fi

# Create complete list of SliTaz Core packages (including all dependencies)
if [ ! -e $WORKING/packages.full ]; then
	echo "Create packages.full"
	for pkg in $(cat $WORKING/packages.list); do
		echo -n "$pkg "
		awk -F$'\t' -vp="$pkg" '$1 == p {print $8}' $WORKING/packages.info | \
			tr ' ' '\n' >> $WORKING/packages.full.raw
	done
	sort -o $WORKING/packages.full -u $WORKING/packages.full.raw
	rm -f $WORKING/packages.full.raw
	echo "Done!"
fi

# Load new commits to the local database
LAST_HG_LONG=$(busybox wget -q -O - http://hg.slitaz.org/wok | fgrep '>Changeset<' | cut -d'"' -f2)
LAST_HG=$(busybox wget -q -O - http://hg.slitaz.org$LAST_HG_LONG | fgrep '>tip<' | tr -cd 0-9: | cut -d: -f1)

LAST_DB=$(tail -n1 $WORKING/all.revs | cut -d$'\t' -f1)
LAST_DB=${LAST_DB:-0}

if [ "$LAST_DB" != "$LAST_HG" ]; then
	echo "Updating database..."
	for rev in $(seq $(( $LAST_DB + 1 )) $LAST_HG); do
		echo "Get revision #$rev"
		RAW=$WORKING/$rev.raw
		busybox wget -q -O $RAW http://hg.slitaz.org/wok/rev/$rev
		AUTHOR="$(grep '"author">&' $RAW | sed 's|&#32;&#60;.*</td>|</td>|g' | sed 's| class="author"||' | sed 's|&#32;| |g; s|&#48;|0|g; s|&#49;|1|g; s|&#50;|2|g; s|&#51;|3|g; s|&#52;|4|g; s|&#53;|5|g; s|&#54;|6|g; s|&#55;|7|g; s|&#56;|8|g; s|&#57;|9|g; s|&#63;|?|g; s|&#65;|A|g; s|&#66;|B|g; s|&#67;|C|g; s|&#68;|D|g; s|&#69;|E|g; s|&#70;|F|g; s|&#71;|G|g; s|&#72;|H|g; s|&#73;|I|g; s|&#74;|J|g; s|&#75;|K|g; s|&#76;|L|g; s|&#77;|M|g; s|&#78;|N|g; s|&#79;|O|g; s|&#80;|P|g; s|&#81;|Q|g; s|&#82;|R|g; s|&#83;|S|g; s|&#84;|T|g; s|&#85;|U|g; s|&#86;|V|g; s|&#87;|W|g; s|&#88;|X|g; s|&#89;|Y|g; s|&#90;|Z|g; s|&#97;|a|g; s|&#98;|b|g; s|&#99;|c|g; s|&#100;|d|g; s|&#101;|e|g; s|&#102;|f|g; s|&#103;|g|g; s|&#104;|h|g; s|&#105;|i|g; s|&#106;|j|g; s|&#107;|k|g; s|&#108;|l|g; s|&#109;|m|g; s|&#110;|n|g; s|&#111;|o|g; s|&#112;|p|g; s|&#113;|q|g; s|&#114;|r|g; s|&#115;|s|g; s|&#116;|t|g; s|&#117;|u|g; s|&#118;|v|g; s|&#119;|w|g; s|&#120;|x|g; s|&#121;|y|g; s|&#122;|z|g;')"
		DESC="$(sed '/<div class="description"/,/<\/div>/!d' $RAW | tr '\n' ' ' | sed 's|</div>|\0\n|' | head -n1 | sed 's|<div|<td|; s|</div|</td|' | sed 's| class="description"||')"
		PKGS=$(grep 'files.*/wok/file/*/' $RAW | sed -e 's|<|\n<|g; s|>|>\n|g' | sed '/</d' | awk -F/ '{print $1}' | sort -u | tr '\n' ' ' | sed 's|  *| |g')
		echo "$PKGS	$DESC$AUTHOR" | sed 's|> <|><|' > ${RAW%.raw}
		rm -f $RAW
		echo -en "$rev\t" >> $WORKING/all.revs
		cat $WORKING/$rev >> $WORKING/all.revs
		rm -f $WORKING/$rev
	done
fi


case "$1" in
	'')
		echo "$0 <rev1> <rev2>: Show changes for Rolling ISO"
		exit 0
		;;

	*)
		if [ "$1" -gt "$2" ]; then
			REV1=$1; REV2=$2
		else
			REV1=$2; REV2=$1
		fi
		# REV1 > $REV2

		rm -f $WORKING/revs.mini
		for rev in $(seq $REV1 -1 $REV2); do
			grep "^$rev	" $WORKING/all.revs >> $WORKING/revs.mini
			echo -n '.'
		done

		REPORT=$WORKING/report-$REV2-$REV1.html
		cat > $REPORT << EOT
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Changes in the SliTaz Rolling ISO from $REV2 to $REV1 revisions</title>
	<style type="text/css">
body {color: #222; background-color: #EEE; }
h1 { font-size: 1.2rem; }
table { margin: 0; padding: 0; border: 0 none; border-collapse: collapse; width: 100%; }
thead { border-top: 1pt solid #444; border-bottom: 1pt solid #444; font-weight: bold; }
td:nth-child(4) { white-space: pre; }
td { vertical-align: middle; }
tr:nth-child(odd)  { background-color: #FFFFFF; }
tr:nth-child(even) { background-color: #ECECEC; }
	</style>
</head>
<body>
<h1>Changes in the SliTaz Rolling ISO from $REV2 to $REV1 revisions</h1>
<table>
	<thead>
		<tr>
			<td>Package</td>
			<td>Revision</td>
			<td>Description</td>
			<td>Committer</td>
		</tr>
	</thead>
	<tbody>
EOT

		for pkg in $(cat $WORKING/packages.full); do
			echo "$pkg" >&2
			awk -F$'\t' -vpkg=" $pkg " ' \
				{
					if ($2 ~ pkg) {
						print "<tr><td>" pkg "</td><td><a href=\"http://hg.slitaz.org/wok/rev/" $1 "\" target=\"_blank\">" $1 "</a></td>" $3 "</tr>"
					}
				}' $WORKING/revs.mini >> $REPORT
		done

		cat >> $REPORT << EOT
	</tbody>
</table>
</body>
</html>
EOT
		;;
esac

exit 0
