#!/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=$(lsb_release -sc)

if [ -d $base/$ARCH ] ; then
    echo "error: $base/$ARCH already exists."
    echo "       Remove the chroot before running $0 again."
    echo "       To install a chroot for a specific architecture,"
    echo "       please use --arch <i386|amd64> as an option."
    echo "       If you want to install in parallell with an old installation,"
    echo "       use --base <new location> --arch <i386|amd64> as options."

    exit 1
fi

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

umounts=
exit_handler() {
    # Unmount if anything is mounted
    for dir in $umounts ; do
	info "Unmounting $dir"
	umount $dir || true
    done
}
trap exit_handler EXIT INT

# 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

ltsp-build-client --eatmydata --accept-unsigned-packages --base $base --arch $ARCH --dist $dist
