#!/bin/bash

. ${TOOLKIT}-functions.sh

syslog_server=$(jtconf service.syslog)
syslog_type=$(jtconf syslog.type)
rdir="/etc/rsyslog.d"
perms="root:root 440"
oldver_tag=

major_version=$(dpkg -l rsyslog | grep '^ii ' | sed -e 's/.*rsyslog *//' -e 's/\..*//')
echo "  rsyslog major_version: $major_version"
echo "  syslog type:           $syslog_type"
echo "  syslog server:         $syslog_server"

[ $major_version -lt 8 ] && oldver_tag="-v7"



installconf()
{
	local target=$(basename "$1")
	
	echo "$1" | fgrep -q ".conf-v" && {
		# fix: removes dangling files with trailing version
		uninstallfiles $rdir/$target
		target=$(basename "$1" | sed -e 's/\.conf-.*/.conf/')
	}
	installfile $1 $rdir/$target $perms
}

removeconf()
{
	local file
	for file
	do
		uninstallfiles $rdir/$file
		purgefiles $rdir/$file
	done
}



case $syslog_type in
  remote_only)
  	[ "X$syslog_server" = "X" ] && {
  		echo "  ERR: 'remote_only' mode, syslog server undefined (service.syslog)" >&2
		exit 1
	}
	installconf	50-no-varlog.conf
	removeconf	50-varlog.conf 50-varlog-v7.conf 50-compact.conf 50-compact-v7.conf
	outfiles="cron.log auth.log debug"
	;;
   compact)
   	installconf	50-compact.conf${oldver_tag}
	removeconf	50-no-varlog.conf 50-varlog.conf 50-varlog.conf-v7.conf
	outfiles="cron.log auth.log daemon.log mail.log debug messages"
	;;
   *)
	installconf	50-varlog.conf${oldver_tag}
	removeconf	50-no-varlog.conf 50-compact.conf 50-compact-v7.conf
	outfiles="cron.log auth.log daemon.log mail.log debug messages
		syslog kern.log lpr.log user.log
		mail.info mail.warn mail.err
		news/news.crit news/news.err news/news.notice
	"
	;;
esac

is_server=false
if [ "X$syslog_server" != "X" ]
then
	myself=$(uname -n)
	myself_short=$(uname -n | cut -d'.' -f1)
	myaddr=$(jtconf network.network).$(jtconf network.address)

	if [ "$syslog_server" = $myself \
		-o "$syslog_server" = $myself_short \
		-o "$syslog_server" = $myaddr ]
	then
		echo "  NOTICE: this is the syslog server ($syslog_server), enabling remote collection"
		is_server=true
		removeconf	80-remote.conf
	else
		echo "  sending all log messages to remote server ($syslog_server)"
		is_server=false
		installconf	80-remote.conf
	fi
fi

enable_net=false
if $is_server
then
	enable_net=true
else
	getconfirm enable_net && {
		echo "  remote collection overridden by configuration (syslog.enable_net)"
		enable_net=true
	}
fi

$enable_net && {
	udp_port=$(jtconf syslog.udp_port 2>/dev/null) || :
	tcp_port=$(jtconf syslog.tcp_port 2>/dev/null) || :

	[ "X$udp_port" = "X" -a "X$tcp_port" = "X" ] && {
		echo "ERROR: network collection enabled, but not utp or tcp port defined" >&2
		exit 1
	}

	file=20-udp-collection.conf
	[ "X$udp_port" != "X" ] && installconf $file || removeconf $file

	file=21-tcp-collection.conf
	[ "X$tcp_port" != "X" ] && installconf $file || removeconf $file
}


file=15-mark-enabled.conf
getconfirm syslog.enable_mark	&& installconf $file || removeconf $file

file=60-xconsole.conf
getconfirm syslog.xconsole	&& installconf $file || removeconf $file

file=60-tty.conf
tty=$(jtconf syslog.tty 2>/dev/null) && {
	echo "  enabling message copy to /dev/$tty"
	installconf $file
} || {
	removeconf $file
}

file=/etc/cron.d/ku-watchdog
if getconfirm syslog.enable_ku_watchdog
then
	installfile ku-watchdog.cron	$file $perms
else
	uninstallfiles $file
	purgefiles $file
fi


# FIXES

# create required files if needed
#
user=$(jtconf syslog.file_owner)
group=$(jtconf syslog.file_group)
fmode=$(jtconf syslog.file_create_mode)

for file in $outfiles
do
	if [ -f /var/log/$file ]
	then
		__installfile_fixperms true /var/log/$file $user:$group $fmode
	else
		installfile /dev/null /var/log/$file $user:$group $fmode
	fi
done

# 2018.08.29
# fix, remove old or wrong files
#
files="
	/etc/cron.daily/logrotate.cron
	$rdir/50-default*.conf
"
uninstallfiles $files
purgefiles $files



$SOMETHING_CHANGED && {
	restart_service rsyslog rsyslogd
}

exit 0
