#! /bin/sh 
# Update kernel page(s).

set -e

# KDIR=~/devel/kernel

# # Takes patch file, name and output filename
# extract_description()
# {
#     echo "<html><head><title>Rusty's Linux Kernel Page: $2</title></head>" > $3
#     echo '<body text="#000000" bgcolor="#fffff0" link="#800000" vlink="#400000" alink="#993399">' >> $3
#     echo "<h1>Description: $NAME</h1>" >> $3

#     # Print after headers, from line preceeding "--- xxxx".
#     zcat $1 | awk '/^--- / { DONTPRINT=1 } { if (NR > 1 && BODY && !DONTPRINT) print LASTLINE; LASTLINE=$0; } /^$/ { BODY=1 }' |
# 	sed -e 's/</\&lt;/g' -e 's/>/\&gt;/g' -e 's/^ *$/<p>/' -e 's:^  \(.*\):<pre> \1</pre>:' >> $3

#     echo "</body></html>" >> $3
# }

# # Sort these patches so that dependencies come before those that
# # depend on them.  Ignore dependencies in other directories.
# sort_patches()
# {
#     for sp; do
# 	for dep in $(zcat $sp | grep '^Depends:' | cut -d: -f2-); do
# 	    if [ patches/$(dirname $dep) = $(dirname $sp) ]; then
# 		echo patches/$dep $sp
# 	    fi
# 	done
# 	echo DUMMY $sp
#     done | tsort | grep -v -x DUMMY
# }

# # Takes a patch file, prints out dependencies.
# print_depends()
# {
#     for dep in $(zcat $1 | grep '^Depends:' | cut -d: -f2-); do
# 	DEPNAME=$(zcat patches/$dep | grep '^Name:' | cut -d: -f2-)
# 	echo "<br>Needs: <a href=\"#$dep\">$DEPNAME</a>"
#     done
# }

# # Find the patch with latest mtime (most likely least broken one)
# latest_patch()
# {
#     ls -t `dirname $1`/`basename $1 .gz`*.gz | head -1
# }

# status_to_color()
# {
#     case "$1" in
#     BROKEN) echo "#FF3F3F";;
#     APPLIES) echo "#3FFF3F";;
#     *) echo "#FFFFFF";;
#     esac
# }

# # Handle args...
# for arg; do
#     case "$arg" in
#     --kernel=*) KERNELS=$(echo "$arg" | cut -d= -f2-);;
#     --compile) EXTRA_ARGS="$EXTRA_ARGS --compile";;
#     --boot) EXTRA_ARGS="$EXTRA_ARGS --boot";;
#     --patch=*) PATCHES=$(echo "$arg" | cut -d= -f2-); ONE_PATCH="$PATCHES";;
#     --dir=*) PATCHES=$(echo $(echo "$arg" | cut -d= -f2-)/*.patch.gz);;
#     *) echo Unknown arg "$arg" >&2; exit 1;;
#     esac
# done

# [ x"$KERNELS" != x ] || KERNELS=$(latest-kernel-version; latest-kernel-version -mm; echo ppc64-2.5)

# # What patches do we have
# [ x"$PATCHES" != x ] ||
#     PATCHES=$(for d in patches/[A-Z]*; do ALL=$(sort_patches $d/*.patch.gz); zcat $ALL | gzip -9 > $(dirname $d)/all-$(basename $d).patch.gz; echo $ALL; done)

# # Clean out obsoleted results.
# for k in $KERNELS; do
#     for p in $PATCHES; do
# 	STATUSFILE=status/$(basename $p)-$k
# 	# If there's been a new patch since then, retry.
# 	for f in "$(echo $p | sed 's/.gz$//')".*gz; do
# 	    if [ "$f" -nt "$STATUSFILE" ]; then
# 		echo Recalculating status for $k $p...
# 		rm -f $STATUSFILE
# 		break
# 	    fi
# 	done
#     done
# done

