#!/bin/sh
# first shot at setting up LTSP5
# see alsp http://www.freesource.info/wiki/Dokumentacija/LTSP5
#
# currently depends on postinstall.d/06-pxe.sh for tftp/portmap fixes
#
# TODO: move this script into a feature subpackage, see also
# http://www.freesource.info/wiki/TZ/AltLinux/WhiteLabel
# 
# 2007, 2008 Michael Shigorin <mike@altlinux.org>

. install2-init-functions

[ -z "$AUTOINSTALL" ] || exit 0

MOUNTED=
LBCCONF="$destdir/etc/ltsp/ltsp-build-client.conf"

[ "$METHOD" = "cdrom" -a -n "$CDROMDEV" ] && {
	MOUNTED="$destdir/$cdrom_stage3"
	mkdir -p "$MOUNTED"
	mount -o ro "$CDROMDEV" "$MOUNTED" || MOUNTED=
}

# FIXME: probably ltsp-build-client should be fixed not to interfere 
# with system apt configuration and caches
cp -a $destdir/var/lib/apt/lists/ $destdir/tmp/

# obsolete module might still be mentioned -- replace it
subst 's,r1000,r8168,' "$LBCCONF" ||:
# use installation media, not ftp.altlinux.org
subst 's,MIRROR=.*,MIRROR="rpm file:/media/cdrom ALTLinux main ltsp",' "$LBCCONF" ||:
# use thin client flavour with patches against nfs swap deadlocks
grep ^KERNEL= "$LBCCONF" || echo 'KERNEL="kernel-image-led-tc"' >> "$LBCCONF"

mkdir -p $destdir/root/.ltsp-log/ ||:
# exec_chroot doesn't suppress stdout *and* resets environment
#exec_chroot ltsp-build-client 2>&1 | tee $destdir/root/.ltsp-log/ltsp-build-client.log
# don't do env *-i*
env PATH="/sbin:/usr/sbin:$PATH" HOME="/root" TMPDIR="/tmp" chroot "$destdir" ltsp-build-client 2>&1 \
| tee $destdir/root/.ltsp-log/ltsp-build-client.log

# FIXME: /dev/console somehow gets lost :-(
# very much likely that something's overly eager with cleanup
cp -a /dev/console $destdir/var/lib/ltsp/i586/dev/console ||:

# configure base services for 192.168.0.1/24 on some interface
# FIXME: LTSP network prefix should become configurable some day...
cp -a $destdir/etc/ltsp/dhcpd.conf $destdir/etc/dhcp ||:
subst 's/only_from = 127.0.0.1/only_from = 192.168.0.0\/24/' $destdir/etc/xinetd.conf ||:
subst 's/-port -1 //' $destdir/etc/sysconfig/xfs ||:
subst 's/ -nolisten tcp//' $destdir/etc/X11/xinit/xserverrc ||:

# KDM pls (TODO: other dms too in case they are?)
sed -ir 's/^Enable=false/Enable=true/' $destdir/etc/X11/kdm/kdmrc ||:

# chkconfig&restart services needed
# tftp is xinetd-based, service has nothing to do there
# NB: also done in 06-pxe.sh
run_chroot chkconfig nfs on ||:
run_chroot chkconfig dm on ||:
run_chroot chkconfig xfs on ||:
run_chroot chkconfig tftp on ||:
run_chroot chkconfig dhcpd on ||:
run_chroot chkconfig xinetd on ||:
run_chroot chkconfig portmap on ||:

# add localdev support -- we're on hardware not in VE okay?
grep -q ^fuse $destdir/etc/modules || echo fuse >> $destdir/etc/modules
run_chroot control fusermount public ||:

# FIXME: all hardwired, but a lot of stuff needs to be reconfigured either
# (when ltsp network is going to be configurable); so:
# care for resolving in the most crude manner possible
grep -q ^192.168.0.1 $destdir/etc/hosts || echo "192.168.0.1 server server.ltsp" >> $destdir/etc/hosts
grep -q ^192.168.0.250 $destdir/etc/hosts || {
        for i in `seq 20 250`; do
                echo "192.168.0.$i ws$i ws$i.ltsp" >> $destdir/etc/hosts
        done
}
run_chroot update_chrooted conf

# prepare mini CD image to boot PXE-less thin clients
run_chroot ltsp-mkbootiso -o /opt/ltsp-boot.iso ||:

# FIXME: workaround for tricky aptwork in ltsp-build-client, part II
rm -f $destdir/var/cache/apt/*.bin ||:
rm -f $destdir/var/cache/apt/gen{src,pkg}list/* ||:
rm -rf $destdir/var/lib/apt/lists/ ||:
mv $destdir/tmp/lists/ $destdir/var/lib/apt ||:

[ -n "$MOUNTED" ] && umount "$MOUNTED"
