#!/bin/sh
tmp=/tmp/kernel-deinstall
cat > $tmp <<EOF
This program will remove the enhanced power-user Linux kernel
and restore the original Nokia kernel.
The settings will be kept but will have no effect.
Warning: This deinstallation method is currently experimental and could lead to a bricked device.
For a safe but manual deinstallation method please visit http://wiki.maemo.org/Kernel_Power

For the deinstallation please make sure that you are connected to
the Internet and that Application Manager is not running.

If you want keep the enhanced kernel, cancel the installation now
by tapping the blurred area above this dialog.
Otherwise confirm the dialog to proceed with the deinstallation.

After the installation, you need to unplug the USB cable,
completely shutdown your device, and boot again to activate the original Nokia kernel.
EOF
maemo-confirm-text "Kernel-Power Flasher" $tmp
res=$?
rm -f $tmp
dbus="run-standalone.sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications"
dbusmsg () { $dbus org.freedesktop.Notifications.SystemNoteInfoprint string:"$1"; }
dbusbox () { $dbus org.freedesktop.Notifications.SystemNoteDialog string:"$1" uint32:0 string:OK; }

if test "$res" != 0; then
    dbusmsg "User has cancelled the deinstallation. No changes were made."
    exit 1
fi

if pgrep -f /usr/bin/hildon-application-manager > /dev/null; then
    dbusbox "Error: Application Manager is running. Please close it before deinstallation."
    exit 1
fi
if pgrep -f apt-worker > /dev/null; then
    dbusbox "Error: apt is running. Please wait and try again later."
    exit 1
fi

dbusmsg "Please wait while restoring Nokia kernel..."

apt-get install --reinstall -y kernel kernel-flasher > $tmp
ok=$?
cat $tmp
if grep -qE "(Could not resolve host|cannot be downloaded)" $tmp; then
    dbusbox "Error during deinstallation: Could not download Nokia kernel. Please make sure the internet connection is working."
    rm $tmp
    exit 1
fi
if test $ok = 0 && grep -q "Image flashed successfully" $tmp; then :
else
    dbusbox "Error during deinstallation: Could not install Nokia kernel."
    rm $tmp
    exit 1
fi
dbusmsg "Nokia kernel was restored. Removing enhanced kernel packages..."
apt-get remove -y kernel-power kernel-power-modules > $tmp
ok=$?
cat $tmp
if test $ok = 0; then
    dbusbox "Kernel sucessfully deinstalled. Nokia kernel was restored."
else
    dbusbox "Warning: Nokia kernel was restored but could not remove enhanced kernel."
fi
rm $tmp
exit $ok
