#!/bin/bash
#
# Author: Koblinger Egmont <egmont@fazekas.hu>
#
# /sbin/init.d/power
#

users="root egmont"

#
# This script is invoked by /sbin/dumbupsd.
#
# First argument is one of cable, init, linechange, battchange, status.
#
# If first argument is "cable" then the environment variable CONNECTION
# contains 0 if connection to ups lost.
#          1 if connection is still wrong (called by dumbupsd every 60 secs).
#          2 if connection okay.
#
# If first argument is "init", "linechange", "battchange" or "status",
# then environment variables LINEFAIL and BATTLOW contain 0 or 1,
# 0 meaning false, so that line is okay, 1 meaning there is problem
# with the power or the battery.
#
# First argument is init:       called when dumbupsd is started.
#                   linechange: called when status of LINEFAIL line changes.
#                   battchange: called when status of BATTLOW line changes.
#                   status:     dumbupsd reports the status of lines when it
#                               receives SIGUSR1.
#
# Note: dumbupsd never sees that the two lines have both changed at the same
# time. If it happens, it calles linechange and battchange in some order.
#
# Another note: dumbupsd uses something similar to the system(3) call,
# so waits for the script to terminate.
#
# Yet another note: stdout and stderr are connected to /dev/console.
#

run="`/sbin/runlevel`"
PREVLEVEL="${run% *}"
RUNLEVEL="${run#* }"
export PREVLEVEL RUNLEVEL

usage="Incorrect usage."

if [ "_$1" = "_cable" ]; then
    case "_$CONNECTION" in
        _0) msg="UPS connection lost";;
        _1) msg="UPS still not connected";;
        _2) msg="UPS connection okay";;
        *)  echo "$usage" >&2; exit 1;;
    esac
    for i in $users; do
        echo "$msg" | write "$i" 2>/dev/null &
    done
    exit 0
fi

if [ "_$LINEFAIL" != "_0" -a "_$LINEFAIL" != "_1" ]; then
    echo "$usage" >&2; exit 1
fi
if [ "_$BATTLOW"  != "_0" -a "_$BATTLOW"  != "_1" ]; then
    echo "$usage" >&2; exit 1
fi

if [ "_$1" = "_init" ]; then
    echo -e "Initial state: linefail $LINEFAIL battlow $BATTLOW\\r"
    exit 0
fi

if [ "_$1" = "_linechange" ]; then
    case $((2*BATTLOW+LINEFAIL)) in
        0)  echo -e "Power restored\\r"
            /sbin/init.d/powerfail stop
            exit 0;;
        1)  echo -e "Power failure\\r"
            /sbin/init.d/powerfail start
            exit 0;;
        2)  echo -e "Power restored, battery still low\\r"
            exit 0;;
        3)  echo -e "Power failure\\r"
            exit 0;;
    esac
    exit 1
fi

if [ "_$1" = "_battchange" ]; then
    case $((2*BATTLOW+LINEFAIL)) in
        0)  echo -e "Battery okay\\r"
            /sbin/init.d/powerfail stop
            exit 0;;
        1)  echo -e "Battery became okay, though no power -- strange, ain't it?\\r"
            exit 0;;
        2)  echo -e "Battery low (though power is okay)\\r"
            /sbin/init.d/powerfail now
            exit 0;;
        3)  echo -e "Battery low\\r"
            /sbin/init.d/powerfail now
            exit 0;;
    esac
    exit 1
fi

if [ "_$1" = "_status" ]; then
    echo -e "State: connnection $CONNECTION linefail $LINEFAIL battlow $BATTLOW\\r"
    rm -f /etc/dumbupsd.pid
    echo $DUMBUPSD_PID >/etc/dumbupsd.pid
    exit 0
fi

echo "$usage" >&2
exit 1

