#!/bin/sh
#
# periodic(8) script that runs duply on a nightly basis
#
# Define these variables in either /etc/periodic.conf or
# /etc/periodic.conf.local to override the defaults.
#
# Configuration Settings (with default values):
#
# backup_duply_enable="NO"
#	enable duply backup
#
# backup_duply_profile=""
#	default duply profile to use
#	see duply usage
#
# backup_duply_command="bkp_status"
#	default duply commands
#
# backup_duply_opt=""
#	options passed to duply
#
# backup_duply_random="3600"
#	randomize backup time
#	In FreeBSD 12.0 is used the anticongestion function (3600) and this
#	variable is ignored
#

if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

: ${backup_duply_enable}="NO"
: ${backup_duply_command}="bkp_status"
: ${backup_duply_profile}=""
: ${backup_duply_opt}=""
: ${backup_duply_random}="3600"

PATH=/bin:/sbin:/usr/bin:/usr/sbin:%%LOCALBASE%%/bin:%%LOCALBASE%%/sbin
export PATH

case "$backup_duply_enable" in
    [Yy][Ee][Ss])
	# In FreeBSD 12.0 the anticongestion function should be used
	# instead of a hard-coded sleep
	if [ -n "$anticongestion_sleeptime" ]; then
		anticongestion
	else
		eval sleep $(jot -r 1 0 ${backup_duply_random})
	fi
	echo
        echo "Duply Backup:"
        for duply_profile in ${backup_duply_profile}
	do
	  eval duply ${duply_profile} ${backup_duply_command} ${backup_duply_opt}
	  if [ $? -gt 0 ]
	  then
	    echo "Errors were reported during backup."
	    rc=3
	    break
	  fi
	done
	;;
esac

exit $rc

