#!/bin/sh -
#
# $FreeBSD$
#

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

rc=0

case "${daily_backup_pkgng_enable:-YES}" in
	[Nn][Oo])
	;;
	*)
	bak="${daily_backup_pkgng_dir:-/var/backups}"
	bak_file="${bak}/pkgng.db"
	pkgcmd=/usr/local/sbin/pkg

	if ${pkgcmd} -N >/dev/null 2>&1 ; then
		echo 'pkg-backup is enabled but pkg is not used'
		rc=2
 	else
		if [ ! -d "$bak" ]
		then
			install -d -o root -g wheel -m 750 $bak || {
			echo 'pkg-backup is enabled but' \
				"$daily_backup_pkgng_dbdir doesn't exist" ;
			exit 2 ; }
		fi

		echo
		echo 'Backing up pkgng database:'

		[ -e "${bak_file}.txz" ] && \
			mv -f "${bak_file}.txz" "${bak_file}.txz.2"

		if ${pkgcmd} backup -d ${bak_file} 2>/dev/null; then
			rc=0
		else
			rc=3
		fi
	fi
	;;
esac

exit $rc
