# Copyright (C) 2004  Melissa K. Schrumpf
# 
# This software may be distributed freely, provided that it is
# distributed in its entirety, without modifications, and with
# the original copyright notice and license included.
# 
# You may distribute your own separate patches together with
# this software package or a modified package if you always
# include a pointer where to get the original unmodified package.
# In this case you must inform the author about the modified package.
# 
# The software may not be sold for profit or as "hidden" part of 
# another software, but it may be included with collections 
# of other free software, such as CD-ROM images of FTP servers
# and similar, provided that this software is not a significant part 
# of that collection.  In this case you must inform the author of the
# method of distribution.
# 
# Pre-compiled binaries of this software may be distributed in the
# same way, provided that this copyright notice and license is
# included without modification.
# 
# This software may be used freely, for personal and non-profit
# use, provided that the original author is always credited.  If you
# intend to use this software as a significant part of business
# (for-profit) activities, the author's permission is required.  Also,
# any usage that is not covered by this license requires the explicit
# permission of the author.
# 
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# 
# 
# 
#!/usr/local/bin/wish

global contf

proc showpkgc {} {
	global contf
	set pkn [.p2l.pkgs get [.p2l.pkgs curselection]]
	if {![catch {exec pkgcont -f $contf $pkn} res]} {
		.p2l.pkcont delete 0 end
		.p2l.pkcont insert 0 "$pkn Contents:"
		foreach {i} $res {
			.p2l.pkcont insert end "  -  $i"
		}
	} else {
		tk_messageBox -icon info -type ok -title "Package" -message "ERR:\n$res"
	}
}

proc showphil {fp} {
	global contf
	if {![catch {exec pkgquery -f $contf $fp} res]} {
		.p2l.pkcont delete 0 end
		.p2l.pkcont insert 0 "$fp is part of package(s):"
		set respl [split $res "\n"]
		foreach {i} $respl {
			if {[string length $i]!=0} {
				.p2l.pkcont insert end "  -  $i"
			}
		}
	} else {
		tk_messageBox -icon info -type ok -title "Package" -message "ERR:\n$res"
	}
}



if {$argc>2 || $argc==1} {
	tk_messageBox -icon info -type ok -title "Package" -message "Usage:\n\n\tpkgcont \[-f contents-file\] file(s)...\n"
	exit 1
} elseif {$argc>0 && [lindex $argv 0]!="-f"} {
	tk_messageBox -icon info -type ok -title "Package" -message "Usage:\n\n\tpkgcont \[-f contents-file\] file(s)...\n"
	exit 1
} else {
	if {[lindex $argv 0]=="-f"} {
		set contf [lindex $argv 1]
	} else {
		set contf "/var/sadm/install/contents"
	}
	if {![file readable $contf]} {
		tk_messageBox -icon info -type ok -title "Package" -message "File not accessible: $contf"
		exit 1
	}
}

wm title . "pkgmap  -  $contf"

set syspkg {}
set fid [open $contf r]
while {![eof $fid]} {
	set str [gets $fid]
	set ll [split $str]
	if {[llength $ll]!=0 && [lindex $ll 0]!="#"} {
		set pkn [lindex $ll end]
		if {[lsearch -exact $syspkg $pkn]==-1} {
			lappend syspkg $pkn
		}
	}
}
close $fid

set syspkg [lsort -decreasing $syspkg]

frame .p2l
	listbox .p2l.pkgs -yscroll ".p2l.scroll set" -setgrid 1
	scrollbar .p2l.scroll -command ".p2l.pkgs yview"
	label .p2l.div -width 1
	listbox .p2l.pkcont -yscroll ".p2l.scroll2 set" -setgrid 1
	scrollbar .p2l.scroll2 -command ".p2l.pkcont yview"
pack .p2l -side top -expand true -fill both
	pack .p2l.pkgs -side left -expand false -fill y
	pack .p2l.scroll -side left -fill y
	pack .p2l.div -side left -expand false -fill none
	pack .p2l.pkcont -side left -expand true -fill both
	pack .p2l.scroll2 -side left -fill y
	foreach {i} $syspkg {
		.p2l.pkgs insert 0 $i
	}
	.p2l.pkgs selection set 0
	bind .p2l.pkgs <Double-Button-1> {showpkgc}
	bind .p2l.pkgs <Return> {showpkgc}
	
frame .p2lb
pack .p2lb -side top -expand false -fill x
	button .p2lb.pkgget -text "list package contents" -command {showpkgc}
	pack .p2lb.pkgget -side top



label .lab -text "Query File" -anchor w
entry .phil -width 40 -textvariable phile
button .ffind -text "File Package Association" -command {showphil $phile}
frame .div -height 20
pack .div -side top
pack .lab -side top
pack .phil -side top -pady 5 -padx 10
pack .ffind -side top
bind .phil <Return> {showphil $phile}



