# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/app-admin/logrotate/logrotate-3.8.4.ebuild,v 1.2 2013/06/10 20:32:29 jer Exp $

EAPI=5

inherit eutils toolchain-funcs flag-o-matic

DESCRIPTION="Creates rolling zfs snapshots"
HOMEPAGE="https://github.com/graudeejs/zfSnap"
SRC_URI="https://github.com/graudeejs/zfSnap/archive/v${PV}.zip"

LICENSE="BEER-WARE"
SLOT="0"
KEYWORDS="~amd64-fbsd"
IUSE=""

RDEPEND=">=sys-apps/sed-4
	>=sys-freebsd/freebsd-cddl-9.0
	sys-devel/bc"
DEPEND=">=sys-apps/sed-4
	app-arch/unzip"

S="${WORKDIR}/zfSnap-${PV}"

src_unpack() {
	unpack ${A}
}

src_prepare() {
	for period in hourly daily weekly monthly; do
		cp xPERIODICx_zfSnap.sh ${period}_zfSnap.sh

		sed -i -e "s/xPERIODICx/${period}/g" -e "s|xPREFIXx|${EPREFIX}/usr/sbin|g" ${period}_zfSnap.sh || die "Failed to sed ${period} file"

		cp xPERIODICx_zfSnap_delete.sh ${period}_zfSnap_delete.sh
		sed -i -e "s/xPERIODICx/${period}/g" -e "s|xPREFIXx|${EPREFIX}/usr/sbin|g" ${period}_zfSnap_delete.sh || die "Failed to sed ${period} delete file"
	done
	mv zfSnap.sh zfSnap
}

src_install() {
	insinto /usr
	dosbin zfSnap

	for period in hourly daily weekly monthly; do
		exeinto /etc/cron.${period}
		newexe "${S}"/${period}_zfSnap.sh "${PN}"
		newexe "${S}"/${period}_zfSnap_delete.sh "${PN}_delete"
	done
}

pkg_postinst() {
	elog "Add configuration to /etc/defaults/periodic.conf"
}