#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2012 Volker Theile
#
# OpenMediaVault is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# OpenMediaVault is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

# Documentation/Howto:
# http://wiki.dreamhost.com/index.php/Crontab

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

OMV_RSYNC_CRONSCRIPT=${OMV_RSYNC_CRONSCRIPT:-"/etc/cron.d/openmediavault-rsync"}
OMV_RSYNC_CRONSCRIPTS_DIR=${OMV_RSYNC_CRONSCRIPTS_DIR:-"/var/lib/openmediavault/cron.d"}
OMV_RSYNC_CRONSCRIPTS_MASK=${OMV_RSYNC_CRONSCRIPTS_MASK:-"755"}
OMV_RSYNC_CRONUSER=${OMV_RSYNC_CRONUSER:-"root"}
OMV_RSYNC_LOGFILE=${OMV_RSYNC_LOGFILE:-"/var/log/rsync.log"}

# Create the rsync cron jobs. Each job is configured in a seperate script.
mkdir -p ${OMV_RSYNC_CRONSCRIPTS_DIR}
rm -f ${OMV_RSYNC_CRONSCRIPTS_DIR}/rsync-*

# Create the scripts
xmlstarlet sel -t -m "//services/rsync/jobs/job[enable='1']" \
  -v "uuid" \
  -i "position() != last()" -n -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc |
  while read uuid; do
	  type=$(omv_config_get "//services/rsync/jobs/job[uuid='${uuid}']/type")
	  filename="${OMV_RSYNC_CRONSCRIPTS_DIR}/rsync-${uuid}"
	  runfile="/var/run/rsync-${uuid}"
	  srcuri=""
	  desturi=""

	  case $type in
	  local)
		  srcsfref=$(omv_config_get "//services/rsync/jobs/job[uuid='${uuid}']/src")
		  srcuri="$(omv_get_sharedfolder_path "${srcsfref}")/"
		  destsfref=$(omv_config_get "//services/rsync/jobs/job[uuid='${uuid}']/dest")
		  desturi=$(omv_get_sharedfolder_path "${destsfref}")
		  ;;
	  remote)
		  mode=$(omv_config_get "//services/rsync/jobs/job[uuid='${uuid}']/mode")
		  case $mode in
		  push)
			  srcsfref=$(omv_config_get "//services/rsync/jobs/job[uuid='${uuid}']/src")
			  srcuri="$(omv_get_sharedfolder_path "${srcsfref}")/"
			  desturi=$(omv_config_get "//services/rsync/jobs/job[uuid='${uuid}']/dest")
			  ;;
		  pull)
			  srcuri=$(omv_config_get "//services/rsync/jobs/job[uuid='${uuid}']/src")
			  destsfref=$(omv_config_get "//services/rsync/jobs/job[uuid='${uuid}']/dest")
			  desturi=$(omv_get_sharedfolder_path "${destsfref}")
			  ;;
		  esac
		  ;;
	  esac

	  xmlstarlet sel -t -m "//services/rsync/jobs/job[uuid='${uuid}']" \
		-o "#!/bin/sh" -n \
		-o "# This configuration file is auto-generated." -n \
		-o "# WARNING: Do not edit this file, your changes will be lost." -n \
		-o "[ -e ${runfile} ] &amp;&amp; exit 0" -n \
		-o "trap &quot;rm -f ${runfile}&quot; 0 1 2 5 15" -n \
		-o "touch ${runfile}" -n \
		-i "string-length(password) > 0" -v "concat('env RSYNC_PASSWORD=&quot;',password,'&quot; ')" -b \
		-o "rsync --verbose --log-file=&quot;${OMV_RSYNC_LOGFILE}&quot;" \
		-i "recursive = '0'" -o " --recursive" -b \
		-i "times = '1'" -o " --times" -b \
		-i "compress = '1'" -o " --compress" -b \
		-i "archive = '1'" -o " --archive" -b \
		-i "delete = '1'" -o " --delete" -b \
		-i "quiet = '1'" -o " --quiet" -b \
		-i "perms = '1'" -o " --perms" -b \
		-i "acls = '1'" -o " --acls" -b \
		-i "xattrs = '1'" -o " --xattrs" -b \
		-i "string-length(extraoptions) > 0" -v "concat(' ',extraoptions)" -b \
		-o " &quot;${srcuri}&quot; &quot;${desturi}&quot;" \
		${OMV_CONFIG_FILE} | xmlstarlet unesc > ${filename}
	  chmod ${OMV_RSYNC_CRONSCRIPTS_MASK} ${filename}
  done

# Create the cron script itself
xmlstarlet sel -t \
  -o "SHELL=/bin/sh" -n \
  -o "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" -n \
  -o "# m h dom mon dow user    command" -n \
  -m "//services/rsync/jobs/job[enable='1']" \
	  -i "everynminute[. = '1']" -v "concat('*/',minute,' ')" -b \
	  -i "everynminute[. = '0']" -v "concat(minute,' ')" -b \
	  -i "everynhour[. = '1']" -v "concat('*/',hour,' ')" -b \
	  -i "everynhour[. = '0']" -v "concat(hour,' ')" -b \
	  -i "everyndayofmonth[. = '1']" -v "concat('*/',dayofmonth,' ')" -b \
	  -i "everyndayofmonth[. = '0']" -v "concat(dayofmonth,' ')" -b \
	  -v "concat(month,' ',dayofweek,' ${OMV_RSYNC_CRONUSER} ${OMV_RSYNC_CRONSCRIPTS_DIR}/rsync-',uuid)" \
	  -i "sendemail[. = '0']" \
		  -o " &gt;/dev/null 2&gt;&amp;1" \
	  -b \
	  -n \
  -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_RSYNC_CRONSCRIPT}
chmod 644 ${OMV_RSYNC_CRONSCRIPT}
