Initial checkin of Webmin
[webmin.git] / oschooser.pl
1 # oschooser.pl
2 # Read the list of operating systems and ask the user to choose
3 # an OS and version
4 # auto param: 0 = always ask user
5 #             1 = automatic, give up if fails
6 #             2 = automatic, ask user if fails
7 #             3 = automatic, ask user if fails and if a TTY
8
9 $| = 1;
10
11 ($oslist, $out, $auto) = @ARGV;
12 open(OS, $oslist) || die "failed to open $oslist : $!";
13 while(<OS>) {
14         chop;
15         if (/^([^\t]+)\t+([^\t]+)\t+([^\t]+)\t+([^\t]+)\t*(.*)$/) {
16                 push(@list, [ $1, $2, $3, $4, $5 ]);
17                 push(@names, $1) if (!$donename{$1}++);
18                 $names_to_real{$1} ||= $3;
19                 }
20         }
21 close(OS);
22
23 if ($auto) {
24         # Try to guess the OS name and version
25         if (-r "/etc/.issue") {
26                 $etc_issue = `cat /etc/.issue`;
27                 }
28         elsif (-r "/etc/issue") {
29                 $etc_issue = `cat /etc/issue`;
30                 }
31         $uname = `uname -a`;
32         foreach $o (@list) {
33                 if ($o->[4] && eval "$o->[4]") {
34                         # Got a match! Resolve the versions
35                         $ver = $o;
36                         if ($ver->[1] =~ /\$/) {
37                                 $ver->[1] = eval "($o->[4]); $ver->[1]";
38                                 }
39                         if ($ver->[3] =~ /\$/) {
40                                 $ver->[3] = eval "($o->[4]); $ver->[3]";
41                                 }
42                         last;
43                         }
44                 if ($@) {
45                         print STDERR "Error parsing $o->[4]\n";
46                         }
47                 }
48
49         if (!$ver) {
50                 if ($auto == 1) {
51                         # Failed .. give up
52                         print "Failed to detect operating system\n";
53                         exit 1;
54                         }
55                 elsif ($auto == 3) {
56                         # Do we have a tty?
57                         local $rv = system("tty >/dev/null 2>&1");
58                         if ($?) {
59                                 print "Failed to detect operating system\n";
60                                 exit 1;
61                                 }
62                         else {
63                                 $auto = 0;
64                                 }
65                         }
66                 else {
67                         # Ask the user
68                         $auto = 0;
69                         }
70                 }
71         }
72
73 if (!$auto) {
74         if (0 && &has_command("dialog")) {
75                 # call the dialog command to ask for the OS (disabled for now)
76                 $cmd = "dialog --menu \"Please select your operating system type from the list below\" 20 60 12";
77                 for($i=0; $i<@names; $i++) {
78                         $cmd .= " ".($i+1)." '$names[$i]'";
79                         }
80                 $tmp_base = $ENV{'tempdir'} || "/tmp/.webmin";
81                 $temp = "$tmp_base/dialog.out";
82                 system("$cmd 2>$temp");
83                 $osnum = `cat $temp`;
84                 $osnum = int($osnum);
85                 if (!$osnum) {
86                         #unlink($temp);
87                         print "ERROR: No operating system selected\n\n";
88                         exit 9;
89                         }
90
91                 # call the dialog command to ask for the version
92                 $name = $names[$osnum-1];
93                 @vers = grep { $_->[0] eq $name } @list;
94                 $cmd = "dialog --menu \"Please select your operating system's version from the list below\" 20 60 12";
95                 for($i=0; $i<@vers; $i++) {
96                         $cmd .= " ".($i+1)." '$name $vers[$i]->[1]'";
97                         }
98                 system("$cmd 2>$temp");
99                 $vnum = `cat $temp`;
100                 $vnum = int($vnum);
101                 unlink($temp);
102                 if (!$vnum) {
103                         print "ERROR: No operating system version selected\n\n";
104                         exit 9;
105                         }
106                 $ver = $vers[$vnum-1];
107                 }
108         else {
109                 # ask for the operating system name ourselves
110                 $dashes = "-" x 75;
111                 print <<EOF;
112 For Webmin to work properly, it needs to know which operating system
113 type and version you are running. Please select your system type by
114 entering the number next to it from the list below
115 $dashes
116 EOF
117                 for($i=0; $i<@names; $i++) {
118                         printf " %2d) %-20.20s ", $i+1, $names[$i];
119                         print "\n" if ($i%3 == 2);
120                         }
121                 print "\n" if ($i%3);
122                 print $dashes,"\n";
123                 print "Operating system: ";
124                 chop($osnum = <STDIN>);
125                 if ($osnum !~ /^\d+$/) {
126                         print "ERROR: You must enter the number next to your operating\n";
127                         print "system, not its name or version number.\n\n";
128                         exit 9;
129                         }
130                 if ($osnum < 1 || $osnum > @names) {
131                         print "ERROR: $osnum is not a valid operating system number.\n\n";
132                         exit 10;
133                         }
134                 print "\n";
135
136                 # Ask for the operating system version
137                 $name = $names[$osnum-1];
138                 print <<EOF;
139 Please enter the version of $name you are running
140 EOF
141                 print "Version: ";
142                 chop($vnum = <STDIN>);
143                 if ($vnum !~ /^\S+$/) {
144                         print "ERROR: An operating system number cannot contain\n\n";
145                         print "spaces. It must be like 2.1 or ES4.0.\n";
146                         exit 10;
147                         }
148                 print "\n";
149                 $ver = [ $name, $vnum,
150                           $names_to_real{$name}, $vnum ];
151                 }
152         }
153
154 # Write the name, version and real name and version to a file
155 open(OUT, ">$out");
156 print OUT "os_type='",$ver->[2],"'\n";
157 print OUT "os_version='",$ver->[3],"'\n";
158 print OUT "real_os_type='",$ver->[0],"'\n";
159 print OUT "real_os_version='",$ver->[1],"'\n";
160 close(OUT);
161
162 # has_command(command)
163 # Returns the full path if some command is in the path, undef if not
164 sub has_command
165 {
166 local($d);
167 if (!$_[0]) { return undef; }
168 local $rv = undef;
169 if ($_[0] =~ /^\//) {
170         $rv = (-x $_[0]) ? $_[0] : undef;
171         }
172 else {
173         foreach $d (split(/:/ , $ENV{PATH})) {
174                 if (-x "$d/$_[0]") { $rv = "$d/$_[0]"; last; }
175                 }
176         }
177 return $rv;
178 }
179
180