#!/bin/sh
#
# Set up LTSP environment for Debian Edu
#
# Author: Petter Reinholdtsen
# Date:   2005-10-08

base=
dist=

while [ $# -gt 0 ] ; do 
  case "$1" in 
    --base) base="$2" ; shift ;;
    --dist) dist="$2" ; shift ;;
    --arch) arch="$2" ; shift ;;
  esac
  shift
done

if [ -z "$base" -a -z "$arch" ] ; then 
  case $(uname -m) in
    ppc) ARCH=powerpc ;;
    i*86) ARCH=i386 ;;
    x86_64) ARCH=amd64 ;;
    *) echo "Unknown Architecture, please fix the script $0"
       exit 9
       ;;
  esac
elif [ -n "$arch" ] ; then
  ARCH = $arch
fi

# Make the installation a bit more quiet when started from the command line
# The locale is not setup in the thin client chroot (yet?)
LC_ALL=C
export LC_ALL

test "$base" || base=/opt/ltsp
test "$dist" || dist=etch

if [ -d $base/$ARCH ] ; then
    echo "error: $base/$ARCH already exist.  Remove it before running $0"
    echo "       If you want to install in parallell with an old installation, "
    echo "       please use --base <new location> as an option"
    exit 1
fi

# Check if server uses install or cd to install
if apt-cache policy ltsp-client | grep -q "cdrom://" ; then 
  mirror=file:///cdrom
  components="main local"
  securitymirror=""
  extramirror=""
else
  mirror=http://ftp.debian.org/debian
  extramirror="http://ftp.skolelinux.no/skolelinux $dist local"
  securitymirror="http://security.debian.org/ $dist/updates main contrib"
  components="main contrib"
fi

# Mount the CD ROM if needed
case $mirror in
    file:///cdrom)
        mount /cdrom
	umounts="/cdrom"
	;;
    file:///media/cdrom)
        mount /media/cdrom
	umounts="/media/cdrom"
	;;
    *)
        ;;
esac

if ltsp-build-client --help 2>&1 |grep -q -- --root ; then
    # Old version
    ltspopts="--root $base/$ARCH"
else
    # new version (at least >= 0.93debian1)
    ltspopts="--accept-unsigned-packages --base /opt/ltsp --arch $ARCH"
fi

# Build LTSP chroot, fetch packages from default APT sources

#workaround until new ltsp packages in etch
export NOSECURITY=true

ltsp-build-client \
  $ltspopts \
  --mirror $mirror \
  --dist $dist \
  --components "$components" \
  --security-mirror "$securitymirror" \
  --extra-mirror "$extramirror"


# Copy X KBD settings from the server
debconf-show xserver-xfree86 | \
  sed -n 's#\* \(xserver.*/keyboard/.*\): #xserver-xfree86 \1 string #p' | \
  chroot $base/$ARCH debconf-set-selections
debconf-show xserver-xorg | \
  sed -n 's#\* \(xserver.*/keyboard/.*\): #xserver-xorg \1 string #p' | \
  chroot $base/$ARCH debconf-set-selections

# Workaround for Skolelinux bug #1035
test -f $base/$ARCH/usr/share/ldm/themes/Debian-Edu/logo-trans.png && \
  test ! -r $base/$ARCH/usr/share/ldm/themes/Debian-Edu/background.png && \
  ln -s logo-trans.png  \
        $base/$ARCH/usr/share/ldm/themes/Debian-Edu/background.png || /bin/true

for dir in $umounts ; do
    umount $umounts
done

#populate /var/lib/tftpboot with kernel images and pxelinux configuration
if [ -f /usr/sbin/ltsp-update-kernels ] ; then
     ltsp-update-kernels
fi
