#!/bin/sh
#
# download the AllInOne page of a manual on wiki.debian.org as docbook
# and transform it into proper XML
#
# very loosly based on the moinmoin2pdf script from Petter Reinholdtsen
#
# Author/Copyright:	Holger Levsen
# Licence:		GPL2+
# first edited:		2006-07-06
# last edited:		2009-05-30
# 
# This program 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

if [ ! $(which xmllint) ] ; then
	echo "Please install libxml2-utils."
	exit 1
fi

if [ "$name" = "" ] ; then 
	exit 1
fi
TMPFILE=$(mktemp)
xmlfile=$name.xml

DEBIAN_EDU_DOC_BUILDDATE=`date -u +%Y-%m-%d`

# the last but one sed "preserves" the 2nd matched regex
# the last sed does the same as dos2unix
# head at the end chops of the last two lines with the Category:Permalink entry
GET -H User-Agent: "${url}AllInOne?action=raw"|sed "s%<<Include(%%g" | sed "s%)>>%%g" | sed "s%$path1%%g" |sed 's/.$//'|head -n -2> id

for i in `cat id` ; do
	TARGET=`echo "${i}" |sed "s/\(.*\)\/\(.*\)/\2/" `.xml
	echo "$TARGET		${url}${i}?action=show&mimetype=text/docbook"
	# download the docbook version of the manual from the wiki and pipe it through sed to
	#   - insert the build date
	#   - convert <code> tag to <computeroutput> as this is understood by docbook (tools)
	#   - provide correct path to the images
	#   - remove the revision history
	#   - remove the Category:Permalink line
	#   - add some linebreaks
	#   - delete the first line containing the XML declaration
	GET "${url}${i}?action=show&mimetype=text/docbook" | 
	sed "s/\$DEBIAN_EDU_DOC_BUILDDATE/<code>$DEBIAN_EDU_DOC_BUILDDATE<\/code>/" |
	# replace tags:
	sed "s%code>%computeroutput>%g" |
	sed "s%/htdocs/rightsidebar/img/%./images/%g" |
	# remove final tag:
	sed "s%</article>%%" |
	# remove tags and enclosed content:
	sed "s#<articleinfo>\(.*\)</articleinfo>##g" |
	sed "s%<para><ulink url='http://wiki.debian.org/CategoryPermalink#'>CategoryPermalink</ulink> </para>%%" |
	# introduce line breaks:
	sed "s%<title>%\n<title>%g" |
	sed "s%<\/title>%\n<\/title>%g" |
	sed "s%<section>%\n\n<section>%g" |
	sed "s%<\/section>%\n<\/section>%g" |
	sed "s%<para>%\n<para>%g" |
	sed "s%<\/para>%\n<\/para>%g" |
	sed "s%FIXME%\nFIXME%g" |
	# cut off first line:
	sed '1d' > $TARGET
	if [ "$(grep -v FIXMEs $TARGET |grep FIXME)" != "" ] ; then
		echo "----------------------------------" >> $TMPFILE
		echo ${url}${i} >> $TMPFILE
		grep -v FIXMEs $TARGET | grep FIXME >> $TMPFILE
	fi
done

# now only keep the page name (equals section id) without path
# (replace with the second match of the regular expression)
sed -i "s/\(.*\)\/\(.*\)/\2/" id

# add id= to <section>s and a linebreak at the end
for i in `cat id` ; do
	sed -i "0,/<section>/ s/<section>/<section id=\"$i\">/" ${i}.xml 
	sed -i "$ s#>#>\n#" ${i}.xml
done

# paste it together
rm -f $xmlfile
for i in `cat id` ; do
	cat ${i}.xml >> $xmlfile
	rm ${i}.xml
done
rm id

# get images and modify $xmlfile
../scripts/get_images $xmlfile $path1

# turn links into internal references if appropriate
# this needs to run after ./get_images
#
#  -0\777  read multiple lines
perl -0\777 -pi -e "s/<ulink url='$path2(.*)\/(.*)'>(.*)\n<\/ulink>/<link linkend=\"\2\">\3<\/link>/g" $xmlfile

# make it a docbook article again
sed -i "1,/</ s#<#<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook XML V4.4//EN\" \"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd\"><article><articleinfo><title>$DEBIAN_EDU_DOC_TITLE</title></articleinfo>\n<#" $xmlfile
sed -i "$ s#>#>\n</article>#" $xmlfile

# remove the first empty line
sed -i "1d" $xmlfile

# clean it further
TMPFILE2=$(mktemp)
xmllint $xmlfile > $TMPFILE2
mv $TMPFILE2 $xmlfile

# motivate
if [ "$(grep -v FIXMEs $xmlfile |grep FIXME)" != "" ] ; then
	echo "====================" >> $TMPFILE
	echo `grep -v FIXMEs $xmlfile |grep FIXME|wc -l` FIXMEs left to fix >> $TMPFILE
	echo "====================" >> $TMPFILE
fi
mv $TMPFILE fixme-status.txt
