#!/bin/bash
#
# $Id: debian-edu-lessdisks 2878 2005-02-16 20:43:22Z finnarne-guest $
# A Script to install lessdisks on a debian-edu-installation
# 

set -e 

# Set some defaults
LESSDISKSARCHIVE=""
SECURITYARCHIVE=""
NONUSARCHIVE=""
ARCH=i386
DIST=sarge
PROXY=${http_proxy}
PROFILE=""

# Set umask for creating safe temporary files
umask 022

if grep -qe "^deb cdrom:.*local main" /etc/apt/sources.list ; then 
  DEFAULTARCHIVE="file:///cdrom/ $DIST main local"
  DISKXTERM="384"
  DISKWS="2048"
  FROMCD=true
  while umount /cdrom ; do : ; done || true
else
  DEFAULTARCHIVE="http://ftp.skolelinux.no/debian/"
  LESSDISKSARCHIVE="http://ftp.skolelinux.no/skolelinux/ $DIST local"
  DISKXTERM="384"
  DISKWS="2560"
fi

# Added support for installing as xterminal or diskless workstation
case "$1" in 
  xterm*|x-term*) 
    PROFILE=""
    MINSIZE=$DISKXTERM
    COPYDIRS="/var/cache/debconf /var/lib/discover"
    ;;
  ws*|work*)
    PROFILE=""
    MINSIZE=$DISKWS
    COPYDIRS="/var/cache/debconf /var/lib/discover /var/lib/gdm /etc/cups"
    ;;
  *)
    echo "Please specify profile"
    echo "Usage:"
    echo "  $0 <profile>"
    echo "were profile means"
    echo " xterminal - normal lessdisks xterminal (LTSP-replacement)"
    echo " workstation - Using a diskless machine as a full workstation"
    exit 9
    ;;
esac


# check if we already have lessdisks installed
if [ -f /var/lib/lessdisks/var/lib/dpkg/info/base-config.list ] ; then 
  echo "Looks like you already have lessdisks installed - I'm bailing out."
  exit 9 
fi

# if we dont have a partition for lessdisks already, try to create one
if ! mount | grep -q "/var/lib/lessdisks" ; then 
  lvcreate -L $(echo "$MINSIZE * 1.1" | bc)  -n lv_lessdisks vg_system
  mke2fs -j /dev/vg_system/lv_lessdisks
  mkdir -p /var/lib/lessdisks
  echo "/dev/vg_system/lv_lessdisks /var/lib/lessdisks ext3 defaults 0 2" \
       >> /etc/fstab
  mount /var/lib/lessdisks
fi

# prepare to export the lessdisks partition 
if ! grep -qe "^/var/lib/lessdisks .*" /etc/exports ; then 
  echo "/var/lib/lessdisks 192.168.0.0/255.255.255.0(ro,async,no_root_squash)" \
     >> /etc/exports
  /etc/init.d/nfs-kernel-server reload
fi

# We want to run lessdisks installation noninteractivly
export lessdisks_noninteractive=true

if [ ! -d /var/lib/lessdisks ] ; then 
  echo "Error: You need a /var/lib/lessdisks"
  exit 9
fi

if [ $(df -Pm /var/lib/lessdisks/ | grep lessdisks | awk '{ print $4 }') -lt $MINSIZE ] ; then 
  echo "There should be at least $MINSIZE MB availible space for lessdisks" 
  exit 9
fi


# Preseed the server config for lessdisks
sed -e "s+%%DIST%%+$DIST+g" \
    -e "s+%%NONUSARCHIVE%%+$NONUSARCHIVE+g" \
    -e "s+%%LESSDISKSARCHIVE%%+$LESSDISKSARCHIVE+g" \
    -e "s+%%ARCH%%+$ARCH+g" \
    -e "s+%%DEFAULTARCHIVE%%+$DEFAULTARCHIVE+g" \
    -e "s+%%PROXY%%+$PROXY+g" \
    -e "s+%%SECURITYARCHIVE%%+$SECURITYARCHIVE+g" \
    /usr/lib/debian-edu-install/defaults.lessdisks-server | \
    debconf-set-selections 

# Install the server part of lessdisks
apt-get -qy install lessdisks

if [ ! -x /usr/sbin/lessdisks-install ] ; then 
  echo "Error: Cant find lessdisks-install"
  exit 9 
fi 

# mount the cdrom
if [ "$FROMCD" ] ; then 
  mount /cdrom
fi

# Create the preseed file for the chroot
TMPSEED=$(mktemp /tmp/lessdisks-seed.XXXXXX) || exit 9
sed -e "s+%%PROFILE%%+$PROFILE+g" \
    -e "s+%%COPYDIRS%%+$COPYDIRS+g" \
    /usr/lib/debian-edu-install/defaults.lessdisks-xterminal > $TMPSEED

