# $Id: Portfile,v 1.10 2006/04/04 22:55:42 markd Exp $
PortSystem		1.0

name			boxbackup
version			0.10
categories		sysutils net
maintainers		ecronin@gizmolabs.org
description		completely automatic on-line backup system for UNIX.
long_description	The backup daemon, bbackupd, runs on all machines to \
					be backed up. The store server daemon, bbstored runs \
					on a central server. Data is sent to the store \
					server, which stores all data on local filesystems, \
					that is, only on local hard drives. Tape or other \
					archive media is not used. \
					The system is designed to be easy to set up and run, \
					and cheap to use. Once set up, there should be no \
					need for user or administrative intervention, apart \
					from usual system maintenance.
homepage		http://www.fluffy.co.uk/boxbackup/
master_sites	sourceforge
extract.suffix	.tgz
checksums		md5 2dc02d87932fdb222ed019443ddcf444
patchfiles		patch-BoxPlatform.pm.in

depends_lib		port:perl5.8 \
			port:zlib \
			port:openssl

post-patch {
	reinplace "s|ac_default_prefix=/usr/local|ac_default_prefix=${prefix}|g" ${worksrcpath}/configure
	reinplace "s|@bindir_expanded@|${prefix}/bin|g" ${worksrcpath}/infrastructure/BoxPlatform.pm.in
	reinplace "s|__PREFIX__|${prefix}|g" ${worksrcpath}/infrastructure/BoxPlatform.pm.in
	reinplace "s|\$install_into_dir|${destroot}\$install_into_dir|g" ${worksrcpath}/infrastructure/makeparcels.pl

	cd ${worksrcpath}
	foreach file [exec find . -type f ( -name *.pl -o -name *-config -o -name *-certs -o -name *.cpp )] {
		reinplace "s|/usr/bin/perl|${prefix}/bin/perl|g" ${file}
	}
	foreach file { \
			bin/bbstored/BackupConstants.h \
			bin/bbackupd/bbackupd-config \
			bin/bbstored/bbstored-certs \
			bin/bbstored/bbstored-config \
			lib/common/BoxPortsAndFiles.h \
			lib/raidfile/RaidFileController.h \
			lib/raidfile/raidfile-config \
		} {
		reinplace "s|/usr/local|${prefix}|g" ${file}
		reinplace "s|/var/run|${prefix}/var/run|g" ${file}
		reinplace "s|/var/bbackupd|${prefix}/var/bbackupd|g" ${file}
		reinplace "s|/etc/box|${prefix}/etc/box|g" ${file}
	}
}

configure.env	PREFIX='${prefix}'
configure.pre_args

destroot.target		install-backup-client
destroot.keepdirs	${destroot}${prefix}/etc/box \
					${destroot}${prefix}/etc/box/bbackupd \
					${destroot}${prefix}/var/run \
					${destroot}${prefix}/var/bbackupd
post-build {
# Since this app does not support DESTDIR ....
                reinplace "s|/usr/local|${destroot}${prefix}|g" ${worksrcpath}/parcels/scripts/install-backup-client
                reinplace "s|/usr/local|${destroot}${prefix}|g" ${worksrcpath}/parcels/scripts/install-backup-server
}

post-destroot {
	xinstall -d -m 0700 ${destroot}${prefix}/etc/box/bbackupd
	xinstall -d -m 0700 ${destroot}${prefix}/var/bbackupd

	xinstall -d -m 0755 ${destroot}${prefix}/share/doc/${name}
	foreach docname {accounts bbackupctl client license nonroot restore retrieve server serverfix space trouble} {
		xinstall -m 0644 ${filespath}/${docname}.html \
			${destroot}${prefix}/share/doc/${name}/${docname}.html
		reinplace "s|/usr/local|${prefix}|g" \
			${destroot}${prefix}/share/doc/${name}/${docname}.html
		reinplace "s|/var/run|${prefix}/var/run|g" \
			${destroot}${prefix}/share/doc/${name}/${docname}.html
		reinplace "s|/var/bbackupd|${prefix}/var/bbackupd|g" \
			${destroot}${prefix}/share/doc/${name}/${docname}.html
		reinplace "s|/etc/box|${prefix}/etc/box|g" \
			${destroot}${prefix}/share/doc/${name}/${docname}.html
	}

	# Create startup rc script (if non-Darwin)
	if {![variant_isset darwin]} {
		xinstall -d -m 0755 ${destroot}${prefix}/etc/rc.d
		xinstall -m 0755 ${filespath}/boxbackup.sh.in \
			${destroot}${prefix}/etc/rc.d/boxbackup.sh
		reinplace "s|__PREFIX__|${prefix}|g" ${destroot}${prefix}/etc/rc.d/boxbackup.sh
	}
}

variant server {
	pre-destroot {
		addgroup bbstored
		set gid [existsgroup bbstored]
		adduser bbstored gid=${gid} realname=BoxBackup\ Server home=${prefix}/etc/box/bbstored
	}
	destroot.target-append		install-backup-server
	destroot.keepdirs-append	${destroot}${prefix}/etc/box/bbstored
	post-destroot {
		xinstall -o bbstored -g bbstored -d -m 0700 ${destroot}${prefix}/etc/box/bbstored
	}

	pre-install {
		addgroup bbstored
		set gid [existsgroup bbstored]
		adduser bbstored gid=${gid} realname=BoxBackup\ Server home=${prefix}/etc/box/bbstored
	}
}

post-activate {
	if {[variant_isset server]} {
		if {![file exists ${prefix}/etc/box/raidfile.conf]} {
			ui_warn "${prefix}/etc/box/raidfile.conf is missing!"
			ui_msg "You need to run 'raidfile-config' to create it."
			ui_msg "See the documentation on server setup for details:"
			ui_msg "  file://${prefix}/share/doc/boxbackup/server.html"
		}
		if {![file exists ${prefix}/etc/box/bbstored.conf]} {
			ui_warn "${prefix}/etc/box/bbstored.conf is missing!"
			ui_msg "You need to run 'bbstored-config' to create it."
			ui_msg "See the documentation on server setup for details:"
			ui_msg "  file://${prefix}/share/doc/boxbackup/server.html"
		}
	}
	if {![file exists ${prefix}/etc/box/bbackupd.conf]} {
		ui_warn "${prefix}/etc/box/bbackupd.conf is missing!"
		ui_msg "You need to run 'bbackupd-config' to create it."
		ui_msg "See the documentation on client setup for details:"
		ui_msg "  file://${prefix}/share/doc/boxbackup/client.html"
	}
}

platform darwin {
	startupitem.create			yes
	startupitem.name			BoxBackup
	startupitem.start			"cd ${prefix}/etc/box"
	if {[variant_isset server]} {
		startupitem.start-append	"\[ -f \"${prefix}/etc/box/bbstored.conf\" \] \\"
		startupitem.start-append	"\t&& \[ -f \"${prefix}/etc/box/raidfile.conf\" \] \\"
		startupitem.start-append	"\t&& ${prefix}/bin/bbstored"
	}
	startupitem.start-append	"\[ -f \"${prefix}/etc/box/bbackupd.conf\" \] \\"
	startupitem.start-append	"\t&& ${prefix}/bin/bbackupd"
	startupitem.stop			"/usr/bin/killall -SIGUSR1 bbackupd"
	if {[variant_isset server]} {
		startupitem.stop-append	"/usr/bin/killall -SIGUSR1 bbstored"
	}
}

