#!/mod/bin/jimsh

package require cgi
source /mod/webif/lib/setup
require pkg.class system.class

cgi_input
#cgi_dump

set cmd [cgi_get cmd update]

proc opkg {cmd} {
	puts ">>> opkg $cmd"
	set bcmd "|/mod/webif/lib/bin/opkg $cmd"
	set fd [open $bcmd r]
	while {[gets $fd line] >= 0} {
		puts "$line"
	}
	close $fd
	puts ""
}

httpheader "text/plain"

set cmdargs [lassign $cmd cmd]

set netcmds {install update upgrade}

if {$cmd in $netcmds && ![system connectivity]} {
	puts ""
	puts "!!  ERROR - No network connectivity to package repository  !!"
	puts ""
	puts "Check your Internet connection and DNS service and then try again."
	exit
}

if {$cmd eq "upgrade"} { opkg update }
opkg "$cmd $cmdargs"

if {$cmd eq "update" || $cmd eq "upgrade"} {
	puts "Updating package meta information"
	pkg fetchmeta
	puts "Done."
	puts ""
	puts "Updating diagnostic meta information"
	pkg fetchdiagmeta
	puts "Done."
}

