UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / platforms / mingw / apccontrol.bat
blob23b8a6aa2a25b5f65db43a1932fb8d1bb73d626a
1 @echo off\r
2 setlocal\r
3 \r
4 rem\r
5 rem  This is the Windows apccontrol file.\r
6 rem\r
7 \r
8 rem Assign parameters to named variables\r
9 SET command=%1\r
10 SET sbindir=%5\r
12 rem Strip leading and trailing quotation marks from paths.\r
13 rem This is easily accomplished on NT, but Win95/98/ME\r
14 rem require an evil little trick with 'FOR'.\r
15 SET sbindir=%sbindir:"=%\r
16 IF "%sbindir%" == "" FOR %%A IN (%5) DO SET sbindir=%%A\r
18 rem Paths to important executables\r
19 SET APCUPSD="%sbindir%\apcupsd"\r
20 SET SHUTDOWN="%sbindir%\shutdown"\r
21 SET BACKGROUND="%sbindir%\background"\r
23 rem Only do popups on Win95/98/ME/NT. All other platforms support \r
24 rem balloon notifications which are provided by apctray.\r
25 SET POPUP=echo\r
26 VER | FIND /I "Windows 95" > NUL\r
27 IF NOT ERRORLEVEL 1 SET POPUP=%BACKGROUND% "%sbindir%\popup"\r
28 VER | FIND /I "Windows 98" > NUL\r
29 IF NOT ERRORLEVEL 1 SET POPUP=%BACKGROUND% "%sbindir%\popup"\r
30 VER | FIND /I "Windows ME" > NUL\r
31 IF NOT ERRORLEVEL 1 SET POPUP=%BACKGROUND% "%sbindir%\popup"\r
32 VER | FIND /I "Windows NT" > NUL\r
33 IF NOT ERRORLEVEL 1 SET POPUP=%BACKGROUND% "%sbindir%\popup"\r
35 rem\r
36 rem This piece is to substitute the default behaviour with your own script,\r
37 rem   perl, C program, etc.\r
38 rem\r
39 rem You can customize any command by creating an executable file (may be a\r
40 rem   script or a compiled program) and naming it the same as the %1 parameter\r
41 rem   passed by apcupsd to this script. We will accept files with any extension\r
42 rem   included in PATHEXT (*.exe, *.bat, *.cmd, etc).\r
43 rem\r
44 rem After executing your script, apccontrol continues with the default action.\r
45 rem   If you do not want apccontrol to continue, exit your script with exit \r
46 rem   code 99. E.g. "exit /b 99".\r
47 rem\r
48 rem WARNING: please be aware that if you add any commands before the shutdown\r
49 rem   in the downshutdown) case and your command errors or stalls, it will\r
50 rem   prevent your machine from being shutdown, so test, test, test to\r
51 rem   make sure it works correctly.\r
52 rem\r
53 rem The apccontrol.bat file will be replaced every time apcupsd is installed,\r
54 rem   so do NOT make event modifications in this file. Instead, override the\r
55 rem   event actions using event scripts as described above.\r
56 rem\r
58 rem Use CALL here because event script might be a batch file itself\r
59 CALL ".\%command%" 2> NUL\r
61 rem This is retarded. "IF ERRORLEVEL 99" means greater-than-or-\r
62 rem equal-to 99, so we have to synthesize an == using two IFs. \r
63 rem Ahh, the glory of Windows batch programming. At least they \r
64 rem gave us a NOT op.\r
65 IF NOT ERRORLEVEL 99 GOTO :events\r
66 IF NOT ERRORLEVEL 100 GOTO :done\r
68 :events\r
70 rem\r
71 rem powerout, onbattery, offbattery, mainsback events occur\r
72 rem   in that order.\r
73 rem\r
75 IF "%command%" == "commfailure"   GOTO :commfailure\r
76 IF "%command%" == "commok"        GOTO :commok\r
77 IF "%command%" == "powerout"      GOTO :powerout\r
78 IF "%command%" == "onbattery"     GOTO :onbattery\r
79 IF "%command%" == "offbattery"    GOTO :offbattery\r
80 IF "%command%" == "mainsback"     GOTO :mainsback\r
81 IF "%command%" == "failing"       GOTO :failing\r
82 IF "%command%" == "timeout"       GOTO :timeout\r
83 IF "%command%" == "loadlimit"     GOTO :loadlimit\r
84 IF "%command%" == "runlimit"      GOTO :runlimit\r
85 IF "%command%" == "doshutdown"    GOTO :doshutdown\r
86 IF "%command%" == "annoyme"       GOTO :annoyme\r
87 IF "%command%" == "emergency"     GOTO :emergency\r
88 IF "%command%" == "changeme"      GOTO :changeme\r
89 IF "%command%" == "remotedown"    GOTO :remotedown\r
90 IF "%command%" == "startselftest" GOTO :startselftest\r
91 IF "%command%" == "endselftest"   GOTO :endselftest\r
92 IF "%command%" == "battdetach"    GOTO :battdetach\r
93 IF "%command%" == "battattach"    GOTO :battattach\r
95 echo Unknown command '%command%'\r
96 echo.\r
97 echo Usage: %0 command\r
98 echo.\r
99 echo Warning: this script is intended to be launched by\r
100 echo apcupsd and should never be launched by users.\r
101 GOTO :done\r
103 :commfailure\r
104    %POPUP% "Communications with UPS lost."\r
105    GOTO :done\r
107 :commok\r
108    %POPUP% "Communciations with UPS restored."\r
109    GOTO :done\r
111 :powerout\r
112    GOTO :done\r
114 :onbattery\r
115    %POPUP% "Power failure. Running on UPS batteries."\r
116    GOTO :done\r
118 :offbattery\r
119    %POPUP% "Power has returned. No longer running on UPS batteries."\r
120    GOTO :done\r
122 :mainsback\r
123    GOTO :done\r
125 :failing\r
126    %POPUP% "UPS battery power exhaused. Doing shutdown."\r
127    GOTO :done\r
129 :timeout\r
130    %POPUP% "UPS battery runtime limit exceeded. Doing shutdown."\r
131    GOTO :done\r
133 :loadlimit\r
134    %POPUP% "UPS battery discharge limit reached. Doing shutdown."\r
135    GOTO :done\r
137 :runlimit\r
138    %POPUP% "UPS battery runtime percent reached. Doing shutdown."\r
139    GOTO :done\r
141 :doshutdown\r
142 rem\r
143 rem  If you want to try to power down your UPS, uncomment\r
144 rem    out the following lines, but be warned that if the\r
145 rem    following shutdown -h now doesn't work, you may find\r
146 rem    the power being shut off to a running computer :-(\r
147 rem  Also note, we do this in the doshutdown case, because\r
148 rem    there is no way to get control when the machine is\r
149 rem    shutdown to call this script with --killpower. As\r
150 rem    a consequence, we do both killpower and shutdown\r
151 rem    here.\r
152 rem  Note that Win32 lacks a portable way to delay for a\r
153 rem    given time, so we use the trick of pinging a\r
154 rem    non-existent IP address with a given timeout.\r
155 rem\r
156 rem   %APCUPSD% /kill\r
157 rem   ping -n 1 -w 5000 10.255.255.254 > NUL\r
158 rem   %POPUP% "Doing %APCUPSD% --killpower"\r
159 rem   %APCUPSD% --killpower\r
160 rem   ping -n 1 -w 12000 10.255.255.254 > NUL\r
161 rem\r
162    %SHUTDOWN% -h now\r
163    GOTO :done\r
165 :annoyme\r
166    %POPUP% "Power problems: please logoff."\r
167    GOTO :done\r
169 :emergency\r
170    %POPUP% "Emergency shutdown initiated."\r
171    GOTO :done\r
173 :changeme\r
174    %POPUP% "Emergency! UPS batteries have failed: Change them NOW"\r
175    GOTO :done\r
177 :remotedown\r
178    %POPUP% "Shutdown due to master state or comms lost."\r
179    GOTO :done\r
181 :startselftest\r
182    %POPUP% "Self-test starting"\r
183    GOTO :done\r
185 :endselftest\r
186    %POPUP% "Self-test completed"\r
187    GOTO :done\r
189 :battdetach\r
190    %POPUP% "Battery disconnected"\r
191    GOTO :done\r
193 :battattach\r
194    %POPUP% "Battery reattached"\r
195    GOTO :done\r
197 :done\r
198 rem That's all, folks\r