#!/bin/sh
##
##  serf.sh -- SERF event handler script
##  (see also http://www.serfdom.io/docs/agent/event-handlers.html)
##

log () {
    echo "[`date '+%Y-%m-%dT%H:%M:%S'`] $SERF_SELF_NAME ($SERF_TAG_ROLE): $*" \
        >>@l_prefix@/var/serfdom/log/self.events.log
}

case "$SERF_EVENT" in
    member-join | member-leave | member-failed | member-update | member-reap )
        while read member; do
            OIFS="$IFS"; IFS="	"
            set -- $member
            IFS="$OIFS"
            log "event=$SERF_EVENT host=$1 addr=$2 role=$3 tags=$4"
        done
        ;;
    user )
        log "event=$SERF_EVENT param=$SERF_USER_EVENT"
        case "$SERF_USER_EVENT" in
            ping )
                ;;
        esac
        ;;
    query )
        log "event=$SERF_EVENT param=$SERF_QUERY_NAME"
        case "$SERF_QUERY_NAME" in
            hello )
                echo "Hello from $SERF_SELF_NAME"
                ;;
        esac
        ;;
esac

exit 0

