#!/bin/sh

install_unixtime_kernel()
{
	local line="$( opkg status kernel | grep ^Installed-Time: )"
	echo "${line#*: }"
}

unixtime_is_circa_kernel_install_unixtime()
{
	local unixtime="$1"
	local kernel_unixtime="$2"
	local threshold=60 		# lookaround in seconds

	local min=$(( $kernel_unixtime - $threshold ))
	local max=$(( $kernel_unixtime + $threshold ))

	test $unixtime -gt $min -a $unixtime -lt $max
}

list_all_userinstalled_packages()
{
	local line unixtime package
	local kernel_unixtime="$( install_unixtime_kernel )"

	opkg status | while read line; do
		case "$line" in
			"Package:"*)
				package="${line#*: }"
			;;
			"Installed-Time:"*)
				unixtime="${line#*: }"
				unixtime_is_circa_kernel_install_unixtime "$unixtime" "$kernel_unixtime" || {
					echo "$package"
				}
			;;
		esac
	done
}

list_all_userinstalled_packages
