#!/bin/sh
#----------------------------------------------------------------------------
# /var/install/config.d/cron.sh - cron configuration file
#
# Creation:     2003-08-31 fm
# Last Update:  $Id: cron.sh 35054 2014-02-05 20:17:31Z hbfl $
#
# Usage:         cron.sh
#                cron.sh --debug    - activate debug output
#                cron.sh --quiet    - suppress output
#                cron.sh --help     - show command line options
#
# Copyright (c) 2003-2014 the eisfair team, team(at)eisfair(dot)org
#
# This program 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 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# added config file
. /etc/config.d/cron

# ---------------------------------------------------------------------------
# usage
# ---------------------------------------------------------------------------
usage ()
{
cat <<EOF

 Usage:
 ${0}
                --quiet      suppress all normal output
                --debug
                --help

EOF
}

# ===========================================================================
# main
# ===========================================================================

cron_path=/var/cron/etc
tmp_cronfile=/tmp/cron.$$

if [ $# -gt 0 ]
then
    # get parameters
    debug=''
    quiet=''

    while [ 1 ]
    do
        case "$1" in
        '--debug'|'-debug')
            # debug mode
            debug='-d'
            shift
            ;;
        '--help'|'-help'|'-?'|'/?')
            # show help
            usage
            exit 1
            ;;
        '--quiet'|'-quiet')
            # quiet mode
            quiet='--quiet'
            shift
            ;;
        *)
            break
            ;;
        esac
    done
fi

cd ${cron_path}

# create directory if it does not already exist
if [ ! -d ${cron_path}/root ]
then
    mkdir -p ${cron_path}/root
fi

if [ -z "${CRON_N}" ]
then
    # initialize variable
    CRON_N='0'
fi

if [ ${CRON_N} -eq 0 ]
then
    # create empty file
    touch ${tmp_cronfile}
fi

# remove temporary file
if [ -e ${tmp_cronfile} ]
then
    rm -rf ${tmp_cronfile}
fi

# write cron configuration to file
idx=1
while [ "${idx}" -le "${CRON_N}" ]
do
    # check for active
    eval active='${CRON_'${idx}'_ACTIVE}'

    if [ "${active}" = "yes" ]
    then
        eval name='$CRON_'${idx}'_NAME'
        eval time='$CRON_'${idx}'_TIMES'
        eval command='$CRON_'${idx}'_COMMAND'

        if [ -n "${name}" ]
        then
            # print description if exists
            echo "# ${name}" >> ${tmp_cronfile}
        fi

        echo "${time} ${command}" >> ${tmp_cronfile}
    fi

    idx=`/usr/bin/expr ${idx} + 1`
done

# remove backup file with '~'
find ${cron_path}/root/ -type f -name "*~" -exec rm -f {} \;
# append existing cronjobs to file and reload it
cat ${cron_path}/root/* >> ${tmp_cronfile} 2>/dev/null

if [ "${quiet}" = "--quiet" -a "${debug}" = "" ]
then
    # quiet mode, suppress output
    fcrontab - ${debug} -u root < ${tmp_cronfile} >/dev/null 2>/dev/null
else
    # show output
    fcrontab - ${debug} -u root < ${tmp_cronfile}
fi

# remove temporary file
rm -f ${tmp_cronfile}

cd ${cron_path}

for user in *
do
    if [ -d ${user} ]
    then
        # directory, go on ...
        case ${user} in
        root)   :  ;;      # see above
        *)
            _restart_required=true
            if [ "${quiet}" = "--quiet" -a "${debug}" = "" ]
            then
                # quiet mode, suppress output
                cat ${user}/* 2>/dev/null | fcrontab - ${debug} -u ${user} >/dev/null 2>/dev/null
            else
                # show output
                cat ${user}/* 2>/dev/null | fcrontab - ${debug} -u ${user}
            fi
            ;;
        esac
    fi
done

if ${_restart_required:-false}
then
    /etc/init.d/cron restart
fi
# ===========================================================================
# end
# ===========================================================================
