#! /bin/sh
# Bootstrap script
# $Id: bootstrap 2079 2009-10-25 09:51:41Z sam $

set -ex
export LANG=C

#
# Variables
#

#
# Update elk
#
update_elk_file() {
  rm -f "${ELKDIR}/$1.new"
  tar xzf "${ELKDIR}/elk-${LATEST}.tar.gz" --to-stdout "elk-${LATEST}/$1" \
    > "${ELKDIR}/$1.new"
  mv -f "${ELKDIR}/$1.new" "${ELKDIR}/$1"
}

ELKDIR="elk"
LATEST="`find ${ELKDIR} -name '*.tar.gz' | sed -ne 's/.*elk-\(.*\)\.tar\.gz$/\1/p' | sort -n | tail -1`"
if test -n "${LATEST}"; then
update_elk_file ChangeLog
update_elk_file COPYING
update_elk_file NEWS
fi

#
# Update libcaca
#
update_libcaca_file() {
  rm -f "${LIBCACADIR}/$1.new"
  tar xzf "${LIBCACADIR}/libcaca-${LATEST}.tar.gz" --to-stdout "libcaca-${LATEST}/$1" \
    > "${LIBCACADIR}/$1.new"
  mv -f "${LIBCACADIR}/$1.new" "${LIBCACADIR}/$1"
}

LIBCACADIR="libcaca"
LATEST="`find ${LIBCACADIR} -name '*.tar.gz' | sed -ne 's/.*libcaca-\(.*\)\.tar\.gz$/\1/p' | sort -n | tail -1`"
if test -n "${LATEST}"; then
update_libcaca_file ChangeLog
update_libcaca_file NEWS
fi
ln -nsf /usr/share/doc/libcaca-dev/html libcaca/doc

#
# Update hot-babe
#
update_babe() {(
  cd "hot-babe"
  if test "$1.tar.gz" -nt "$1"; then return; fi
  tar cvzf "$1.tar.gz" --exclude=".svn" "$1"
)}
update_babe hot-binladen
update_babe hot-wtc

#
# Update mailing-lists archives
# requires: mhonarc
#
for list in elk flessd opencv monsterz; do
  if test -d /var/lib/ecartis/archives/${list}; then
    (cd ${list}
     mhonarc /var/lib/ecartis/archives/${list}/${list}.*
     find . -name 'gz??????????.gz' | xargs rm -f
     find . -name 'pgp??????????.pgp' | xargs rm -f
     find . -name 'bin??????????.bin' | xargs rm -f)
  fi
done

#
# Update lectures
# requires: xvfb, magicpoint
#
update_lecture() {(
  cd "lectures/$1/src"
  if test "../index.html" -nt "slides.mgp"; then return; fi
  #mgp -x vflib -b white -D .. slides.mgp
  xvfb-run -s "-screen scrn ${2}x16" mgp -x vflib -b white -D .. slides.mgp
)}

update_lecture 20010519-videolan 1024x768
update_lecture 20020214-mpeg 1024x768
update_lecture 20020424-debian 1024x768
update_lecture 20021115-debian 1024x768
update_lecture 20030403-php 1024x768
update_lecture 20040708-re 1024x768
update_lecture 20040903-sucon 1400x1050
update_lecture 20041111-debian 1400x1050
update_lecture 20041111-debian2 1400x1050
update_lecture 20050419-wikipedia 1400x1050

#
# Update Debian stuff
# requires: dpkg-scanpackages
#
(cd debian
for dist in woody sarge sid; do
  for arch in i386; do
    d="dists/$dist/main/binary-$arch"
    if [ -d "$d" ]; then
      # Build a fake tree
      rm -Rf packages-tmp
      for dir in `find pool -type d`; do
        mkdir -p "packages-tmp/$dir"
      done
      for file in `find pool -name '*_'$arch'.deb' -o -name '*_all.deb'`; do
        ln -s "`pwd`/$file" "packages-tmp/$file"
      done
      # Remove unwanted stuff
      if [ "$dist" = "sid" ]; then
        rm -f `find packages-tmp -type l -name '*sarge*'`
        rm -f `find packages-tmp -type l -name '*woody*'`
      fi
      if [ "$dist" = "sarge" ]; then
        rm -f `find packages-tmp -type l ! -name '*sarge*'`
      fi
      if [ "$dist" = "woody" ]; then
        rm -f `find packages-tmp -type l ! -name '*woody*'`
      fi
      # Build package list
      cd packages-tmp
      #dpkg-scanpackages pool /dev/null | gzip -9c >| "$d/Packages.gz"
      dpkg-scanpackages pool /dev/null | gzip -9c >| "../$d/Packages.gz"
      cd ..
      rm -Rf packages-tmp
    fi
  done
  d="dists/$dist/main/source"
  if [ -d "$d" ]; then
    # Build a fake tree
    rm -Rf packages-tmp
    for dir in `find pool -type d`; do
      mkdir -p "packages-tmp/$dir"
    done
    for file in `find pool -name '*.dsc' -o -name '*.tar.gz'`; do
      ln -s "`pwd`/$file" "packages-tmp/$file"
    done
    # Remove unwanted stuff
    if [ "$dist" != "woody" ]; then
      rm -f `find packages-tmp -type l -name '*woody*'`
    fi
    if [ "$dist" = "woody" ]; then
      rm -f `find packages-tmp -type l ! -name '*woody*'`
    fi
    # Build sources list
    cd packages-tmp
    #dpkg-scansources pool /dev/null | gzip -9c >| "$d/Sources.gz"
    dpkg-scansources pool /dev/null | gzip -9c >| "../$d/Sources.gz"
    cd ..
    rm -Rf packages-tmp
  fi
done)

#
# Update thumbnails
# requires: convert
#
update_thumbs() {(
  GENETHUMB="`pwd`/projects/genethumb/genethumb-0.0.9"
  cd "$1"
  "${GENETHUMB}" "$2" --force
)}

update_thumbs goatse thumbs.inc

#
# Update Debian
# requires: wget
#
#TABLE="debian/table.inc"
#wget 'http://qa.debian.org/developer.php?login=sam.deb@zoy.org' -O - | sed -ne '/<table width/,/table>/p' | sed -e 's/cellpadding="."/cellpadding="0"/' -e 's/cellspacing="."/cellspacing="0"/' -e 's/\(alpha\|arm\|hppa\|hurd-i386\|ia64\|i386\|m68k\|mips\|mipsel\|powerpc\|s390\|sh\|sparc\)&nbsp;/\1 /g' >| "${TABLE}.new"
#mv "${TABLE}.new" "${TABLE}"


