#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
#
# 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_id"
user="$(cat "$__object/parameter/user")"
command="$(cat "$__object/parameter/command")"

# set defaults
test -f "$__object/parameter/state" || echo "present" > "$__object/parameter/state"

if [ -f "$__object/parameter/raw" ]; then
   raw="$(cat "$__object/parameter/raw")"
   entry="$raw $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"
fi

# NOTE: if changed, also change in explorers
prefix="#cdist:__cron/$name"
suffix="#/cdist:__cron/$name"
echo "$prefix" | tee "$__object/parameter/prefix" > "$__object/parameter/entry"
echo "$entry" >> "$__object/parameter/entry"
echo "$suffix" | tee "$__object/parameter/suffix" >> "$__object/parameter/entry"
