#!/bin/sh

# Helper script for the Update Notifier Applet.
#
# Installs pending updates with apt-get [dist-]upgrade or
# aptitude safe-/full-upgrade, resets the applet's state
# and finally prompts the user for confirmation when finished.

PKGSFILE=/var/lib/un-applet/pkgnames
STATEFILE=/var/lib/un-applet/state
CMD="dist-upgrade"
DIST_UPGRADE="1"
PM_PROGRAM="apt-get"

TITLE=`gettext -d un-applet "Installing updates..."`
/bin/echo -ne  "\033]0;$TITLE\007"

if [ -e /etc/un-applet/un-applet.conf ]; then
    . /etc/un-applet/un-applet.conf
fi

if [ "$PM_PROGRAM" = "aptitude" ]; then
    if [ -z `which aptitude` ]; then

        echo `gettext -d un-applet "You configured un-applet to use aptitude to install available updates"`","
        echo `gettext -d un-applet "however aptitude does not seem to be installed on your system."`
        echo
        echo `gettext -d un-applet "Possible solutions:"`
        echo "    "`gettext -d un-applet "1. use apt-get instead of aptitude to install the available updates now."`
        echo "    "`gettext -d un-applet "2. do nothing and install the available updates later."`

        n=0
        while [ $n -le 3 ]; do
            echo -n `gettext -d un-applet "Please enter your choice"`" (1 | 2):  "
            read choice
            case $choice in
                1)
                    n=4
                    PM_PROGRAM="apt-get";;
                2)
                    TITLE=`gettext -d un-applet "Update cancelled"`
                    /bin/echo -ne "\033]0;$TITLE\007"
                    echo  `gettext -d un-applet "Software update has been cancelled."`
                    echo -n `gettext -d un-applet "Please press Enter to close the window."`
                    read whatever
                    exit 0;;
                *)
                    n=$((n+1))
                    PM_PROGRAM="none"
                    echo `gettext -d un-applet "Invalid input"`": \"$choice\"";;
            esac
        done
        if [ "$PM_PROGRAM" = "none" ]; then
            TITLE=`gettext -d un-applet "Update cancelled"`
            /bin/echo -ne "\033]0;$TITLE\007"
            echo  `gettext -d un-applet "Software update has been cancelled."`
            echo -n `gettext -d un-applet "Please press Enter to close the window."`
            read whatever
            exit 0
        fi
        echo
    fi
fi

if [ "$DIST_UPGRADE" = "0" ]; then
    if [ "$PM_PROGRAM" = "aptitude" ]; then
        CMD="safe-upgrade"
    else
        CMD="upgrade"
    fi
else
    if [ "$PM_PROGRAM" = "aptitude" ]; then
        CMD="full-upgrade"
    fi
fi

echo `gettext -d un-applet "Running command:"`" $PM_PROGRAM $CMD"
echo
$PM_PROGRAM $CMD

case "$?" in
    "0")
        echo "0" > $STATEFILE
        echo "" > $PKGSFILE
        TITLE=`gettext -d un-applet "Upgrade finished"`
        /bin/echo -ne "\033]0;$TITLE\007"
        echo
        echo `gettext -d un-applet "Software upgrade successfully finished."`;;
    *)
        echo "-3" > $STATEFILE
        echo "" > $PKGSFILE
        TITLE=`gettext -d un-applet "Error"`
        /bin/echo -ne "\033]0;$TITLE\007"
        echo
        echo `gettext -d un-applet "Software upgrade finished with error(s)."`;;
esac

echo -n `gettext -d un-applet "Please press Enter to close the window."`
read whatever
#echo
#un-applet-update-pkgdatabase -i

exit 0
