#!/bin/bash

. ${TOOLKIT}-functions.sh

[ -x /usr/sbin/kubackup-update-rsync -a -e /etc/kubackup-run.conf ] && {
	kubackup-update-rsync
}

file=/etc/kusa/modules/local/rsyncd.secrets
orig=/etc/rsyncd.secrets
instfile="/etc/kusa/modules/local/install"

[ -f $file ] || {
	installfile rsyncd.secrets $file root:root 660

	if [ -f $orig ]
	then
		putwarning "/etc/rsyncd.secrets" \
			"This file was moved to $file" \
			"and is now under kusa control"
		cat $orig >>$file
		mv $orig $file.orig
	else
		putwarning "/etc/rsyncd.secrets" \
			"A new, empty file $file was created" 
	fi
	grep -q "rsyncd.secrets" $instfile || {
		echo		>>$instfile
		echo "# rsync"	>>$instfile
		echo "rsyncd.secrets	/etc/ root:root 440" >>$instfile
	}
}


src="rsyncd.conf.tmp"
target="/etc/rsyncd.conf"
stat="NOFILE"

[ -f $target ] && {
	stat="NOINCLUDE"
	grep -q "^&include\s\s*/etc/rsyncd.d" $target && stat="INCLUDE"
}

case $DISTRIB_ID in
  OMV*)
  	case $stat in
	  NOFILE|NOINCLUDE)
		echo
	  	echo "RSYNC on OpenMediaVault: you need to activate the service"
		echo "and add '&include /etc/rsyncd.d' in custom parameters"
		echo
		exit 1
		;;
	esac
	;;
esac
case $stat in
  INCLUDE)	target="/etc/rsyncd.d/kusa-auto.conf" ;;
  *)		target="/etc/rsyncd.conf" ;;
esac

installfile $src $target root:root 440




$SOMETHING_CHANGED && {
	which xinetd >/dev/null && {
		restart_service xinetd xinetd || exit 1
	}
}

exit 0
