#!/bin/sh
#
# Check LTSP chroot installation (if amd64, i386 or even both archs are available).

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

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

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

for LTSPARCH in amd64 i386 ; do
    if echo "$PROFILE" | grep -q LTSP-Server && [ -e /opt/ltsp/images/$LTSPARCH.img ]; then
        chroot="/opt/ltsp/$LTSPARCH"
        if [ -f $chroot/etc/lts.conf ] ; then
	    success "Found LTSP $LTSPARCH chroot"
        else
	    error "LTSP $LTSPARCH 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 $LTSPARCH chroot"
	    else
	        error "package $pkg is not installed in LTSP $LTSPARCH 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 $LTSPARCH chroot"
	    else
	        error "package $pkg is not installed in LTSP $LTSPARCH 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 ; do
	    if cmp -s $path  $chroot$path ; then
	        success "$path is identical inside and outside LTSP $LTSPARCH"
	    else
	        error "$path is different inside and outside LTSP $LTSPARCH"
	    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/$LTSPARCH cat /etc/resolv.conf | cmp -s /etc/resolv.conf ; then
	    success "resolv.conf is identical inside and outside LTSP $LTSPARCH"
        else
	    error "resolv.conf differ inside and outside LTSP $LTSPARCH"
        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 NBD is configured for LTSP $LTSPARCH chroot.
        if [ -e /opt/ltsp/images/$LTSPARCH.img ] \
            && [ -f /etc/nbd-server/conf.d/ltsp_$LTSPARCH.conf ] ; then
            echo "success: $0: found NBD image and configuration for LTSP $LTSPARCH"
        else
            echo "error: $0: NBD configuration for LTSP $LTSPARCH is broken"
        fi
    fi
done
