1950Sjkh#!/usr/local/bin/wish -f
250479Speter#$FreeBSD$
3950Sjkh#
4950Sjkhset pkgname ""
5950Sjkhwm title . "Package Installation"
6950Sjkh#--------------------------------------------------------------
7950Sjkh# The top level main window, consisting of a bar of buttons and a list
8950Sjkh# of packages and a description of the current package.
9950Sjkh#--------------------------------------------------------------
10950Sjkhframe .menu -relief raised -borderwidth 1
11950Sjkhframe .frame -borderwidth 4
12950Sjkh
13950Sjkhscrollbar .frame.scroll -relief sunken -command ".frame.list yview"
14950Sjkhlistbox .frame.list -yscroll ".frame.scroll set" -relief sunken -setgrid 1
15950Sjkhpack append .frame .frame.scroll {right filly} \
16950Sjkh        .frame.list {left expand fill}
17950Sjkh
18950Sjkh# build the lower window shoing the complete description of a pacage
19950Sjkhframe .f -borderwidth 4
20950Sjkhtext .f.t -width 80 -height 20 -yscrollcommand ".f.s set" -relief sunken
21950Sjkh
22950Sjkh# Initially display instructions in this window.  Erase the
23950Sjkh# instructions and show the package description when the user clicks
24950Sjkh# on a package.
25950Sjkh# 
264996Sjkh.f.t insert end "Double click on a package above to see its
27950Sjkhcomplete description here."
28950Sjkhscrollbar .f.s -relief sunken -command ".f.t yview"
29950Sjkhpack append .f .f.s {right filly} .f.t {left expand fill}
30950Sjkh
31950Sjkhbind .frame.list <Double-Button-1> \
32950Sjkh    {foreach i [selection get] {do_description $i}}
33950Sjkhpack append .  .menu {top fill} \
34950Sjkh   .f {bottom expand fill} \
35950Sjkh   .frame {bottom expand fill}
36950Sjkh
37950Sjkh#----------------------------------------------------------------
38950Sjkh# Make menu bar:
39950Sjkh#----------------------------------------------------------------
40950Sjkhbutton .menu.inst -text "Install" \
41950Sjkh   -command "apply_to_pkg \"pkg_add -v\""
42950Sjkhbutton .menu.dein -text "Deinstall" \
43950Sjkh   -command "apply_to_pkg \"pkg_delete -v\""
44950Sjkhbutton .menu.installed -text "What is Installed?" \
45950Sjkh   -command "list_pkgs \"pkg_info -I -a |tr '	' ' '\""
46950Sjkhbutton .menu.available -text "What can I install?" \
47950Sjkh   -command "list_pkgs \"pkg_info -I -c [glob -nocomplain *.{tgz,tar.z,tar.gz,tar.Z}] |tr '	' ' '\""
48950Sjkhbutton .menu.cont -text "Contents?" \
494996Sjkh   -command "apply_to_pkg \"pkg_info -d -v\""
50950Sjkhbutton .menu.quit -text "Quit" -command "destroy ."
51950Sjkhbutton .menu.help -text "Help" -command "do_help"
52950Sjkh
53950Sjkhpack append .menu \
54950Sjkh  .menu.inst left \
55950Sjkh  .menu.dein left \
56950Sjkh  .menu.installed left \
57950Sjkh  .menu.available left \
58950Sjkh  .menu.cont left \
59950Sjkh  .menu.quit left \
60950Sjkh  .menu.help right
61950Sjkh#-------------------------------------------------------
62950Sjkh# Display the package description.
63950Sjkh#-------------------------------------------------------
64950Sjkhproc list_pkgs {s} {
65950Sjkh  set line ""
664996Sjkh  set f [eval "open {| sh -c \"$s\" } r"]
67950Sjkh  .frame.list delete 0 end
68950Sjkh  while {[gets $f line] > 0} {
69950Sjkh    .frame.list insert end $line
70950Sjkh  }
71950Sjkh  close $f
72950Sjkh}
73950Sjkh
74950Sjkh# display the list of available packages
75950Sjkhset archives [glob -nocomplain *.{tgz,tar.z,tar.gz,tar.Z}]
764996Sjkhif {$archives == ""} {
77950Sjkh  .frame.list delete 0 end
78950Sjkh .frame.list insert end "Warning: no compressed tar archives files found."
79950Sjkh} else {
80950Sjkh  list_pkgs "pkg_info -I -c $archives |tr '	' ' '"
81950Sjkh}
82950Sjkh
83950Sjkh#-------------------------------------------------------
84950Sjkh# Display the package description.
85950Sjkh#-------------------------------------------------------
86950Sjkhproc do_description {s} {
87950Sjkh  global pkgname
88950Sjkh  regexp {[^ 	]*} $s filename
89950Sjkh  set pkgname $filename
90950Sjkh  .f.t delete 0.0 end
91950Sjkh  set cmd "pkg_info -d $filename |tr -d ''"
92950Sjkh  set f [eval "open {| csh -c \"$cmd\" } r"]
93950Sjkh  while {![eof $f]} {
94950Sjkh    .f.t insert end [read $f]
95950Sjkh  }
96950Sjkh}
97950Sjkh#-------------------------------------------------------
98950Sjkh# package install window.
99950Sjkh#-------------------------------------------------------
100950Sjkhproc do_help {{w .help}} {
101950Sjkh  catch {destroy $w}
102950Sjkh  toplevel $w
103950Sjkh  wm title $w "Help"
104950Sjkh  wm iconname $w "Help"
105950Sjkh  button $w.ok -text OK -command "destroy $w"
106950Sjkh  message $w.t -relief raised -bd 2 \
1074996Sjkh    -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."
108950Sjkh  pack append $w $w.ok {bottom fillx} $w.t {expand fill}
109950Sjkh}
110950Sjkh#-------------------------------------------------------
111950Sjkh# Apply a command to a package.
112950Sjkh#-------------------------------------------------------
113950Sjkhproc apply_to_pkg {s} {
1144996Sjkh    apply_to_pkg_err $s ""
1154996Sjkh}
1164996Sjkh#-------------------------------------------------------
1174996Sjkh# Apply a command to a package, with error stream redirection instructions.
1184996Sjkh#-------------------------------------------------------
1194996Sjkhproc apply_to_pkg_err {s errredir} {
120950Sjkh  global pkgname
121950Sjkh  .f.t delete 0.0 end
122950Sjkh  if {$pkgname == ""} {
123950Sjkh    .f.t insert end "You must double click on a package name first!"
124950Sjkh  } else {
1254996Sjkh    apply_to_pkg_int "$s $pkgname" "2>&1"
1264996Sjkh  }
1274996Sjkh}
1284996Sjkhproc apply_to_pkg_int {s errredir} {
129950Sjkh    .f.t delete 0.0 end
1304996Sjkh    .f.t insert end "Running: $s\n"
1314996Sjkh    set f [eval "open {| sh -c \"$s $errredir\" } r"]
132950Sjkh    while {![eof $f]} {
133950Sjkh      .f.t insert end [read $f 64]
134950Sjkh    }
135950Sjkh}
136950Sjkh#-------------------------------------------------------
137950Sjkh# Invoke an arbitrary command.
138950Sjkh#-------------------------------------------------------
139950Sjkhproc do_command {s} {
140950Sjkh  .f.t delete 0.0 end
141950Sjkh  .f.t insert end "Running: $s\n"
142950Sjkh  set f [eval "open {| $s} r"]
143950Sjkh  while {![eof $f]} {
144950Sjkh    .f.t insert end [read $f 64]
145950Sjkh  }
146950Sjkh}
147950Sjkh# local variables:
148950Sjkh# mode: csh
149950Sjkh# compile-command: ""
150950Sjkh# comment-start: "# "
151950Sjkh# comment-start-skip: "# "
152950Sjkh# end:
153