#!/mod/bin/jimsh

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

if {[[settings] pkgdev]} { set filter 0 } else { set filter 1 }

pkg loadmeta

proc nav {active} {
	set q "<div data-role=navbar data-id=pkg_navbar>\n <ul>\n"
	foreach f {installed available upgrades} {
		append q "  <li><a"
		if {$f eq $active} {
			append q { class="ui-btn-active ui-state-persist"}
		}
		append q " href=pkg[string range $f 0 0]_b.jim>"
		append q "[string totitle $f]</a></li>\n"
	}
	append q " </ul>\n</div>\n"
	return $q
}

proc pkgtop {type} {
	puts "
	<div data-role=page id=pkga>
		<div data-role=header data-theme=b>
			[_header]
			[nav $type]
		</div>
		<div data-role=content>
	"
}

proc pkgrow {type pkg} {
	set name [$pkg get name]
	puts "<li class=\"pkg_$name\"><h3>$name</h3>"

	if {$type eq "avail"} {
		puts "<p class=ui-li-aside>[$pkg get latest]</p>"
	} else {
		puts "<p class=ui-li-aside>[$pkg get installed]</p>"
	}

	if {$type eq "upgr"} {
		puts "<p>Latest: [$pkg get latest]</p>"
	}

	if {[dict exists $::pkgmeta $name]} {
		puts "<p class=wsn>$::pkgmeta($name)</p>"
	} else {
		puts "<p class=wsn>[$pkg get descr]</p>"
	}

	if {![$pkg is installed]} {
		set btype Install
	} elseif {$type eq "upgr" && [$pkg is upgradable]} {
		set btype Upgrade
	} else {
		set btype Remove
	}

	puts "
 <p>
    <a data-inline=true data-role=button data-mini=true data-theme=e
     href=#
     action=[string tolower $btype] pkg=\"$name\"
     class=[string tolower $btype]>$btype</a>
 </p>
	"

	puts "</li>"
}

proc pkglist {type} {
	set pkgs [pkg $type]

	puts "<ul data-role=listview>"

	if {[llength $pkgs] <= 0} {
		puts "<li><i>No packages "
		switch $type {
			inst { puts "are installed." }
			upgr { puts "are available for upgrade; try updating the package list from the Internet using the button above." }
			avail { puts "are available for installation." }
		}
		puts "</i></li></ul>"
		return
	}

	foreach pkg $pkgs {
		if {$type ne "upgr" && $::filter && \
		    ![dict exists $::pkgmeta $pkg]} { continue }
		pkgrow $type [pkg load $pkg]
	}
	if {$::filter} {
		puts "<li data-role=list-divider>This is a filtered package list. To show all packages, enable the <i>Show development and advanced packages</i> in the settings screen.</li>"
	}
	puts "</ul>"
}

mheader

switch $pageid {
	pkgi_b {
		set type installed
		set qtype inst
	}
	pkga_b {
		set type available
		set qtype avail
	}
	default {
		set type upgrades
		set qtype upgr
	}
}

pkgtop $type

puts {
<ul data-role=listview>
<li>
	<button data-theme=e class=opkg_update>
		Update package list from Internet
	</button>
}

if {$pageid eq "pkgu_b"} {
	puts {
		<button data-theme=e class=opkg_upgrade>
			Upgrade all packages
		</button>
	}
}

puts {
</li>
</ul>
}

pkglist $qtype

puts {
<div data-role=popup class="opkg_popup ui-corner-all" data-theme=e>
	<div data-role=header data-theme=a class=ui-corner-top>
		<h1>Results</h1>
	</div>
	<div data-role=content data-theme=d class="ui-corner-bottom ui-content">
		<p class="pre opkg_popup_text" data-theme=d></p>
	</div>
	<center><div class="hidden opkg_op_complete">
		<img border=0 src=/images/167_2_00_Check_W2_SUB.png>
		Operation complete.
		<a href=# data-role=button data-inline=true data-rel=back
		    data-mini=true data-theme=b>Close</a>  
	</div></center>
</div>
}

mfooter

