#!/bin/sh
# ----------------------------------------------------------------------
# /var/install/config.d/rsnapshot -- rsnapshot configuration
#
# Creation:     2009-10-25 alex
# Last update:  $Id$
#
# Copyright (c) 2009,2011 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.
# ----------------------------------------------------------------------

#exec 2> /tmp/rsnapshot-trace_$$.log
#set -x

_PACKAGE_NAME='rsnapshot'

_RSNAPSHOT_BIN_PATH='/usr/bin/rsnapshot'

# ----------------------------------------------------------------------
# Includes
# ----------------------------------------------------------------------
. /var/install/include/eislib       # eislib
. /etc/default/${_PACKAGE_NAME}     # shared package defaults
. /etc/config.d/${_PACKAGE_NAME}    # package eisfair config

# ----------------------------------------------------------------------
# Default variables (not or not yet user changeable)
# ----------------------------------------------------------------------
_RSNAPSHOT_CONFIG_VERSION='1.2'

_RSNAPSHOT_CMD_CP='/bin/cp'
_RSNAPSHOT_CMD_RM='/bin/rm'
_RSNAPSHOT_CMD_RSYNC='/usr/bin/rsync'
_RSNAPSHOT_CMD_LOGGER='/usr/bin/logger'
_RSNAPSHOT_CMD_DU='/usr/bin/du'
_RSNAPSHOT_CMD_RSNAPSHOT_DIFF='/usr/bin/rsnapshot-diff'

_RSNAPSHOT_INTERVAL_DAILY='7'
_RSNAPSHOT_INTERVAL_WEEKLY='4'

_RSNAPSHOT_VERBOSE='2'
_RSNAPSHOT_LOGLEVEL='3'

# ----------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------
_add_missing_trailing_slash()
{
    # `expr substr <string> <offset> <length>` with <offset> determined
    # by `expr length <string>`
    _last_chr=$(expr substr ${1} $(expr length ${1}) 1)
    if [ "${_last_chr}" == '/' ]
    then
        echo "${1}"
    else
        echo "${1}/"
    fi
}

_add_cron_job()
{
    _crontab_path=`dirname ${RSNAPSHOT_CRONTAB_FILE}`
    # check for cron directory
    if [ ! -d ${_crontab_path} ]
    then
        mkdir -p ${_crontab_path}
    fi

    # write cronjob file
    _creation_time=`date --iso-8601='seconds'`
    {
        echo '# -------------------------------------------------------'
        echo "# ${RSNAPSHOT_CRONTAB_FILE} created by ${0}"
        echo '#'
        echo '# DO NOT EDIT THIS FILE BY HAND! (use eisfair setup!)'
        echo '#'
        echo "# Created: ${_creation_time}"
        echo '# -------------------------------------------------------'
    } > ${RSNAPSHOT_CRONTAB_FILE}

    case ${RSNAPSHOT_BACKUPS_PER_DAY} in
        2)
            echo "0 1,13 * * * ${_RSNAPSHOT_BIN_PATH} hourly" >> ${RSNAPSHOT_CRONTAB_FILE}
        ;;
        3)
            echo "0 1,9,17 * * * ${_RSNAPSHOT_BIN_PATH} hourly" >> ${RSNAPSHOT_CRONTAB_FILE}
        ;;
        4)
            echo "0 1,7,13,19 * * * ${_RSNAPSHOT_BIN_PATH} hourly" >> ${RSNAPSHOT_CRONTAB_FILE}
        ;;
        6)
            echo "0 1,5,9,13,17,21 * * * ${_RSNAPSHOT_BIN_PATH} hourly" >> ${RSNAPSHOT_CRONTAB_FILE}
        ;;
        8)
            echo "0 1,4,7,10,13,16,19,22 * * * ${_RSNAPSHOT_BIN_PATH} hourly" >> ${RSNAPSHOT_CRONTAB_FILE}
        ;;
        12)
            echo "0 1,3,5,7,9,11,13,15,17,19,21,23 * * * ${_RSNAPSHOT_BIN_PATH} hourly" >> ${RSNAPSHOT_CRONTAB_FILE}
        ;;
        *)
            mecho -warn 'Config not edited via setup menu!'
            mecho -warn 'Value for RSNAPSHOT_BACKUPS_PER_DAY not supported.'
            mecho -warn 'Falling back to default ...'
            echo "0 1,5,9,13,17,21 * * * ${_RSNAPSHOT_BIN_PATH} hourly" >> ${RSNAPSHOT_CRONTAB_FILE}
        ;;
    esac

    {
        echo "45 0 * * * ${_RSNAPSHOT_BIN_PATH} daily"
        echo "30 0 * * ${RSNAPSHOT_WEEKLY_BACKUP_DAY} ${_RSNAPSHOT_BIN_PATH} weekly"
        echo "15 0 1 * * ${_RSNAPSHOT_BIN_PATH} monthly"
    } >> ${RSNAPSHOT_CRONTAB_FILE}

    /var/install/bin/update-cron
}

