#!/bin/sh
#
# nitdroid-uninstaller		Uninstall NITdroid kernels and multiboot
#
#	Copyright (c) 2010  Jay Cartman <jay.cartman@gmail.com>
#

VERSION=0.3


## Functions

init_fake_and()
{
	install -d -m 0700 /and
	mount -t tmpfs -o rw,nosuid,noatime,size=256k,mode=755 tmpfs /and
	install -d /and/system/etc
}

done_fake_and()
{
	umount /and
}


purge()
{
	apt-get -y purge "$@"
}


## Check OK?

if [ $(id -u) != '0' ]
then
	echo "You must be root to run this program."
	echo "Type 'root' to become root and try again"
	exit 1
fi


## Startup message

cat << EOM

**********************************************************
*                                                        *
* This uninstaller will automatically REMOVE NITDroid    *
*          and Multiboot from your device.               *
*                                                        *
*                       WARNING!                         *
*    THIS PROGRAM WILL REMOVE APPLICATIONS AND FILES     *
*        FROM YOUR DEVICE WITHOUT ANY WARNINGS.          *
*   THERE IS A SLIGHT RISK THAT THIS WILL RENDER YOUR    *
*     DEVICE UNUSABLE, REQUIRING FULL MAEMO REFLASH.     *
*                                                        *
*      PLEASE BACKUP YOUR DATA BEFORE PROCEEDING         *
*                                                        *
*                 YOU HAVE BEEN WARNED                   *
*                                                        *
**********************************************************

EOM

read -t 60 -p "Type 'YES' to continue >" ans

if [ "${ans}" != "YES" ]
then
	echo "You typed '${ans}' instead of 'YES'"
	echo "OK - not doing anything."
	exit 1
fi


## Init fake /and partition - won't touch the real MMC

init_fake_and


## Remove old kernels

for list in /var/lib/dpkg/info/nitdroid-kernel-*.list
do
	if [ -f $list ]
	then
		pkg=${list##*/nitdroid-kernel-}
		ver=${pkg%%.list}

		echo "Removing package nitdroid-kernel-$ver"

		purge nitdroid-kernel-$ver
	fi
done


## Remove manual kernels

for mod in /lib/modules/2.6.28.NIT.*
do
	if [ -d $mod ]
	then
		ver=${mod##*/}

		echo "Removing manually installed nitdroid-kernel-$ver"

		rm -f /boot/vmlinuz-$ver
		rm -f /boot/multiboot/vmlinuz-$ver
		rm -f -r $mod
	fi
done


## Remove multiboot

purge multiboot-kernel-maemo 
purge multiboot-kernel-power

purge multiboot
purge multiboot-extras

purge nitdroid-installer


## Remove any leftover files

if [ -d /etc/multiboot.d ]
then
	echo "Removing /etc/multiboot.d"
	rm -f -r /etc/multiboot.d
fi

for file in /sbin/preinit[._]* /sbin/multiboot[._]*
do
	if [ -f $file ]
	then
		echo "Removing $file"
		rm -f $file
	fi
done


## Done

done_fake_and


cat << 'EOM'

**********************************************************
                   _____   ___   ___   ___      ___
      |\   ||  /| /____/  |   \ |   \ |   \ /| |   \ 
      | \  ||  ||   |     |   | |___/ |   | || |   |
      || \ ||  ||   ||    ||  | ||\\  ||  | || ||  |
      ||  \||  ||   ||    ||__/ || \\ ||__| || ||__/

**********************************************************
*                                                        *
*           has been succesfully REMOVED!                *
*                                                        *
*    You may now format your MicroSD card to remove the  *
*                NITDroid partitions.                    *
*                                                        *
**********************************************************

EOM


exit 0