# Install into the lessdisks chroot
LANG=C lessdisks-install --skip -d cmdline --seed=$TMPSEED

# Stop processes that are using /var/lib/lessdisks (thy is one)
if mount | grep -q /var/lib/lessdisks ; then 
  fuser -mk /var/lib/lessdisks || /bin/true
fi

# Fix locale config
if [ -f /etc/locale.gen ] ; then 
  [ -f /etc/environment ] && cp /etc/environment /var/lib/lessdisks/etc
  cp /etc/locale.gen /var/lib/lessdisks/etc/
  LANG=C lessdisks-chroot -- dpkg-reconfigure -pcritical locales
fi

# Fix Console Keymap 
if [ -f /etc/console/boottime.kmap.gz ] ; then 
  mkdir -p  /var/lib/lessdisks/etc/console/
  cp /etc/console/boottime.kmap.gz  /var/lib/lessdisks/etc/console/
fi

# Fix X keymap
debconf-show xserver-xfree86 | grep layout | \
   sed -e 's:^.:xserver-xfree86:g' \
       -e 's/:/ string/g' | \
           lessdisks-chroot -- debconf-set-selections || /bin/true
	   
# Fetch Debian-edu config (if it exists)
if [ -f /etc/debian-edu/config ] ; then 
  . /etc/debian-edu/config
fi


# Added support for installing as xterminal or diskless workstation
case "$1" in 
  xterm*|x-term*) 
    TASK="education-xterminal"
    PROFILE=XTerminal
    ;;
  ws*|work*)
    TASK="education-workstation"
    PROFILE=Workstation
    ;;
  *) 
    echo "This should never happen"
    exit 9
    ;;
esac

# mount the make the cdrom availible within the chroot
if [ "$FROMCD" ] ; then 
  mount --bind /cdrom /var/lib/lessdisks/mirrors/archives/default
fi

# create a /etc/debian-edu/config
mkdir -p /var/lib/lessdisks/etc/debian-edu
cat <<EOF > /var/lib/lessdisks/etc/debian-edu/config
# Created by $(basename $0)
NONINTERACTIVE="true"
PROFILE="$PROFILE"
LANGCODE="$LANGCODE"
LOCALE="$LANG"
EOF

# install the basic debian-edu packages
lessdisks-chroot -- apt-get -qy install \
                                debian-edu-install \
				education-tasks \
				localization-config

# make sure that the server (or its services) is not restarted during
# the installation into the chroot.
for RESTART in debian-edu-reboot debian-edu-finish xdebconfigurator ; do 
  SCRIPT=/var/lib/lessdisks/usr/lib/base-config/menu/$RESTART
  if [ -f $SCRIPT ] ; then 
    cp $SCRIPT $SCRIPT.org
    cat <<EOF > $SCRIPT
#!/bin/bash

#nothing to do here in the lessdisks-chroot
exit 0
EOF
  fi
done 

# Dont fetch packages from cd, use the already defined sources
# Which may very well be a cd, but ...
cp /var/lib/lessdisks/usr/lib/base-config/menu/debian-edu-auto-to-inst \
   /var/lib/lessdisks/usr/lib/base-config/menu/debian-edu-auto-to-inst.org
sed "s:^jump_to 130 debian-edu-probe-cd$:jump_to 130 debian-edu-inst:" \
   /var/lib/lessdisks/usr/lib/base-config/menu/debian-edu-auto-to-inst.org >\
   /var/lib/lessdisks/usr/lib/base-config/menu/debian-edu-auto-to-inst

# turn on shadow passwords
lessdisks-chroot -- shadowconfig on

# Copy password from outside lessdisks chroot
( grep ^root: /etc/shadow ; 
  grep -v ^root: /var/lib/lessdisks/etc/shadow ) | \
  > /var/lib/lessdisks/etc/shadow.new
mv /var/lib/lessdisks/etc/shadow.new /var/lib/lessdisks/etc/shadow
# Then do the installation:
lessdisks-chroot base-config

lessdisks-chroot -- debconf-set-frontend Noninteractive
case "$PROFILE" in 
  XTerminal)
    lessdisks-aptget -- -qy install x-window-system-core xdebconfigurator
    # Start up X and query LTSP-server
    cat << EOF > /var/lib/lessdisks/etc/init.d/Xterminal.sh
#!/bin/bash

case "\$1" in 
  start)
    /usr/bin/X11/X -query ltspserver.intern &
    ;;
