ChCh Öffnungsstatus

Aus Chaostreff Chemnitz

Dieses Script wird einmal in der Minute gestartet und prüft das Ring-Signal der seriellen Schnittstelle. Wenn unsere Außenbeleuchtung eingeschaltet wird, gibt es dieß im IRC bekannt. Ebenso gibt es das Ausschalten bekannt.

#! /bin/bash

# USER CONFIG #
IRC_USER="chch"
IRC_CHANNEL="#chaoschemnitz"
IRC_SERVER="irc.freenode.net"
ON_MSG="Wir haben geöffnet."
OFF_MSG="Wir schließen jetzt."
ON_TOPIC=" | Status: geöffnet"
OFF_TOPIC=" | Status: geschlossen"
###############

# SYSTEM CONFIG #
II_START_TIMEOUT=10
JOIN_TIMEOUT=10
IRC_PREFIX=/var/irc
PORT="port:000003F8"

CAT=/bin/cat
CUT=/usr/bin/cut
TAIL=/usr/bin/tail
GREP=/bin/grep
SED=/bin/sed
ECHO=/bin/echo
PS=/bin/ps
SLEEP=/bin/sleep
II=/usr/bin/ii
#################

IRC_CHAN_IN=$IRC_PREFIX/$IRC_SERVER/$IRC_CHANNEL/in
IRC_CHAN_OUT=$IRC_PREFIX/$IRC_SERVER/$IRC_CHANNEL/out
IRC_SRV_IN=$IRC_PREFIX/$IRC_SERVER/in
IRC_SRV_OUT=$IRC_PREFIX/$IRC_SERVER/out
II_RUNNING=$($PS awx | $GREP "ii" | $GREP "\-n $IRC_USER")
RING=$($CAT /proc/tty/driver/serial | $GREP "$PORT" | $GREP -o "RI")

# RESTART II
if [ -z "$II_RUNNING" ]; then
        $II -n $IRC_USER -i $IRC_PREFIX &
        while [ $II_START_TIMEOUT -ge 0 -a ! -a $IRC_SRV_IN ]; do
                II_START_TIMEOUT=$II_START_TIMEOUT-1
                $SLEEP 1
        done
        if [ $II_START_TIMEOUT -lt 0 -a ! -a $IRC_SRV_IN ]; then
                exit 1
        fi
        $ECHO -e "/j $IRC_CHANNEL\n" > $IRC_SRV_IN
        if [ $? -ne 0 ]; then
                exit 1
        fi
fi

if [ ! -a $IRC_CHAN_OUT ]; then
        TMP=$JOIN_TIMEOUT
        while [ $JOIN_TIMEOUT -ge 0 -a ! -a $IRC_CHAN_OUT ]; do
                JOIN_TIMEOUT=$JOIN_TIMEOUT-1
                $SLEEP 1
        done
        if [ $JOIN_TIMEOUT -lt 0 -a ! -a $IRC_CHAN_OUT ]; then
                $ECHO -e "/j $IRC_CHANNEL\n" > $IRC_SRV_IN
        fi
        JOIN_TIMEOUT=$TMP
        while [ $JOIN_TIMEOUT -ge 0 -a ! -a $IRC_CHAN_OUT ]; do
                JOIN_TIMEOUT=$JOIN_TIMEOUT-1
                $SLEEP 1
        done
        if [ $JOIN_TIMEOUT -lt 0 -a ! -a $IRC_CHAN_OUT ]; then
                exit 1
        fi
fi

# FIND OUR LAST STATUS MESSAGE
PREV_MSG=$($CAT $IRC_CHAN_OUT | $CUT -d":" -f2 | $GREP -e "^[0-9]\{2\} <$IRC_USER> $ON_MSG\$\|^[0-9]\{2\} <$IRC_USER> $OFF_MSG\$" | $TAIL -n1 | $GREP -oe "$ON_MSG\|$OFF_MSG")
CURRENT_TOPIC=$($ECHO "/TOPIC $IRC_CHANNEL" > $IRC_SRV_IN; $SLEEP 3; $CAT $IRC_SRV_OUT | $GREP -i -e "[0-9]\{4,\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\} $IRC_CHANNEL" | $GREP -v "End of /NAMES list." | $GREP -v "[[:alnum:]]*!~[[:alnum:]]*@" | $TAIL -n1 | $SED -e "s/[0-9]\{4,\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\} $IRC_CHANNEL //I")
CURRENT_STATUS=$($ECHO $CURRENT_TOPIC | $GREP -oe "$ON_TOPIC$\|$OFF_TOPIC$")
if [ ! -z "$CURRENT_STATUS" ]; then
        CURRENT_TOPIC=$($ECHO $CURRENT_TOPIC | $SED -e "s/$CURRENT_STATUS//")
fi

# CHOOSE CORRECT MESSAGE AND TOPIC
if [ -z "$RING" ]; then
        MSG="$OFF_MSG"
        TOPIC="$OFF_TOPIC"
else
        MSG="$ON_MSG"
        TOPIC="$ON_TOPIC"
fi

# SEND MESSAGE IF IT DIIFERS FROM PREVIOUS STATUS
if [ "$MSG" != "$PREV_MSG" ]; then
        $ECHO -e "$MSG\n" > $IRC_CHAN_IN
fi
# SET TOPIC IF IT DIFFERS FROM PREVIOUS ONE
if [ "$TOPIC" != "$CURRENT_STATUS" ]; then
        $ECHO -e "/t $CURRENT_TOPIC$TOPIC\n" > $IRC_CHAN_IN
fi
if [ $? -ne 0 ]; then
        exit 1
fi