#!/bin/sh
#
# Check the LTSP installation.

LTSPARCH=`ltsp-arch-debian-edu`

PROFILE=
if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

success() {
    echo "success: $0: $*"
}

error() {
    echo "error: $0: $*"
}

if echo "$PROFILE" | grep -q Thin-Client-Server ; then
    chroot="/opt/ltsp/$LTSPARCH"
    if [ -f $chroot/etc/lts.conf ] ; then
	success "Found LTSP chroot"
    else
	error "LTSP chroot is missing in $chroot/"
    fi

    # Verify that the wanted packages are installed, first thin clients
    for pkg in $(tasksel --task-packages education-thin-client | sort) ; do
	if chroot $chroot dpkg -l $pkg > /dev/null 2>&1 ; then
	    success "package $pkg is installed in LTSP chroot"
	else
	    error "package $pkg is not installed in LTSP chroot"
	fi
    done

    # and then diskless workstation
    for pkg in education-workstation ; do
	if chroot $chroot dpkg -l $pkg > /dev/null 2>&1 ; then
	    success "package $pkg is installed in LTSP chroot"
	else
	    error "package $pkg is not installed in LTSP chroot"
	fi
    done

    # Ensure the profile file exist and is properly set for a diskless
    # workstation
    if [ -f $chroot/etc/debian-edu/config ] ; then
        ( # In a subshell to avoid keeping the values in this script
            . $chroot/etc/debian-edu/config
            if [ Workstation != "$PROFILE" ] ; then
                error "Missing PROFILE=Workstation in $chroot/etc/debian-edu/config."
            else
		success "PROFILE=Workstation is set in $chroot/etc/debian-edu/config."
            fi
        )
    else
        error "Missing $chroot/etc/debian-edu/config"
    fi

    for path in /etc/ldap/ssl/ldap-server-pubkey.pem \
	/etc/iceweasel/profile/cert_override.txt ; do
	if cmp -s $path  $chroot$path ; then
	    success "$path is identical inside and outside LTSP"
	else
	    error "$path differ inside and outside LTSP"
	fi
    done

    # Need to cat the file in the chroot to chech the current content,
    # in case resolvconf converted resolv.conf into a symlink.
    # The resolv.conf files should be identical to make sure apt can
    # look up DNS entries and upgrade packages in the chroot.
    if chroot /opt/ltsp/i386 cat /etc/resolv.conf | cmp -s /etc/resolv.conf ; then
	success "resolv.conf is identical inside and outside LTSP"
    else
	error "resolv.conf differ inside and outside LTSP"
    fi

    # Verify that IP-forwarding is enabled by init.d/enable-nat
    if [ 1 = "`cat /proc/sys/net/ipv4/ip_forward`" ]; then
	success "IPv4 forwarding is enabled"
    else
	error "IPv4 forwarding is not enabled"
    fi

    # Verify that the default LTSP configuration is available in LDAP
    if ldapsearch -x "(&(ltspConfig=*)(cn=ltspConfigDefault))" > /dev/null 2>&1
    then
	echo "success: $0: found LTSP default settings in LDAP"
    else
	echo "error: $0: LTSP default settings are missing in LDAP"
    fi

    # Verify that the default LTSP configuration is available in LDAP.
    # Bug somewhere caused it to only accept connections from
    # localhost.
    if nc "$(uname -n)" 9571 | grep -q ^session: ; then
	echo "success: $0: able to connect to ldinfod"
    else
	echo "error: $0: not able to connect to ldinfod"
    fi

    # Test if the LTSP chroots can be NFS mounted
    myname=$(uname -n)
    foundnfs=false
    for m in $(showmount -e $myname | grep /opt/ltsp | awk '{print $1}') ; do
	if mount $myname:$m /mnt; then
	    echo "success: $0: mounting $myname:$m succeeded"
	    umount /mnt
	    foundnfs=true
	else
	    echo "error: $0: unable to mount $myname:$m"
	fi
    done
    if [ false = "$foundnfs" ] ; then
	echo "error: $0: unable to find any NFS mount points on $myname"
    fi
fi
