#!/bin/sh
#
# Set up diskless/stateless workstations for Debian Edu, using LTSP 
#
# Author: Finn-Arne Johansen
# Date:   2006-01-25

target=

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

# 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 "$target" || target=/opt/ltsp/i386
dist=sarge

if [ ! -d $target ] ; then
    echo "error: $target dont  exist. You need to run debian-edu-ltsp first"
    exit 1
fi

# Check if server uses mirror or cd to install
if apt-cache policy ltsp-client | grep -q "cdrom://" ; then 
  mirror=file:///cdrom
else
  mirror=http://ftp.debian.org/debian
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 [ "$umounts" ] ; then 
  mount --bind /cdrom  $target/cdrom
fi

mount -t proc proc  $target/proc
cat /usr/lib/debian-edu-install/defaults.common \
    /usr/lib/debian-edu-install/defaults.networked \
    /usr/lib/debian-edu-install/defaults.workstation | \
    chroot $target debconf-set-selections
debconf-get-selections | grep "^locales" | \
  chroot $target debconf-set-selections

if [ -f /etc/locale.gen -a ! -f $target/locale.gen ] ; then 
  cp /etc/locale.gen $target/etc/locale.gen 
fi

if [ -f /etc/environment -a ! -f $target/envrionment ] ; then 
  cp /etc/environment $target/etc/environment
fi

# set the timezone
cp -d /etc/localtime $target/etc 

# Make the install Silent
DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND

# install som more packages
chroot $target apt-get update
chroot $target apt-get install -qy education-tasks 

# Install all edu-dependencies
chroot $target tasksel install education-workstation

# All Packages may not exist on the CD
# Install one at a time, to get as many as possible
#ADDPKG="usbmount samba libpam-mount smbfs"
# FIXME Add Samba when we have working thin client smb.conf
ADDPKGS="usbmount libpam-mount smbfs"
for PKG in $ADDPKGS ; do
  chroot $target apt-get install -qy $PKG || /bin/true
done

test "$umounts" && umount $target/cdrom
umount $target/proc

# Kill processes that are currently running on $target
if [ "$CHROOTPART" = "$target" ] ; then 
  fuser -mkv $target/ || /bin/true
fi

# check which network the install is on, and start either as diskless
# workstation or thin client
cat <<EOF > $target/etc/init.d/ltsp_set_runlevel
#!/bin/sh

case "\$1" in
  start) ;;
  *) exit 0 ;;
esac

IP_ETH0=\$(/sbin/ifconfig eth0 | \
            sed -ne 's/ *inet addr:\([0-9.]*\) .*/\1/p')

case "\$IP_ETH0" in
  10.*)
    telinit 3 ;;
  192.*)
    telinit 4 ;;
esac

exit 0
EOF
chmod 0755 $target/etc/init.d/ltsp_set_runlevel
ln -s ../init.d/ltsp_set_runlevel $target/etc/rcS.d/S99ltsp_set_runlevel

# a lot of services should only be started in runlevel 3, eg, when
# running in workstation mode
for SERVICE in kdm xfs nscd cupsys autofs ; do 
  rm -f $target/etc/rc[245].d/S[0-9][0-9]$SERVICE
done

# alot of services should only be started in runlevel 4, eg. when running
# in thin client mode
for SERVICE in ltsp-client samba ; do 
  rm -f $target/etc/rc[235].d/S[0-9][0-9]$SERVICE
done

# Some services should not be started on the terminals
for SERVICE in munin-node cron ; do
  rm -f $target/etc/rc[2345].d/S[0-9][0-9]$SERVICE
done

## set up samba for thinclients
# Fixme - provide a working smb.conf
#ln -sf smb-thinclient-debian-edu.conf $target/etc/samba/smb.conf

# set up usbmount for terminals
ln -sf usbmount-debian-edu.conf $target/etc/usbmount/usbmount.conf

# Set up randomdevice for kdm
update-ini-file $target/etc/kde3/kdm/kdmrc 'General' RandomDevice /dev/urandom

# Create /skole, since the chroot will be read-only
mkdir -p $target/skole

# Make the ldap users availible in the chroot
chroot $target cfengine-debian-edu -Dinstallation

# Create a new resolv.conf based on the one on the host
# FixME, this will not work if server have no net during setup, and is
# not a main-server
rm -rf $target/etc/resolv.conf
sed -e 's:127.0.0.1:10.0.2.2:g' /etc/resolv.conf | \
   grep -v "^#" > $target/etc/resolv.conf
chmod 0644 $target/etc/resolv.conf

# Set up a working hosts file
rm -rf $target/etc/hosts
cp /etc/hosts $target/etc/hosts

# it's nice to have a running ssh for the admin
rm -f $target/etc/ssh/sshd_not_to_be_run

# Unmount if anything is mounted
for dir in $umounts ; do
    umount $umounts
done

umount $target/proc

#Include pammount config in /etc/pam.d/kdm
KDM_PAM=$target/etc/pam.d/kdm
KDM_ADD=$(grep -n ^auth $KDM_PAM | head -1 | cut -f1 -d:)

cat << EOF | patch -Np0 $KDM_PAM
--- $KDM_PAM
+++ $KDM_PAM.new
@@ -$KDM_ADD,0 +$KDM_ADD,2 @@
+@include common-pammount
+
EOF

# Need to set up some boot info in dhcpd.conf
# FIXME I guess this also will fail if done on a thin-client-server
# without proper connection
# But then again - the boot information should then be located on the
# main server
DHCP_OLD=/etc/dhcp3/dhcpd-debian-edu.conf
DHCP_NEW=/etc/dhcp3/dhcpd-stateless-debian-edu.conf
DHCP_ADD=
IP_ETH0=$(/sbin/ifconfig eth0 | sed -ne 's/ *inet addr:\([0-9.]*\) .*/\1/p')
NET_ETH0=$(/sbin/route -n | awk '/eth0/ { print $1 }'| head -1 )
MASK_ETH0=$(/sbin/route -n | awk '/eth0/ { print $3 }'| head -1 )
INTERNAL_NET=$(grep -n "shared-network INTERNAL" $DHCP_OLD | cut -f1 -d:)
THIN_NET=$(grep -n "shared-network THINCLIENTS"  $DHCP_OLD | cut -f1 -d:)

OPTION_END=$(head -$THIN_NET $DHCP_OLD | tail +$INTERNAL_NET  | \
             grep -ne "^ *option" | tail -1 | cut -f1 -d:)
PXEFILE="$(tail +$THIN_NET $DHCP_OLD | grep -e "^ *filename.*pxe" | head -1)"

head -$THIN_NET $DHCP_OLD | tail +$INTERNAL_NET | \
  grep -qe "^ *filename" || DHCP_ADD="$DHCP_ADD$PXEFILE\n"
head -$THIN_NET $DHCP_OLD | tail +$INTERNAL_NET | \
  grep -qe "^ *option root-path" || \
  DHCP_ADD="$DHCP_ADD  option root-path \"$IP_ETH0:$target\";\n"

if [ "$DHCP_ADD" ] ; then
  DHCP_SPLIT=$(expr $INTERNAL_NET + $OPTION_END)
  head -$DHCP_SPLIT $DHCP_OLD > $DHCP_NEW
  echo "$DHCP_ADD" >> $DHCP_NEW
  tail +$DHCP_SPLIT $DHCP_OLD >> $DHCP_NEW
  test -h /etc/dhcp3/dhcpd.conf && \
    ln -sf dhcpd-stateless-debian-edu.conf /etc/dhcp3/dhcpd.conf
  /usr/sbin/invoke-rc.d dhcp3-server restart
fi

# Export filsystem to the stateless machines
echo "$target $NET_ETH0/$MASK_ETH0(ro,async,no_root_squash)" >> /etc/exports
/usr/sbin/invoke-rc.d nfs-kernel-server restart


