Handle hostnames with upper-case letters
[webmin.git] / software / csw-lib.pl
1 # csw-lib.pl
2 # Functions for installing packages from Blastwave
3
4 $pkg_get = -x "/opt/csw/bin/pkgutil" ? "/opt/csw/bin/pkgutil" :
5            -x "/opt/csw/bin/pkg-get" ? "/opt/csw/bin/pkg-get" :
6            &has_command("pkgutil") ? &has_command("pkgutil") :
7                                      &has_command("pkg-get");
8
9 sub list_update_system_commands
10 {
11 return ($pkg_get);
12 }
13
14 # update_system_install([file])
15 # Install some package with pkg-get
16 sub update_system_install
17 {
18 local $update = $_[0] || $in{'update'};
19 local (@rv, @newpacks, %seen, $failed);
20
21 # Setup for non-interactive mode
22 &copy_source_dest("/var/pkg-get/admin", "/var/pkg-get/admin-old");
23 &copy_source_dest("/var/pkg-get/admin-fullauto", "/var/pkg-get/admin");
24
25 # Run pkg-get
26 $| = 1;
27 local ($failed, $retry, %already);
28 do {
29         if ($already{$update}++) {
30                 # Don't try the same update twice
31                 last;
32                 }
33         local $flag = $pkg_get =~ /pkgutil$/ ? "-y" : "-f";
34         print "<b>",&text('csw_install',
35                         "<tt>$pkg_get -i $flag $update</tt>"),"</b><p>\n";
36         $failed = 0;
37         $retry = 0;
38         print "<pre>";
39         &open_execute_command(PKGGET,
40                 "$pkg_get -i $flag ".quotemeta($update), 2);
41         while(<PKGGET>) {
42                 if (!/\s*\d+\%\s+\[/) {
43                         # Output everything except download lines
44                         print &html_escape($_);
45                         }
46                 if (/Installation of <(.*)> was successful/i) {
47                         push(@rv, $1);
48                         }
49                 elsif (/Installation of <(.*)> failed/i) {
50                         $failed = 1;
51                         }
52                 elsif (/dependancy\s+(\S+)\s+.*not up to date/i) {
53                         # Needs a dependecy .. so we will need to re-run!
54                         local $dep = $1;
55                         $update = join(" ", &unique(
56                                         $dep, split(/\s+/, $update)));
57                         $retry = 1;
58                         }
59                 elsif (/you already have version/i) {
60                         $failed = 2;
61                         }
62                 }
63         close(PKGGET);
64         print "</pre>";
65
66         if ($retry) {
67                 print "<b>$text{'csw_retry'}</b><p>\n";
68                 }
69         } while ($retry);
70
71 # Cleanup fullout file
72 &copy_source_dest("/var/pkg-get/admin-old", "/var/pkg-get/admin");
73
74 if ($failed == 1) {
75         print "<b>$text{'csw_failed'}</b><p>\n";
76         return ( );
77         }
78 elsif ($failed == 2) {
79         print "<b>$text{'csw_already'}</b><p>\n";
80         return ( );
81         }
82 else {
83         print "<b>$text{'csw_ok'}</b><p>\n";
84         if (!@rv) {
85                 # If nothing failed, assume that everything worked
86                 @rv = split(/\s+/, $update);
87                 }
88         return @rv;
89         }
90 }
91
92 # update_system_available()
93 # Returns a list of all available CSW packages
94 sub update_system_available
95 {
96 local @rv;
97 open(PKG, "$pkg_get -a |");
98 while(<PKG>) {
99         s/\r|\n//g;
100         s/#.*$//;
101         next if (/^\s*WARNING:/);
102         if (/^\s*(\S+)\s+(\S+)\s+(\d\S+)\s+([0-9\.]+)\s+(KB|MB|GB|B)/i) {
103                 # New pkgutil format
104                 push(@rv, { 'name' => $1, 'version' => $3,
105                             'select' => "$1-$3" });
106                 }
107         elsif (/^\s*(\S+)\s+(\S+)/) {
108                 # Old pkg-get format
109                 push(@rv, { 'name' => $1, 'version' => $2,
110                             'select' => "$1-$2" });
111                 }
112         }
113 close(PKG);
114 return sort { lc($a->{'name'}) cmp lc($b->{'name'}) } @rv;
115 }
116
117 # update_system_form()
118 # Shows a form for updating all packages on the system
119 sub update_system_form
120 {
121 print &ui_subheading($text{'csw_form'});
122 print &ui_form_start("csw_upgrade.cgi");
123 print &ui_submit($text{'csw_upgrade'});
124 print &ui_form_end();
125 }
126
127 1;
128