
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>


#define PWRSTAT "/etc/powerstatus"

void powerfail(int);

main(int argc, char* argv[]) {
    char s[1000];
    int i=0;
    while(i<7) {
        scanf("%s",s);
        i++;
    }
    scanf("%s",s);
    if (!strcmp("b",argv[1]))
        if ((!strcmp(s,"33.1.6.3.3"))||(!strcmp(s,"upsMIB.upsObjects.upsAlarm.upsWellKnownAlarms.upsAlarmLowBattery")))
            powerfail(1);
    if (!strcmp("p",argv[1]))
        if ((!strcmp(s,"33.1.6.3.3"))||(!strcmp(s,"upsMIB.upsObjects.upsAlarm.upsWellKnownAlarms.upsAlarmLowBattery")))
            powerfail(0);
}
/* As the program may be activated in the event of other alarms as well, the inner 'if' are necessary */
void powerfail(int event) {
    int fd;
    unlink(PWRSTAT);
    if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
          switch (event)
           {
           case 0:
                  write(fd, "OKWAIT\n", 7);
                   break;

             case 1:
                    write(fd, "FAIL\n", 5);
                     break;
             }
          close(fd);
       }
       kill(1, SIGPWR);
}