#!/bin/sh

# work directory
startdir=`dirs -l`

# create directory structure
mkdir -p $startdir/{references,tarballs}

# get gtk api stuff, thanks for the handy links gnome ;-)
wget -c --directory-prefix=$startdir/tarballs/ http://library.gnome.org/devel/glib/glib-html-2.15.0.tar.gz
wget -c --directory-prefix=$startdir/tarballs/ http://library.gnome.org/devel/gobject/gobject-html-2.15.0.tar.gz
wget -c --directory-prefix=$startdir/tarballs/ http://library.gnome.org/devel/atk/atk-html-1.20.0.tar.gz
wget -c --directory-prefix=$startdir/tarballs/ http://library.gnome.org/devel/pango/pango-html-1.19.2.tar.gz
wget -c --directory-prefix=$startdir/tarballs/ http://library.gnome.org/devel/gtk/gtk-html-2.12.3.tar.gz
wget -c --directory-prefix=$startdir/tarballs/ http://library.gnome.org/devel/gdk/gdk-html-2.12.3.tar.gz
wget -c --directory-prefix=$startdir/tarballs/ http://library.gnome.org/devel/gdk-pixbuf/gdk-pixbuf-html-2.12.3.tar.gz

for tarball in glib gobject atk pango gtk gdk gdk-pixbuf
do
   # extract, cleanup and rename
  tar --use-compress-program=gzip -C $startdir/references -xf $startdir/tarballs/$tarball-html-*.tar.gz
  mv $startdir/references/$tarball-html-* $startdir/references/$tarball
done

function getfixxrefline ()
{
  # enter directory
  cd $startdir/references

  # init result
  result="\n\nFIXXREF_OPTIONS+="

  # append all the directories
  for directory in *
  do
    result="$result\t \\ \n\t--extra-dir=../$directory"
  done

  # add empty lines
  result="$result\n\n"

  # return
  echo $result
}

function builddocs ()
{
  # cleanup
  cd $startdir
  rm -Rf $startdir/checkout
  rm -Rf $startdir/references/$3

  # download copy from svn
  svn checkout $1 $startdir/checkout

  # add another fixxref link in Makefile.am
  echo -e `getfixxrefline` >> $startdir/checkout/$2/Makefile.am

  # copy all the references to the parent dir
  cd $startdir/checkout/$2/../
  cp -R $startdir/references/* ./

  # enter package directory
  cd $startdir/checkout

  # compile the package
  ./autogen.sh --enable-gtk-doc && make

  # copy the generated reference
  cp -R $2/html $startdir/references/$3

  # create tarball and move it to the tarball dir
  cd $startdir/references
  tar -zcf $3-docs.tar.gz $3/
  mv $3-docs.tar.gz $startdir/tarballs/
}

# build all the reference documents
builddocs "http://svn.xfce.org/svn/xfce/libxfce4util/tags/xfce_4_4_1/" "docs" "libxfce4util"
builddocs "http://svn.xfce.org/svn/xfce/libxfce4mcs/tags/xfce_4_4_1/" "docs" "libxfce4mcs"
builddocs "http://svn.xfce.org/svn/xfce/libxfcegui4/tags/xfce_4_4_1/" "docs" "libxfcegui4"
builddocs "http://svn.xfce.org/svn/xfce/libxfce4menu/trunk/" "docs/reference" "libxfce4menu"
builddocs "http://svn.xfce.org/svn/xfce/xfce4-panel/trunk/" "docs/API/" "libxfce4panel"
builddocs "http://svn.xfce.org/svn/xfce/libexo/tags/exo-0.3.2/" "docs/reference" "exo"
builddocs "http://svn.xfce.org/svn/xfce/thunar/tags/thunar-0.9.0/" "docs/reference/thunar-vfs" "thunar-vfs"
builddocs "http://svn.xfce.org/svn/xfce/thunar/tags/thunar-0.9.0/" "docs/reference/thunarx" "thunarx"
builddocs "http://svn.xfce.org/svn/xfce/squeeze/trunk/" "docs/reference/libsqueeze" "libsqueeze"

# cleanup
rm -Rf $startdir/checkout

exit 0
