#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
# 2013      Nico Schottelius (nico-cdist at schottelius.org)
# 2013      Thomas Oettli (otho at sfs.biz)
# 2017      Daniel Heule (hda at sfs.biz)
#
# This file is part of cdist.
#
# cdist 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
# (at your option) any later version.
#
# cdist 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 cdist. If not, see <http://www.gnu.org/licenses/>.
#

name="$__object_name"
user="$(cat "$__object/parameter/user")"
command="$(cat "$__object/parameter/command")"

if [ -f "$__object/parameter/raw" ]; then
   raw="$(cat "$__object/parameter/raw")"
   entry="$raw $command # $name"
elif [ -f "$__object/parameter/raw_command" ]; then
   entry="$command"
else
   minute="$(cat "$__object/parameter/minute" 2>/dev/null || echo "*")"
   hour="$(cat "$__object/parameter/hour" 2>/dev/null || echo "*")"
   day_of_month="$(cat "$__object/parameter/day_of_month" 2>/dev/null || echo "*")"
   month="$(cat "$__object/parameter/month" 2>/dev/null || echo "*")"
   day_of_week="$(cat "$__object/parameter/day_of_week" 2>/dev/null || echo "*")"
   entry="$minute $hour $day_of_month $month $day_of_week $command # $name"
fi

mkdir "$__object/files"
echo "$entry" > "$__object/files/entry"

if diff -q "$__object/files/entry" "$__object/explorer/entry" >/dev/null; then
    state_is=present
else
    state_is=absent
fi

state_should="$(cat "$__object/parameter/state" 2>/dev/null || echo "present")"

[ "$state_is" = "$state_should" ] && exit 0

# If anything is going to change, ensure the old entries are
# not present anymore

# These are the old markers
prefix="#cdist:__cron/$__object_id"
suffix="#/cdist:__cron/$__object_id"
filter="^# DO NOT EDIT THIS FILE|^# \(.* installed on |^# \(Cron version V|^# \(Cronie version .\..\)$"
cat << DONE
crontab -u $user -l 2>/dev/null | grep -v -E "$filter" | awk -v prefix="$prefix" -v suffix="$suffix" '
{
   if (index(\$0,prefix)) {
      triggered=1
   }
   if (triggered) {
      if (index(\$0,suffix)) {
            triggered=0
      }
   } else {
      print
   }
}
' | crontab -u $user -
DONE

case "$state_should" in
    present)
        # if we insert new entry, filter also all entrys out with the same id 
        echo "("
        echo "crontab -u $user -l 2>/dev/null | grep -v -E \"$filter\" | grep -v \"# $name\\$\" 2>/dev/null || true"
        echo "echo '$entry'"
        echo ") | crontab -u $user -"
    ;;
    absent)
        if [ -f "$__object/parameter/raw_command" ]; then
            echo "( crontab -u $user -l 2>/dev/null | grep -v -E \"$filter\" 2>/dev/null || true ) | \\"
            echo "grep -v \"^$entry\\$\" | crontab -u $user -"
        else
            echo "( crontab -u $user -l 2>/dev/null | grep -v -E \"$filter\" 2>/dev/null || true ) | \\"
            echo "grep -v \"# $name\\$\" | crontab -u $user -"
        fi
    ;;
esac
