1*1e76060aSYuxiao Zhang #include "file-io.hpp"
2*1e76060aSYuxiao Zhang 
3*1e76060aSYuxiao Zhang #include <stdplus/print.hpp>
4*1e76060aSYuxiao Zhang 
5*1e76060aSYuxiao Zhang #include <cstring>
6*1e76060aSYuxiao Zhang #include <iostream>
7*1e76060aSYuxiao Zhang 
printUsage()8*1e76060aSYuxiao Zhang static void printUsage()
9*1e76060aSYuxiao Zhang {
10*1e76060aSYuxiao Zhang     stdplus::println(stderr, "Usage: update_dhcp_status <state> <message>");
11*1e76060aSYuxiao Zhang     stdplus::println(stderr,
12*1e76060aSYuxiao Zhang                      "<state> is one of 'DONE', 'POWERCYCLE' or 'ONGOING'");
13*1e76060aSYuxiao Zhang }
14*1e76060aSYuxiao Zhang 
genStatusCode(char * state)15*1e76060aSYuxiao Zhang static int genStatusCode(char* state)
16*1e76060aSYuxiao Zhang {
17*1e76060aSYuxiao Zhang     if (std::strcmp(state, "DONE") == 0)
18*1e76060aSYuxiao Zhang     {
19*1e76060aSYuxiao Zhang         return 0;
20*1e76060aSYuxiao Zhang     }
21*1e76060aSYuxiao Zhang     else if (std::strcmp(state, "POWERCYCLE") == 0)
22*1e76060aSYuxiao Zhang     {
23*1e76060aSYuxiao Zhang         return 1;
24*1e76060aSYuxiao Zhang     }
25*1e76060aSYuxiao Zhang     else if (std::strcmp(state, "ONGOING") == 0)
26*1e76060aSYuxiao Zhang     {
27*1e76060aSYuxiao Zhang         return 2;
28*1e76060aSYuxiao Zhang     }
29*1e76060aSYuxiao Zhang 
30*1e76060aSYuxiao Zhang     return -1;
31*1e76060aSYuxiao Zhang }
32*1e76060aSYuxiao Zhang 
main(int argc,char * argv[])33*1e76060aSYuxiao Zhang int main(int argc, char* argv[])
34*1e76060aSYuxiao Zhang {
35*1e76060aSYuxiao Zhang     if (argc != 3)
36*1e76060aSYuxiao Zhang     {
37*1e76060aSYuxiao Zhang         printUsage();
38*1e76060aSYuxiao Zhang         return 1;
39*1e76060aSYuxiao Zhang     }
40*1e76060aSYuxiao Zhang 
41*1e76060aSYuxiao Zhang     int statusCode = genStatusCode(argv[1]);
42*1e76060aSYuxiao Zhang 
43*1e76060aSYuxiao Zhang     if (statusCode == -1)
44*1e76060aSYuxiao Zhang     {
45*1e76060aSYuxiao Zhang         printUsage();
46*1e76060aSYuxiao Zhang         return 1;
47*1e76060aSYuxiao Zhang     }
48*1e76060aSYuxiao Zhang 
49*1e76060aSYuxiao Zhang     try
50*1e76060aSYuxiao Zhang     {
51*1e76060aSYuxiao Zhang         std::string status;
52*1e76060aSYuxiao Zhang         status.push_back(statusCode);
53*1e76060aSYuxiao Zhang         status.append(argv[2]);
54*1e76060aSYuxiao Zhang         fileWrite(statusFile, status);
55*1e76060aSYuxiao Zhang     }
56*1e76060aSYuxiao Zhang     catch (const std::exception& e)
57*1e76060aSYuxiao Zhang     {
58*1e76060aSYuxiao Zhang         stdplus::println(stderr, "Failed to update status file {}", e.what());
59*1e76060aSYuxiao Zhang         return 1;
60*1e76060aSYuxiao Zhang     }
61*1e76060aSYuxiao Zhang 
62*1e76060aSYuxiao Zhang     return 0;
63*1e76060aSYuxiao Zhang }
64