#!/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/rpcbind fixes
#
# TODO: move this script into a feature subpackage, see also
# http://www.freesource.info/wiki/TZ/AltLinux/WhiteLabel
# 
# 2007, 2008, 2009, 2010 Michael Shigorin <mike@altlinux.org>

. install2-init-functions

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

MOUNTED=
LBCCONF="$destdir/etc/ltsp/ltsp-build-client.conf"
cdrom_stage3="${cdrom_stage3:-/media/cdrom}"	 # where l-b-c expects it

mount_image()
{
	MOUNTED="$destdir/$cdrom_stage3"
	mkdir -p "$MOUNTED"
	mount -o bind /image "$MOUNTED" || MOUNTED=
}

case "$METHOD" in
cdrom|nfs)
	mount_image;;
*)
	echo "$0: installation method '$METHOD' not supported" >&2
	echo "$0: you might have to ltsp-build-client yourself" >&2
	exit 0;;	# would have to pass custom sources.list down then
esac

# 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",' "$LBCCONF" ||:

# use thin client flavour with patches against nfs swap deadlocks
#grep ^KERNEL= "$LBCCONF" || echo 'KERNEL="kernel-image-tmc-tc"' >> "$LBCCONF"
# FIXME: kludge (std-def won't boot on 32M x86_64 thin client btw)
#if [ "`arch`" = i686 ]; then
#	echo 'KERNEL="kernel-image-ltsp-client"' >> "$LBCCONF"
#else
#	echo 'KERNEL="kernel-image-std-def"' >> "$LBCCONF"
#fi
#echo 'KERNEL_MODULES="alsa compcache drm fglrx nvidia"' >> "$LBCCONF"
echo 'KERNEL_MODULES="alsa drm sound"' >> "$LBCCONF"

# mc :(  nx :)
echo 'LATE_PACKAGES="mc ltsp-client-nxsession"' >> "$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 ||:

# enable XDMCP
for kdmrc in $destdir/etc/X11/kdm{,4}/kdmrc; do
	[ -f "$kdmrc" ] && sed -ir 's/^Enable=false/Enable=true/' "$kdmrc" ||:
done
# gdm
[ -f "$destdir/etc/control.d/facilities/gdm_xdmcp" ] \
&& run_chroot control gdm/xdmcp enabled ||:
# lightdm
CONF="$destdir/etc/lightdm/lightdm.conf"
[ -f "$CONF" ] && sed -i -e '/^\[XDMCPServer\]/aenabled=true' "$CONF" ||:

# chkconfig&restart services needed
# tftp is xinetd-based, service has nothing to do there
# NB: also done in 06-pxe.sh
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 rpcbind on ||:
run_chroot chkconfig nfs 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

# ask syslog to accept thin clients' logs
sed -i "s,^SYSLOGD_OPTIONS=',&-r ," $destdir/etc/sysconfig/syslogd

# 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" ||:
