#!/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://www.cyberciti.biz/faq/disable-the-mail-alert-by-crontab-command
# http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses
# http://wiki.dreamhost.com/index.php/Crontab

set -e

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

OMV_CRONTAB_POWERMNGMT_CONFIG=${OMV_CRONTAB_POWERMNGMT_CONFIG:-"/etc/cron.d/openmediavault-powermngmt"}
OMV_CRONTAB_USERDEFINED_CONFIG=${OMV_CRONTAB_USERDEFINED_CONFIG:-"/etc/cron.d/openmediavault-userdefined"}
OMV_CRONTAB_CRONSCRIPTS_DIR=${OMV_CRONTAB_CRONSCRIPTS_DIR:-"/var/lib/openmediavault/cron.d"}
OMV_CRONTAB_CRONSCRIPTS_MASK=${OMV_CRONTAB_CRONSCRIPTS_MASK:-"755"}

# Create the power management cron jobs
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 "//system/crontab/job[type='reboot' or type='shutdown'][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,' ',username,' ',command,' &gt;/dev/null 2&gt;&amp;1')" -n \
  -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_CRONTAB_POWERMNGMT_CONFIG}
chmod 644 ${OMV_CRONTAB_POWERMNGMT_CONFIG}

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

# Create the scripts
xmlstarlet sel -t -m "//system/crontab/job[enable='1' and type='userdefined']" \
  -v "uuid" \
  -i "position() != last()" -n -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc |
  while read uuid; do
	  filename="${OMV_CRONTAB_CRONSCRIPTS_DIR}/userdefined-${uuid}"
	  xmlstarlet sel -t -m "//system/crontab/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 \
		-v command \
		${OMV_CONFIG_FILE} | xmlstarlet unesc > ${filename}
	  chmod ${OMV_CRONTAB_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 "//system/crontab/job[enable='1' and type='userdefined']" \
	  -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,' ',username,' ${OMV_CRONTAB_CRONSCRIPTS_DIR}/userdefined-',uuid)" \
	  -i "sendemail[. = '0']" \
		  -o " &gt;/dev/null 2&gt;&amp;1" \
	  -b \
	  -n \
  -b \
  ${OMV_CONFIG_FILE} | xmlstarlet unesc > ${OMV_CRONTAB_USERDEFINED_CONFIG}
chmod 644 ${OMV_CRONTAB_USERDEFINED_CONFIG}