esac
EOF

    ln -s /etc/init.d/Xterminal.sh /var/lib/lessdisks/etc/rc2.d/S99Xterminal.sh

    ;;
  Workstation)
    # Workaround for KDM bug #96772 in KDE
    lessdisks-chroot -- /usr/bin/update-ini-file /etc/kde3/kdm/kdmrc \
                                   'General' RandomDevice '/dev/urandom'
    #lessdisks-aptget -- -qy install gdm
    #echo "/usr/bin/gdm" > /var/lib/lessdisks/etc/X11/default-display-manager
    ;;
esac

lessdisks-chroot -- debconf-set-frontend Dialog

# Installation done - unmount cd if it was used
if [ "$FROMCD" ] ; then 
  while umount /cdrom ; do : ; done || true 
fi

# unmount things that were mounted during base-config
# Got to find why this happens
grep "/var/lib/lessdisks/" /var/lib/lessdisks/etc/mtab | \
  tac | awk '{ print $2 }' | xargs -rn1 umount  || /bin/true


# Replace XF86Config created during installation with a symlink to
# /var/state/lessdisks
ln -sf /var/state/lessdisks/etc/XF86Config-4 /var/lib/lessdisks/etc/X11/XF86Config-4

# Make sure chroot has a working /etc/resolv.conf
if grep -qe "^nameserver\ 127\.0\.0\.1$" /etc/resolv.conf ; then 
  cat << EOF > /var/lib/lessdisks/etc/resolv.conf
search intern
nameserver 10.0.2.2 
EOF
else
  cp /etc/resolv.conf /var/lib/lessdisks/etc/
fi

# Generating XF86Config using xdebconfigurator && dexconf at boottime
cat << EOF > /var/lib/lessdisks/etc/init.d/boot_xconf.sh
#!/bin/bash

MACADDR=\$(ifconfig  eth0 | sed -n 's/.*HWaddr //p')

case "\$1" in 
  start)
    if [ -f /etc/lessdisks/terminals/XF86Config-4.\$MACADDR ] ; then
      ln -s /etc/lessdisks/terminals/XF86Config-4.\$MACADDR /tmp/XF86Config-4
    elif [ -f /etc/lessdisks/terminals/XF86Config.\$MACADDR ] ; then
      ln -s /etc/lessdisks/terminals/XF86Config.\$MACADDR /tmp/XF86Config-4
    elif [ -f /etc/lessdisks/terminals/XF86Config-4.default ] ; then
      ln -s /etc/lessdisks/terminals/XF86Config-4.default /tmp/XF86Config-4
    elif [ -f /etc/lessdisks/terminals/XF86Config.default ] ; then
      ln -s /etc/lessdisks/terminals/XF86Config.default /tmp/XF86Config-4
    else
      /usr/sbin/xdebconfigurator -di && /usr/bin/dexconf
    fi
    ;;
esac
EOF

ln -s /etc/init.d/boot_xconf.sh /var/lib/lessdisks/etc/rc2.d/S98boot_xconf.sh


# Move kernel and initrd in place for pxe-booting
cp /var/lib/lessdisks/boot/vmlinuz /tftpboot/lts/vmlinuz-ld
cp /var/lib/lessdisks/boot/initrd.img /tftpboot/lts/initrd.img-ld

# Move kernel in place for use with etherboot
cp /var/lib/lessdisks/boot/vmlinuz.nb /tftpboot/lts/vmlinuz.nb-ld

# Create a nice "menu" to present to the user
cat << EOF > /tftpboot/lts/pxemenu




     Please enter ltsp or lessdisks
       ltsp - Normal boot of ltsp
       lessdisks - Use Lessdisks as a $PROFILE

     Default is ltsp

EOF

# Extend the boot-menu for PXE-bootable machines
mv /tftpboot/lts/pxelinux.cfg/default /tftpboot/lts/pxelinux.cfg/ltsp-only

cat << EOF > /tftpboot/lts/pxelinux.cfg/default
DISPLAY pxemenu
TIMEOUT 100
DEFAULT ltsp
EOF

sed -e "s:^label linux$:label ltsp:g" \
    -e "s:^prompt=0$:prompt 1:g" \
    /tftpboot/lts/pxelinux.cfg/ltsp-only >> /tftpboot/lts/pxelinux.cfg/default
cat << EOF >> /tftpboot/lts/pxelinux.cfg/default
label lessdisks
  kernel vmlinuz-ld
  append initrd=initrd.img-ld rw root=/dev/nfs ip=dhcp nfsroot=/var/lib/lessdisks
EOF

# Give a short message of what's left to do ....
cat << EOF
# There is more work to it:
# - Fixing automount of homedir from tjener
#   (This should work given that autofs-ldap works)
# - setting up access to local cdrom
# - I guess there is other things

EOF

