#!/bin/sh

#set -x

abs_path()
{
	echo "$(cd "${1%/*}" 2>/dev/null && echo "$PWD"/"${1##*/}")" || exit 1
}

where_am_i()
{
	local realme="$1"
	while [ -L "$realme" ]; do
		realme=`readlink "$realme"`
	done
	echo "$(cd "${realme%/*}" 2>/dev/null && pwd)" || exit 1
}

# this script's parent dir
HERE="$(where_am_i "$0")"

ShowUsage()
{
  cat <<-End

Usage: $0 [install|remove|versions]

'install' installs Palm PDK packages onto the device.
'remove' removes the Palm PDK packages from the device.
'versions' shows the versions of the currently-installed packages.
It requires a USB-connected Palm Pre or Palm Pixi device.
Developer Mode must already be enabled on the device.

End
}

NEW_PACKAGES="dropbear"
SHOW_VERSIONS=0

case "$1" in
	"versions")
		SHOW_VERSIONS=1
		PKG_DIR="${HERE}/Native-SDK"
		REMOVE_PKGS=0
		;;
	"install")
		PKG_DIR="${HERE}/Native-SDK"
		REMOVE_PKGS=0
		;;
	"remove")
		PKG_DIR="${HERE}/Albacore"
		REMOVE_PKGS=1
		;;
	*)	ShowUsage
		exit 1
		;;
esac

if [ -n "$2" ]; then
	DEVICE_TYPE="$2"
else
	echo ERROR: Device type not specified.
	exit 1
fi

# Make sure we can find (or are running from) the script dir
if [ ! -d "${PKG_DIR}" ] ; then
	echo "ERROR: Required Palm PDK packages not found."
	exit 1
fi

# Make sure novacom is installed
if [ -z "`which novacom`" ] ; then
	echo "ERROR: Novacom must be installed in order for this script to work."
	exit 1
fi

case "$DEVICE_TYPE" in
	"castle")
		PACKAGES=""
		;;
	"pixie")
		PACKAGES=""
		;;
	*)
		echo "ERROR: Device type must be castle or pixie."
		exit 1
		;;
esac
echo

if [ ${REMOVE_PKGS} -eq 1 ] ; then
	echo "This script will remove PDK packages from your Palm ${DEV_NAME}."
else
	if [ ${SHOW_VERSIONS} -ne 1 ] ; then
		echo "This script will install PDK packages onto your Palm ${DEV_NAME}."
	fi
	# if we're installing, add the new packages to the list
	PACKAGES="${PACKAGES} ${NEW_PACKAGES}"
fi

if [ ${SHOW_VERSIONS} -ne 1 ] ; then
	echo
	echo "Note that your device will be rebooted after this script completes."
fi

# Display version information for currently-installed packages
PKG_PATH="${PKG_DIR}/${DEVICE_TYPE}"
for PKG in ${PACKAGES} ; do
	echo -n "Checking package ${PKG}: "
	VERSION=`/usr/bin/mmipkg info ${PKG} | grep Version | cut -d' ' -f2`
	if [ -n "$VERSION" ] ; then
		echo "(device has ${VERSION})"
	else
		# some packages are new and won't already exist on the device
		echo "(not on device)"
	fi
	# sanity check: make sure we have a package by this name
	PACKAGE=`find ${PKG_PATH} -name "${PKG}_*"`
	if [ -z ${PACKAGE} ] ; then
		echo "ERROR: Required package ${PKG} missing!"
		exit 1
	fi
done

if [ ${SHOW_VERSIONS} -eq 1 ] ; then
	echo "Done."
	echo
	exit 0
fi

# Enable writing to rootfs
/usr/sbin/rootfs_open

# Remove ${NEW_PACKAGES} if we're removing (uninstalling PDK)
if [ ${REMOVE_PKGS} -eq 1 ] ; then
	echo
	echo "Removing unneeded packages..."
	/usr/bin/mmipkg remove ${NEW_PACKAGES}
fi

PKG_LIST=""
# Install new packages, in the required ${PACKAGES} order (not random/filesystem order)
for PKG in ${PACKAGES} ; do
	PACKAGE=`find ${PKG_PATH} -name "${PKG}_*.ipk"`
	PKG_LIST="${PKG_LIST} ${PACKAGE}"
	if [ -z ${PACKAGE} ] ; then
		echo "ERROR: Required package ${PKG} missing!"
		exit 1
	fi
done

#echo "Beginning package install: ${PKG_LIST}"
echo "Beginning package install..."
/usr/bin/mmipkg install ${PKG_LIST} 2>&1 | while read line ; do echo $line | grep -v "^$" | grep -v "^ " | grep -v "ldconfig" | grep -v "not a symbolic" | grep -v ": Linking" ; done

# Sync rootfs (should make rootfs_open finish faster...)
echo
echo "NOTE: Do not disconnect device until you see \"Done.\" message below!"
echo
echo "Syncing buffered data to flash..."
/bin/sync

# Disable writing to rootfs (reboots device)
echo
echo "Rebooting device..."
echo "NOTE: Do not disconnect device until you see \"Done.\" message below!"
/usr/sbin/rootfs_open -r

echo "Done."
echo
