#!/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

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
	    ;;
	Workstation)
	    check_installed_task education-workstation
	    ;;
	Thin-Client-Server|LTSP-server)
	    check_installed_task education-thin-client-server
	    ;;
	Main-Server|Server)
	    check_installed_task education-main-server
	    ;;
	Sugar)
	    check_installed_task education-desktop-sugar
	    ;;
	*)
	    error "unknown profile '$profile'"
	    ;;
    esac
done

exit $retval
