#!/bin/bash

echo Update linux kernel from kernel.org Mercurial repository
# mkdir -p ~/linux
# cd ~/linux
# hg clone http://kernel.org/hg/linux-2.6 hg
cd ~/linux/hg
hg pull -u

echo Convert kconfig to html for all architectures
mkdir -p ~/www/kdocs/menuconfig
echo "<html>Menuconfig docs for kernel $(make kernelversion)</html><body><ul>" > ~/www/kdocs/menuconfig/index.html
for i in $(find arch -maxdepth 2 -name Kconfig | sort)
do
  echo Converting "$i"
  ARCH=$(echo $i | sed -r 's@.*/(.*)/.*@\1@')
  echo "<li><a href=${ARCH}.html>${ARCH}</a></li>" >> ~/www/kdocs/menuconfig/index.html
  ~/www/kdocs/make/menuconfig2html.py $i > ~/www/kdocs/menuconfig/$ARCH.html
done
echo "</ul></body></html>" >> ~/www/kdocs/menuconfig/index.html

echo Install updated Documentation
rm -rf ~/www/kdocs/Documentation/
cp -a ~/linux/hg/Documentation ~/www/kdocs/Documentation

echo Build htmldocs and xhtml-nochunks
rm -rf ~/linux/temp
cd ~/linux/hg
mkdir ~/linux/temp
make allnoconfig O=~/linux/temp

# I have a dual processor laptop, so overlap work here.  Generate the xml
# first, then have make htmldocs produce split-up versions in the background
# while a script produces nochunks versions in the foreground.
cd ~/linux/temp
make -j 2 xmldocs
make -j 2 htmldocs &
cd Documentation/DocBook
for i in *.xml
do
  echo "Converting $i"
  xmlto xhtml-nochunks $i -o xhtml-nochunks 
done
# Wait for background task to finish before retrieving result
while [ $(jobs | wc -l) -ne 0 ]
do
  sleep 1
  # Without this next line, bash never notices a change in the number of jobs.
  # Bug noticed in Ubuntu 7.04
  jobs > /dev/null
done
mv xhtml-nochunks/*.html .
rmdir xhtml-nochunks

echo Install htmldocs and update index
rm -rf ~/www/kdocs/htmldocs
cp -a ~/linux/temp/Documentation/DocBook ~/www/kdocs/htmldocs
cd ~/www/kdocs/htmldocs
head -n 2 index.html > index2.html
echo "<ul>" >> index2.html
for i in $(ls -p | sed -n 's@/$@@p')
do
  echo "<li>(<a href=\"$i\">chunks</a>) (<a href=\"${i}.html\">nochunks</a>) " >> index2.html
  sed -nr 's@.*<title>(.*)</title>.*@\1@p' ${i}.xml | head -n 1 >> index2.html
  echo "</li>" >> index2.html
done
echo "</ul>" >> index2.html
mv index2.html index.html
echo "Done with kdocs"
