# $Id: Portfile 92837 2012-05-09 00:51:48Z ryandesign@macports.org $
PortSystem		1.0

name			cfengine
version			2.2.1
revision		3
categories		sysutils
maintainers		nomaintainer
description		a tool for setting up and maintaining computer systems
long_description	Cfengine, or the configuration engine is an \
					autonomous agent and a middle to high level \
					policy language for building expert systems \
					which administrate and configure large computer \
					networks. 
homepage		http://www.cfengine.org/
master_sites	ftp://ftp.iu.hio.no/pub/cfengine/ \
				ftp://sunsite.uio.no/pub/cfengine/ \
				http://ftp.us.xemacs.org/ftp/pub/cfengine/cfengine-ftp/
checksums		sha1 ce4954174110c43541d25775747ae65ada30063d
platforms		darwin

depends_lib		port:db44 \
				port:openssl \
				port:pcre

patchfiles		patch-pub__getopt.c
post-patch {
	foreach manfile [glob ${worksrcpath}/doc/*.8] {
		reinplace "s|/usr/local|${prefix}|g" $manfile
		reinplace "s|/var/cfengine|${prefix}&|g" $manfile
	}
}

set cfworkdir	${prefix}/var/cfengine

configure.cflags	-I${prefix}/include/db44 -I${prefix}/include
configure.ldflags	-L${prefix}/lib/db44 -L${prefix}/lib
configure.args	--mandir='\${prefix}/share/man' \
				--infodir='\${prefix}/share/info' \
				--with-workdir="${cfworkdir}" \
				--with-berkeleydb=default \
				--with-openssl=default \
				--with-pcre=default \
				--with-docs

build.args		ps_DATA=
destroot.args	ps_DATA=

pre-destroot {
	if {[geteuid] != 0} {
		ui_msg "-----------------------------------------------------------"
		ui_msg "Note that you are not running as root, so files installed"
		ui_msg "by this port will not end up with proper ownership and"
		ui_msg "likely not work correctly with ${name}."
		ui_msg "-----------------------------------------------------------"
	}
}

destroot.keepdirs	${destroot}${cfworkdir}/bin \
					${destroot}${cfworkdir}/inputs \
					${destroot}${cfworkdir}/modules \
					${destroot}${cfworkdir}/outputs \
					${destroot}${cfworkdir}/ppkeys \
					${destroot}${cfworkdir}/rpc_in \
					${destroot}${cfworkdir}/rpc_out \
					${destroot}${cfworkdir}/state
post-destroot {
	# Create cfengine directories
	xinstall -d -m 0755 ${destroot}${cfworkdir}
	# Create cfengine sub-directories
	foreach subdir {bin modules state} {
		xinstall -d -m 0755 ${destroot}${cfworkdir}/${subdir}
	}
	foreach subdir {inputs masterfiles/inputs outputs ppkeys rpc_in rpc_out} {
		xinstall -d -m 0700 ${destroot}${cfworkdir}/${subdir}
	}
	# Create distribution sample conf files
	set hostname [exec hostname]
	set host [lindex [split ${hostname} {.}] 0]
	set domain [join [lrange [split ${hostname} {.}] 1 end] {.}]
	set timezone [exec date +%Z]
	foreach conf {cfagent.conf cfservd.conf update.conf cfrun.hosts} {
		xinstall -m 0644  ${filespath}/${conf}.in \
			${destroot}${cfworkdir}/masterfiles/inputs/${conf}-dist
		reinplace "s|__PREFIX|${prefix}|g" \
			${destroot}${cfworkdir}/masterfiles/inputs/${conf}-dist
		reinplace "s|__CFWORKDIR|${cfworkdir}|g" \
			${destroot}${cfworkdir}/masterfiles/inputs/${conf}-dist
		reinplace "s|__HOST|${host}|g" \
			${destroot}${cfworkdir}/masterfiles/inputs/${conf}-dist
		reinplace "s|__DOMAIN|${domain}|g" \
			${destroot}${cfworkdir}/masterfiles/inputs/${conf}-dist
		reinplace "s|__TIMEZONE|${timezone}|g" \
			${destroot}${cfworkdir}/masterfiles/inputs/${conf}-dist
	}
	# Create startup rc script (if non-Darwin)
	if {${os.platform} != "darwin"} {
		xinstall -d -m 0755 ${destroot}${prefix}/etc/rc.d
		xinstall -m 0755 ${filespath}/${name}.sh.in \
			${destroot}${prefix}/etc/rc.d/${name}.sh
		reinplace "s|__PREFIX|${prefix}|" \
			${destroot}${prefix}/etc/rc.d/${name}.sh
		reinplace "s|__CFWORKDIR|${cfworkdir}|" \
			${destroot}${prefix}/etc/rc.d/${name}.sh
	}
}

post-activate {
	# Make sure initial conf files are present and setup correctly
	if {![file exists ${cfworkdir}/inputs/update.conf]} {
		xinstall -m 0644 ${cfworkdir}/masterfiles/inputs/update.conf-dist \
			${cfworkdir}/inputs/update.conf
	}
	foreach conf {cfagent.conf cfservd.conf update.conf cfrun.hosts} {
		if {![file exists ${cfworkdir}/masterfiles/inputs/${conf}]} {
			xinstall -m 0644 ${cfworkdir}/masterfiles/inputs/${conf}-dist \
				${cfworkdir}/masterfiles/inputs/${conf}
		}
	}
	# Check for root public-private keys, create if missing
	if {![file exists ${cfworkdir}/ppkeys/localhost.priv] \
		|| ![file exists ${cfworkdir}/ppkeys/localhost.pub]} {
		if {[file exists ${cfworkdir}/ppkeys/localhost.priv]} {
			delete ${cfworkdir}/ppkeys/localhost.priv
		}
		if {[file exists ${cfworkdir}/ppkeys/localhost.pub]} {
			delete ${cfworkdir}/ppkeys/localhost.pub
		}
		system "${prefix}/sbin/cfkey"
	}
}

variant psdocs {
	depends_build			bin:tex:texlive \
							bin:texinfo:texinfo
	build.args-delete		ps_DATA=
	destroot.args-delete	ps_DATA=
}

platform darwin {
	startupitem.create			yes
	startupitem.name			Cfengine
	startupitem.start			"\[ -x ${cfworkdir}/bin/cfservd \] && ${cfworkdir}/bin/cfservd"
	startupitem.start-append	"\[ -x ${cfworkdir}/bin/cfenvd \] && ${cfworkdir}/bin/cfenvd -H"
	startupitem.start-append	"\[ -x ${cfworkdir}/bin/cfexecd \] && ${cfworkdir}/bin/cfexecd"
	startupitem.start-append	"(\[ -x ${cfworkdir}/bin/cfservd \] && \[ -x ${cfworkdir}/bin/cfenvd \] && \\"
	startupitem.start-append	" \[ -x ${cfworkdir}/bin/cfexecd \] && \[ -x ${cfworkdir}/bin/cfagent \]) \\"
	startupitem.start-append	"\t|| ${prefix}/sbin/cfagent --no-splay -f update.conf"
	startupitem.stop			"/usr/bin/killall -SIGUSR1 cfagent 2>/dev/null"
	startupitem.stop-append		"/usr/bin/killall -SIGUSR1 cfexecd 2>/dev/null"
	startupitem.stop-append		"/usr/bin/killall -SIGUSR1 cfenvd 2>/dev/null"
	startupitem.stop-append		"/usr/bin/killall -SIGUSR1 cfservd 2>/dev/null"
}

