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