Handle hostnames with upper-case letters
[webmin.git] / init / atboot.pl
1 #!/usr/local/bin/perl
2 # atboot.pl
3 # Called by setup.sh to have webmin started at boot time
4
5 $no_acl_check++;
6 require './init-lib.pl';
7 $product = $ARGV[0] || "webmin";
8 $ucproduct = ucfirst($product);
9
10 if ($init_mode eq "osx") {
11         # Darwin System
12         &enable_at_boot("webmin", "Webmin administration server",
13                         "$config_directory/start >/dev/null 2>&1 </dev/null",
14                         "$config_directory/stop");
15         }
16 elsif ($init_mode eq "local") {
17         # Add to the boot time rc script
18         $lref = &read_file_lines($config{'local_script'});
19         for($i=0; $i<@$lref && $lref->[$i] !~ /^exit\s/; $i++) { }
20         splice(@$lref, $i, 0, "$config_directory/start >/dev/null 2>&1 </dev/null # Start $ucproduct");
21         &flush_file_lines();
22         }
23 elsif ($init_mode eq "init") {
24         # Create a bootup action, if missing
25         @start = &get_start_runlevels();
26         $fn = &action_filename($product);
27         if (!-r $fn) {
28                 &open_tempfile(ACTION,">$fn");
29                 $desc = "Start/stop $ucproduct";
30                 &print_tempfile(ACTION, "#!/bin/sh\n");
31                 $start_order = "9" x $config{'order_digits'};
32                 $stop_order = "9" x $config{'order_digits'};
33                 if ($config{'chkconfig'}) {
34                         # Redhat-style description: and chkconfig: lines
35                         &print_tempfile(ACTION, "# description: $desc\n");
36                         &print_tempfile(ACTION,
37                                      "# chkconfig: $config{'chkconfig'} ",
38                                      "$start_order $stop_order\n");
39                         }
40                 elsif ($config{'init_info'}) {
41                         # Suse-style init info section
42                         &print_tempfile(ACTION, "### BEGIN INIT INFO\n",
43                                      "# Provides: $product\n",
44                                      "# Required-Start: \$network \$syslog\n",
45                                      "# Required-Stop: \$network\n",
46                                      "# Default-Start: ",join(" ", @start),"\n",
47                                      "# Default-Stop:\n",
48                                      "# Description: $desc\n",
49                                      "### END INIT INFO\n");
50                         }
51                 else {
52                         # Just description in a comment
53                         &print_tempfile(ACTION, "# $desc\n");
54                         }
55                 &print_tempfile(ACTION, "\n");
56                 &print_tempfile(ACTION, "case \"\$1\" in\n");
57
58                 &print_tempfile(ACTION, "'start')\n");
59                 &print_tempfile(ACTION, "\t$config_directory/start >/dev/null 2>&1 </dev/null\n");
60                 &print_tempfile(ACTION, "\tRETVAL=\$?\n");
61                 if ($config{'subsys'}) {
62                         &print_tempfile(ACTION, "\tif [ \"\$RETVAL\" = \"0\" ]; then\n");
63                         &print_tempfile(ACTION, "\t\ttouch $config{'subsys'}/$product\n");
64                         &print_tempfile(ACTION, "\tfi\n");
65                         }
66                 &print_tempfile(ACTION, "\t;;\n");
67
68                 &print_tempfile(ACTION, "'stop')\n");
69                 &print_tempfile(ACTION, "\t$config_directory/stop\n");
70                 &print_tempfile(ACTION, "\tRETVAL=\$?\n");
71                 if ($config{'subsys'}) {
72                         &print_tempfile(ACTION, "\tif [ \"\$RETVAL\" = \"0\" ]; then\n");
73                         &print_tempfile(ACTION, "\t\trm -f $config{'subsys'}/$product\n");
74                         &print_tempfile(ACTION, "\tfi\n");
75                         }
76                 &print_tempfile(ACTION, "\t;;\n");
77
78                 &print_tempfile(ACTION, "'status')\n");
79                 &print_tempfile(ACTION, "\tpidfile=`grep \"^pidfile=\" $config_directory/miniserv.conf | sed -e 's/pidfile=//g'`\n");
80                 &print_tempfile(ACTION, "\tif [ -s \$pidfile ]; then\n");
81                 &print_tempfile(ACTION, "\t\tpid=`cat \$pidfile`\n");
82                 &print_tempfile(ACTION, "\t\tkill -0 \$pid >/dev/null 2>&1\n");
83                 &print_tempfile(ACTION, "\t\tif [ \"\$?\" = \"0\" ]; then\n");
84                 &print_tempfile(ACTION, "\t\t\techo \"$product (pid \$pid) is running\"\n");
85                 &print_tempfile(ACTION, "\t\t\tRETVAL=0\n");
86                 &print_tempfile(ACTION, "\t\telse\n");
87                 &print_tempfile(ACTION, "\t\t\techo \"$product is stopped\"\n");
88                 &print_tempfile(ACTION, "\t\t\tRETVAL=1\n");
89                 &print_tempfile(ACTION, "\t\tfi\n");
90                 &print_tempfile(ACTION, "\telse\n");
91                 &print_tempfile(ACTION, "\t\techo \"$product is stopped\"\n");
92                 &print_tempfile(ACTION, "\t\tRETVAL=1\n");
93                 &print_tempfile(ACTION, "\tfi\n");
94                 &print_tempfile(ACTION, "\t;;\n");
95
96                 &print_tempfile(ACTION, "'restart')\n");
97                 &print_tempfile(ACTION, "\t$config_directory/stop ; $config_directory/start\n");
98                 &print_tempfile(ACTION, "\tRETVAL=\$?\n");
99                 &print_tempfile(ACTION, "\t;;\n");
100
101                 &print_tempfile(ACTION, "*)\n");
102                 &print_tempfile(ACTION, "\techo \"Usage: \$0 { start | stop }\"\n");
103                 &print_tempfile(ACTION, "\tRETVAL=1\n");
104                 &print_tempfile(ACTION, "\t;;\n");
105                 &print_tempfile(ACTION, "esac\n");
106                 &print_tempfile(ACTION, "exit \$RETVAL\n");
107                 &close_tempfile(ACTION);
108                 chmod(0755, $fn);
109                 }
110
111         # Add whatever links are needed to start at boot
112         &enable_at_boot($product);
113         print STDERR "Created init script $fn\n";
114         }
115 elsif ($init_mode eq "win32") {
116         # Create win32 service
117         $perl_path = &get_perl_path();
118         &enable_at_boot($product, $ucproduct, $perl_path." ".&quote_path("$root_directory/miniserv.pl")." ".&quote_path("$config_directory/miniserv.conf"));
119         }
120 elsif ($init_mode eq "rc" || $init_mode eq "upstart") {
121         # Create RC or upstart script
122         &enable_at_boot($product, $ucproduct, "$config_directory/start",
123                                               "$config_directory/stop",
124                                               undef,
125                                               { 'fork' => 1 });
126         }
127
128 $config{'atboot_product'} = $product;
129 &save_module_config();
130