1#!/usr/local/bin/wish -f
2#$FreeBSD$
3#
4set pkgname ""
5wm title . "Package Installation"
6#--------------------------------------------------------------
7# The top level main window, consisting of a bar of buttons and a list
8# of packages and a description of the current package.
9#--------------------------------------------------------------
10frame .menu -relief raised -borderwidth 1
11frame .frame -borderwidth 4
12
13scrollbar .frame.scroll -relief sunken -command ".frame.list yview"
14listbox .frame.list -yscroll ".frame.scroll set" -relief sunken -setgrid 1
15pack append .frame .frame.scroll {right filly} \
16        .frame.list {left expand fill}
17
18# build the lower window shoing the complete description of a pacage
19frame .f -borderwidth 4
20text .f.t -width 80 -height 20 -yscrollcommand ".f.s set" -relief sunken
21
22# Initially display instructions in this window.  Erase the
23# instructions and show the package description when the user clicks
24# on a package.
25# 
26.f.t insert end "Double click on a package above to see its
27complete description here."
28scrollbar .f.s -relief sunken -command ".f.t yview"
29pack append .f .f.s {right filly} .f.t {left expand fill}
30
31bind .frame.list <Double-Button-1> \
32    {foreach i [selection get] {do_description $i}}
33pack append .  .menu {top fill} \
34   .f {bottom expand fill} \
35   .frame {bottom expand fill}
36
37#----------------------------------------------------------------
38# Make menu bar:
39#----------------------------------------------------------------
40button .menu.inst -text "Install" \
41   -command "apply_to_pkg \"pkg_add -v\""
42button .menu.dein -text "Deinstall" \
43   -command "apply_to_pkg \"pkg_delete -v\""
44button .menu.installed -text "What is Installed?" \
45   -command "list_pkgs \"pkg_info -I -a |tr '	' ' '\""
46button .menu.available -text "What can I install?" \
47   -command "list_pkgs \"pkg_info -I -c [glob -nocomplain *.{tgz,tar.z,tar.gz,tar.Z}] |tr '	' ' '\""
48button .menu.cont -text "Contents?" \
49   -command "apply_to_pkg \"pkg_info -d -v\""
50button .menu.quit -text "Quit" -command "destroy ."
51button .menu.help -text "Help" -command "do_help"
52
53pack append .menu \
54  .menu.inst left \
55  .menu.dein left \
56  .menu.installed left \
57  .menu.available left \
58  .menu.cont left \
59  .menu.quit left \
60  .menu.help right
61#-------------------------------------------------------
62# Display the package description.
63#-------------------------------------------------------
64proc list_pkgs {s} {
65  set line ""
66  set f [eval "open {| sh -c \"$s\" } r"]
67  .frame.list delete 0 end
68  while {[gets $f line] > 0} {
69    .frame.list insert end $line
70  }
71  close $f
72}
73
74# display the list of available packages
75set archives [glob -nocomplain *.{tgz,tar.z,tar.gz,tar.Z}]
76if {$archives == ""} {
77  .frame.list delete 0 end
78 .frame.list insert end "Warning: no compressed tar archives files found."
79} else {
80  list_pkgs "pkg_info -I -c $archives |tr '	' ' '"
81}
82
83#-------------------------------------------------------
84# Display the package description.
85#-------------------------------------------------------
86proc do_description {s} {
87  global pkgname
88  regexp {[^ 	]*} $s filename
89  set pkgname $filename
90  .f.t delete 0.0 end
91  set cmd "pkg_info -d $filename |tr -d ''"
92  set f [eval "open {| csh -c \"$cmd\" } r"]
93  while {![eof $f]} {
94    .f.t insert end [read $f]
95  }
96}
97#-------------------------------------------------------
98# package install window.
99#-------------------------------------------------------
100proc do_help {{w .help}} {
101  catch {destroy $w}
102  toplevel $w
103  wm title $w "Help"
104  wm iconname $w "Help"
105  button $w.ok -text OK -command "destroy $w"
106  message $w.t -relief raised -bd 2 \
107    -text "You can install, deinstall and list info on the available packages.  To select a package and see its complete description, press mouse button 1 over the package name.  To install a selected package, press the Install button.  To exit, press the \"Quit\" button."
108  pack append $w $w.ok {bottom fillx} $w.t {expand fill}
109}
110#-------------------------------------------------------
111# Apply a command to a package.
112#-------------------------------------------------------
113proc apply_to_pkg {s} {
114    apply_to_pkg_err $s ""
115}
116#-------------------------------------------------------
117# Apply a command to a package, with error stream redirection instructions.
118#-------------------------------------------------------
119proc apply_to_pkg_err {s errredir} {
120  global pkgname
121  .f.t delete 0.0 end
122  if {$pkgname == ""} {
123    .f.t insert end "You must double click on a package name first!"
124  } else {
125    apply_to_pkg_int "$s $pkgname" "2>&1"
126  }
127}
128proc apply_to_pkg_int {s errredir} {
129    .f.t delete 0.0 end
130    .f.t insert end "Running: $s\n"
131    set f [eval "open {| sh -c \"$s $errredir\" } r"]
132    while {![eof $f]} {
133      .f.t insert end [read $f 64]
134    }
135}
136#-------------------------------------------------------
137# Invoke an arbitrary command.
138#-------------------------------------------------------
139proc do_command {s} {
140  .f.t delete 0.0 end
141  .f.t insert end "Running: $s\n"
142  set f [eval "open {| $s} r"]
143  while {![eof $f]} {
144    .f.t insert end [read $f 64]
145  }
146}
147# local variables:
148# mode: csh
149# compile-command: ""
150# comment-start: "# "
151# comment-start-skip: "# "
152# end:
153