#!/bin/sh

po_domain="alterator-hpc"
sysconfdir=/etc
datadir=/usr/share/alterator-hpc
conffile=syslog-ng.conf

hostname_re='^[a-zA-Z_][.a-zA-Z0-9_-]*$'

. alterator-sh-functions

on_message()
{
	case "$in_action" in
		list)
		    echo '('
		    printf '("%s" label "%s")' "gather" "`_ "gather all logs from hosts"`"
		    printf '("%s" label "%s")' "scatter" "`_ "send all logs to remote host"`"
		    echo ')'
		    ;;
		write)
		    local sysconffile="$sysconfdir/$conffile"
		    case "$in_mode" in
		        scatter)
		    	    if echo "$in_server" |grep -qs "$hostname_re";then
			        cp -f "$datadir/$conffile.$in_mode" "$sysconffile"
			        sed -i "s^@HOSTNAME@^$in_server^g" "$sysconffile"
			        write_nop
			    else
			        write_error "`_ "invalid log server"`"
			    fi
			    ;;
			gather)
			    cp -f "$datadir/$conffile.$in_mode" "$sysconffile"
			    write_nop
			    ;;
			*)
			    write_error "invalid_mode"
			    ;;
		    esac
		    ;;
		*)
		    echo '#f'
		    ;;
	esac
}

message_loop
