#!/bin/sh -e
#
# Check to make sure the correct profile packages are installed

installed="$(dpkg --get-selections | awk '/\tinstall$/ {print $1}')"

# Check if a deb package is installed.  Return true if it is, and
# false if it isn't. (copied from debian-edu-install)
deb_installed() {
    echo $installed | grep -qw $1
}

check_installed() {
    deb=$1
    if deb_installed "$deb" ; then
         echo "success: $0: Package $deb is installed."
    else
        echo "error: $0: Package $deb is not installed!"
        retval=1
    fi
}

check_installed_task() {
    task=$1
    found=false
    for deb in $(tasksel --task-packages $task); do
        found=true
        if deb_installed "$deb" ; then
             echo "success: $0: Package $deb in task $task is installed."
        else
            echo "error: $0: Package $deb in task $task is not installed!"
            retval=1
        fi
    done
    if [ false = $found ] ; then
            echo "error: $0: Task $task is empty, tasksel --task-packages $task return nothing!"
            retval=1
    fi
}

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

# Check if the desktop type selection worked
check_desktop_task() {
    desktop=$(debconf-show tasksel |tr -d "*" |awk '/tasksel\/desktop/ {print $2}')
    pkg="education-desktop-$desktop"
    if deb_installed "$pkg" ; then
	echo "success: $0: Desktop package $pkg is installed."
    else
	echo "error: $0: Desktop package $pkg is not installed."
    fi
}


retval=0

check_installed education-common
check_installed education-tasks

for value in `echo $PROFILE |sed 's/ /-/g' | sed 's/,-/ /g'`; do
    case $value in
	Standalone)
	    check_installed_task education-standalone
	    check_desktop_task
	    ;;
	Workstation)
	    check_installed_task education-workstation
	    check_desktop_task
	    ;;
	Roaming-Workstation)
	    check_installed_task education-roaming-workstation
	    check_desktop_task
	    ;;
	Thin-Client-Server|LTSP-server)
	    check_installed_task education-thin-client-server
	    check_desktop_task
	    ;;
	Main-Server|Server)
	    check_installed_task education-main-server
	    ;;
	Minimal)
	    check_installed_task education-networked
	    ;;
	*)
	    echo "error: $0: unknown profile '$profile'"
	    ;;
    esac
done

exit $retval
