#!/bin/bash
# Set host for forwarding syslog messages
# Call: switchsyslog [ip-address[:port]]
# Calling without parameter resets according to entry in config.lua

reset=0

OS=$(omgsysteminfo --filter="os.name" --val)

if [ -z "$1" ] ; then
    reset=1
    if test -f /data/config.lua ; then
        if grep -q string_SyslogDestination /data/config.lua; then
            SYSLOG_HOST=$(grep string_SyslogDestination /data/config.lua | sed -e "s/.*\"\(.*\)\".*/\\1/")
            SYSLOG_PORT=$(grep string_SyslogPort /data/config.lua | sed -e "s/.*\"\(.*\)\".*/\\1/")
            SYSLOG_PROTOCOL=$(grep string_SyslogProtocol /data/config.lua | sed -e "s/.*\"\(.*\)\".*/\\1/")
        else
            SYSLOG_HOST="local"
        fi
    fi
    if [ -z "${SYSLOG_HOST}" ] ; then
	echo "OMG: SyslogDestination missing, setting to 0.0.0.0"
	SYSLOG_HOST="0.0.0.0"
    fi
    
    if [ -z "${SYSLOG_PORT}" ] ; then
	echo "OMG: SyslogDestination port missing, setting to 514"
        SYSLOG_PORT="514"
    fi
    if [ -z "${SYSLOG_PROTOCOL}" ] ; then
	echo "OMG: SyslogProtocol missing, setting to udp"
        SYSLOG_PORT="udp"
    fi
    if grep -q "^export SYSLOG_HOST=" /tmp/cmdline;then
	sed -i -e "/^export SYSLOG_HOST=/cexport SYSLOG_HOST=\"${SYSLOG_HOST}\"" /tmp/cmdline
    else
	echo "export SYSLOG_HOST=\"${SYSLOG_HOST}\"" >> /tmp/cmdline
    fi
    if grep -q "^export SYSLOG_PORT=" /tmp/cmdline;then
	sed -i -e "/^export SYSLOG_PORT=/cexport SYSLOG_PORT=\"${SYSLOG_PORT}\"" /tmp/cmdline
    else
	echo "export SYSLOG_PORT=\"${SYSLOG_PORT}\"" >> /tmp/cmdline
    fi
    if grep -q "^export SYSLOG_PROTOCOL=" /tmp/cmdline;then
	sed -i -e "/^export SYSLOG_PROTOCOL=/cexport SYSLOG_PROTOCOL=\"${SYSLOG_PROTOCOL}\"" /tmp/cmdline
    else
	echo "export SYSLOG_PROTOCOL=\"${SYSLOG_PROTOCOL}\"" >> /tmp/cmdline
    fi
    
fi

# Running rsyslogd
if [ "${reset}" = "1" ] ; then
    echo '$template ferrariFormatKernel,"<%PRI%>%timereported:1:10:date-rfc3339% %timereported:12:22:date-rfc3339% %HOSTNAME% %msg%\n"' > /etc/rsyslog.d/50-default.conf
    echo '$template ferrariFormat,"<%PRI%>%timereported:1:10:date-rfc3339% %timereported:12:22:date-rfc3339% %HOSTNAME%%msg%\n"'>> /etc/rsyslog.d/50-default.conf

    if test "${SYSLOG_HOST}" = "0.0.0.0"; then
        echo "*.*   /dev/null" >> /etc/rsyslog.d/50-default.conf  
    elif test "${SYSLOG_HOST}" = "local"; then
        if [ "${OS}" = "ubuntu" ]; then
            echo "kern.*   /var/log/syslog;ferrariFormatKernel" >> /etc/rsyslog.d/50-default.conf
            echo "local5.*;auth.*;authpriv.*;cron.*;daemon.*;lpr.*;mail.*;mark.*;news.*;security.*;syslog.*;user.*;uucp.*;local0.*;local1.*;local2.*;local3.*;local4.*;local6.*;local7.*  /var/log/syslog;ferrariFormat" >> /etc/rsyslog.d/50-default.conf
        else
            echo "kern.*   /var/log/messages;ferrariFormatKernel" >> /etc/rsyslog.d/50-default.conf
            echo "local5.*;auth.*;authpriv.*;cron.*;daemon.*;lpr.*;mail.*;mark.*;news.*;security.*;syslog.*;user.*;uucp.*;local0.*;local1.*;local2.*;local3.*;local4.*;local6.*;local7.*  /var/log/messages;ferrariFormat" >> /etc/rsyslog.d/50-default.conf
        fi
    else
        if [ "${SYSLOG_PROTOCOL}" = "tcp" ]; then
            echo "kern.*   @@${SYSLOG_HOST}:${SYSLOG_PORT};ferrariFormatKernel" >>  /etc/rsyslog.d/50-default.conf
            echo "local5.*;auth.*;authpriv.*;cron.*;daemon.*;lpr.*;mail.*;mark.*;news.*;security.*;syslog.*;user.*;uucp.*;local0.*;local1.*;local2.*;local3.*;local4.*;local6.*;local7.*   @@${SYSLOG_HOST}:${SYSLOG_PORT};ferrariFormat" >>  /etc/rsyslog.d/50-default.conf
        else
            echo "kern.*   @${SYSLOG_HOST}:${SYSLOG_PORT};ferrariFormatKernel" >>  /etc/rsyslog.d/50-default.conf
            echo "local5.*;auth.*;authpriv.*;cron.*;daemon.*;lpr.*;mail.*;mark.*;news.*;security.*;syslog.*;user.*;uucp.*;local0.*;local1.*;local2.*;local3.*;local4.*;local6.*;local7.*  @${SYSLOG_HOST}:${SYSLOG_PORT};ferrariFormat" >>  /etc/rsyslog.d/50-default.conf
        fi
    fi

else
    echo "*.* @${1}" > /etc/rsyslog.d/50-default.conf
fi

if [ "${OS}" = "ubuntu" ]; then
    initctl stop rsyslog 2>/dev/null
    initctl start rsyslog 2>/dev/null
else
    service rsyslog restart
fi

# Start netconsole
if lsmod | grep -q netconsole; then
    rmmod netconsole
fi

if [ "${SYSLOG_HOST}" = "0.0.0.0"  -o "${SYSLOG_HOST}" = "local" ]; then
    exit 0
fi

# Increase console loglevel
dmesg -n 3
modprobe netconsole netconsole=@/,${SYSLOG_PORT}@${SYSLOG_HOST}/ 2>/dev/null # Ignore errors

exit 0