_delete_cron_job()
{
    if [ -f ${RSNAPSHOT_CRONTAB_FILE} ]
    then
        rm -f ${RSNAPSHOT_CRONTAB_FILE}
        /var/install/bin/update-cron
    fi
}

_rsnapshot_conf_printvar()
{
    # variables are devided by tabstops

    # every statement has at least two vars
    echo -n "${1}	${2}"

    # optional third var
    if [ -z "${3}" ]
    then
        echo -n ''
    else
        echo -n "	${3}"
    fi

    # optional fourth var
    if [ -z "${4}" ]
    then
        echo ''
    else
        echo "	${4}"
    fi
}

_rsnapshot_create_conf()
{
    mecho -n "Creating ${_PACKAGE_NAME} configuration file ..."

    {
        # default, not changed values first

        echo '# -------------------------------------------------------'
        echo '# rsnapshot config created by eisfair setup menu'
        echo '# DO NOT EDIT THIS FILE DIRECTLY! USE EISFAIR SETUP!'
        echo '# -------------------------------------------------------'
        echo
        
        echo '# config file version'
        _rsnapshot_conf_printvar 'config_version' ${_RSNAPSHOT_CONFIG_VERSION}
        echo

        echo '# external program dependencies'
        _rsnapshot_conf_printvar 'cmd_cp'               ${_RSNAPSHOT_CMD_CP}
        _rsnapshot_conf_printvar 'cmd_rm'               ${_RSNAPSHOT_CMD_RM}
        _rsnapshot_conf_printvar 'cmd_rsync'            ${_RSNAPSHOT_CMD_RSYNC}
        _rsnapshot_conf_printvar 'cmd_logger'           ${_RSNAPSHOT_CMD_LOGGER}
        _rsnapshot_conf_printvar 'cmd_du'               ${_RSNAPSHOT_CMD_DU}
        _rsnapshot_conf_printvar 'cmd_rsnapshot_diff'   ${_RSNAPSHOT_CMD_RSNAPSHOT_DIFF}
        echo

        echo '# locking and logging'
        _rsnapshot_conf_printvar 'verbose'  ${_RSNAPSHOT_VERBOSE}
        _rsnapshot_conf_printvar 'loglevel' ${_RSNAPSHOT_LOGLEVEL}
        _rsnapshot_conf_printvar 'logfile'  ${RSNAPSHOT_LOGFILE}
        _rsnapshot_conf_printvar 'lockfile' ${RSNAPSHOT_LOCKFILE}
        echo

        echo '# backup intervals'
        _rsnapshot_conf_printvar 'interval' 'hourly'    ${RSNAPSHOT_BACKUPS_PER_DAY}
        _rsnapshot_conf_printvar 'interval' 'daily'     ${_RSNAPSHOT_INTERVAL_DAILY}
        _rsnapshot_conf_printvar 'interval' 'weekly'    ${_RSNAPSHOT_INTERVAL_WEEKLY}
        _rsnapshot_conf_printvar 'interval' 'monthly'   ${RSNAPSHOT_MONTHLY_BACKUPS_TO_KEEP}
        echo

        echo '# snapshot root'
        if [ "${RSNAPSHOT_CREATE_ROOT}" = 'yes' ]
        then
            _rsnapshot_conf_printvar 'no_create_root' '0'
        else
            _rsnapshot_conf_printvar 'no_create_root' '1'
        fi

        # eisfair config dependent values
        _rsnapshot_conf_printvar 'snapshot_root' $(_add_missing_trailing_slash ${RSNAPSHOT_BACKUP_TO_DIR})
        echo

        # local backup folders
        echo '# local backup folders'
        for _idx in `seq ${RSNAPSHOT_BACKUP_LOCAL_N}`
        do
            eval _active='$RSNAPSHOT_BACKUP_LOCAL_'${_idx}'_ACTIVE'
            eval _dir='$RSNAPSHOT_BACKUP_LOCAL_'${_idx}'_DIR'

            if [ "${_active}" = 'yes' ]
            then
                _rsnapshot_conf_printvar                    \
                    'backup'                                \
                    $(_add_missing_trailing_slash ${_dir})  \
                    'localhost/'
            fi
        done
        echo

        # end
        echo '# -------------------------------------------------------'
        echo '# vim:list'
    } > ${RSNAPSHOT_CONFIG}

    mecho ' Done.'
}

# ----------------------------------------------------------------------
# Main
# ----------------------------------------------------------------------

if [ "${START_RSNAPSHOT}" = 'yes' ]
then
    _rsnapshot_create_conf
    _add_cron_job
else
    # don't recreate native config if START_RSNAPSHOT='no' because
    # eischk does not check config in this case!
    _delete_cron_job
fi

exit 0
# ----------------------------------------------------------------------
# End
# ----------------------------------------------------------------------