# for k in $KERNELS; do
#     # For each patch, see if it applies.
#     for p in $PATCHES; do
# 	STATUSFILE=status/$(basename $p)-$k
# 	STATUSLINE=$(tail -1 $STATUSFILE 2>/dev/null)
# 	if [ x"$STATUSLINE" = x ] || [ x"$ONE_PATCH" = x$p ]; then
# 	    # Remove any old result.
# 	    rm -f $STATUSFULE
# 	    echo Testing $p on $k...
# 	    STATUSLINE=$(scripts/try_patch $p $KDIR/linux-$k $k $EXTRA_ARGS)
# 	    rm -rf $KDIR/.$$-$k
# 	    echo "$STATUSLINE" >> $STATUSFILE
# 	fi
#     done
# done

# # Now, generate table.
# exec > index.html.new
# LINE=$(fgrep -xn '<!-- table-here -->' template-index.html | cut -d: -f1)

# # Prepend...
# head -$(($LINE - 1)) template-index.html

# # Actual table.
# echo '<table width="100%" border=2>'
# echo '<tr><td colspan=10 align=center> Patch Compatibility Chart </td></tr>'
# echo '<tr>'
# echo '<td><strong>Patch name</strong></td>'
# echo '<td><strong>Status</strong></td>'
# for k in $KERNELS; do echo "<td> <strong> v$k </strong> </td>"; done
# echo '</tr>'
# SECTION=""

# # Reversing the list puts foo.patch.gz before foo-xxx.patch.gz.
# for p in $(for d in patches/[A-Z]*; do sort_patches $d/*.patch.gz; done); do
#     # Extract name, then description from patch.
#     NAME=$(zcat $p | grep '^Name:' | cut -d: -f2-)
#     NAMEFILE=$(echo -n $NAME.html | tr -c '[A-Za-z0-9.]' '_')
#     # Is this same section as last time? (Remove spaces)
#     NEWSECTION=$(basename $(dirname $p) )
#     if [ x"$NEWSECTION" = x"$SECTION" ]; then :
#     else
# 	SECTION=$NEWSECTION
# 	echo "<tr><td><strong><a href=\"$(dirname $(dirname $p) )/all-$SECTION.patch.gz\">$SECTION section:</a></strong></td></tr>"
#     fi
#     extract_description $p "$NAME" $NAMEFILE
#     echo "<a name=\"$(basename $p)\"><tr>"
#     echo "<td><a href=\"$NAMEFILE\">$NAME</a>"
#     print_depends $p
#     echo "</td>"
#     STAT=$(zcat $p | grep '^Status:' | cut -d: -f2-)
#     echo "<td>$STAT</td>"
#     for k in $KERNELS; do
# 	STATUSFILE=status/$(basename $p)-$k
# 	STATUSLINE=$(tail -1 $STATUSFILE 2>/dev/null)
# 	if [ "x$STATUSLINE" = x ]; then STATUSLINE=":UNKNOWN:"; fi
# 	STATUS=$(echo $STATUSLINE | cut -d: -f2)
# 	PATCH=$(echo $STATUSLINE | cut -d: -f3)
# 	if [ "x$PATCH" = x ]; then
# 	    # Guess at patch with most recent mtime.
# 	    PATCH=`latest_patch $p`
# 	fi
# 	BGCOLOR=`status_to_color $STATUS`
# 	echo "<td bgcolor=\"$BGCOLOR\"><a href=\"$PATCH\">$STATUS</td>"
#     done
#     echo "</tr></a>"
# done
# echo "</table>"

# # Append...
# tail +$LINE template-index.html
# cp index.html index.html.old
# mv index.html.new index.html

# # Delete old status files
# find status -type f -mtime +30 | xargs -r rm

# Annotate series files with descriptions from patch names.
for f in */series; do
    rm -f $f.new
    STARTED=0
    while read LINE; do
	case $LINE in
	    \#*) if [ $STARTED -eq 0 ]; then echo "$LINE" >> $f.new; fi;;
	    "") STARTED=1; echo >> $f.new;;
	    *)  STARTED=1
		PATCH=`dirname $f`/$LINE
		zcat $PATCH | sed -n 's/^Name:/#/p' >> $f.new
		echo $LINE >> $f.new
		;;
	esac
    done < $f
    mv $f.new $f
done

# Create megapatches
for f in */series; do
    for p in $(grep -v '^#' $f); do zcat `dirname $f`/$p; echo; done | gzip -9 > `dirname $f`/ALL.patch.gz
done
