#
# This is the makefile template for the platform directory
# which contains general platform installation.
#
#  17 August 2009 -- Lorenz Schori
# 
#   for Bacula release @VERSION@ (@DATE@) -- @DISTNAME@
#


# bacula version and download site
BACULA_VERSION:=@VERSION@
BACULA_DL_URL:=http://downloads.sourceforge.net/project/bacula/bacula/${BACULA_VERSION}/bacula-${BACULA_VERSION}.tar.gz

# Build universal binary. Comment out when building versions of bacula < 3.0.0
ARCHFLAGS:=-arch i386 -arch ppc
MACOSX_SDK_SYSROOT:=/Developer/SDKs/MacOSX10.4u.sdk
MACOSX_VERSION_FLAGS:=-mmacosx-version-min=10.4

# Tools
PM:=/Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/PackageMaker
MAKE:=/usr/bin/make
CURL:=/usr/bin/curl
TAR:=/usr/bin/tar

########### you should not have to edit anything beyond this line ###########

# Build paths
DL_DIR:=dl
BUILD_DIR:=build
PRODUCTS_DIR:=products

WORKING_DIR:=${BUILD_DIR}/${BACULA_VERSION}
BACULA_TAR:=${DL_DIR}/bacula-${BACULA_VERSION}.tar.gz
BACULA_SOURCE:=${WORKING_DIR}/bacula-${BACULA_VERSION}
BACULA_DESTDIR:=${WORKING_DIR}/destdir
BACULA_PREFIX:=/usr/local/bacula-${BACULA_VERSION}
BACULA_FD_CONF:=/Library/Preferences/bacula/bacula-fd.conf
BACULA_WORKING_DIR:=/private/var/bacula/working
BACULA_PMDOC:=${WORKING_DIR}/installer.pmdoc

# Detect whether we sit inside the bacula source tree. In this case we won't
# download the tar from sourceforge but instead work with what its there
# already
CURSUB:=$(CURDIR:%/platforms/osx=%)
ifneq ($(CURDIR),$(CURSUB))
	BACULA_TAR:=
	BACULA_SOURCE:=../../
#	BACULA_VERSION:=$(shell sed -n 's,^VERSION=,,p' $(CURSUB)/autoconf/Make.common)
endif

PACKAGE_TITLE:=Bacula File Daemon ${BACULA_VERSION}
PACKAGE_ID:=org.bacula.bacula-fd.pkg
PACKAGE_DIR:=${PRODUCTS_DIR}/${PACKAGE_TITLE}
PACKAGE_BUNDLE:=${PACKAGE_DIR}/${PACKAGE_TITLE}.pkg
PACKAGE_DMG:=${PRODUCTS_DIR}/${PACKAGE_TITLE}.dmg
PACKAGE_RESOURCES:=ReadMe.html postflight preupgrade
PACKAGE_XRESOURCES:=postflight preupgrade

# Flags for the toolchain
CONFIGFLAGS:=--enable-client-only --prefix=${BACULA_PREFIX} \
    --with-dir-password=@DIR_PW@ --with-fd-password=@FD_PW@ \
    --with-sd-password=@SD_PW@ --with-mon-dir-password=@MON_DIR_PW@ \
    --with-mon-fd-password=@MON_FD_PW@ --with-mon-sd-password=@MON_SD_PW@ \
    --with-basename=@BASENAME@ --with-hostname=@HOSTNAME@ \
    --with-working-dir=${BACULA_WORKING_DIR}
CPPFLAGS:=-isysroot ${MACOSX_SDK_SYSROOT} ${MACOSX_VERSION_FLAGS}
CFLAGS:=-O -g ${ARCHFLAGS}
CXXFLAGS:=${CFLAGS}
LDFLAGS:=-Wl,-syslibroot,${MACOSX_SDK_SYSROOT} ${MACOSX_VERSION_FLAGS} ${ARCHFLAGS}

# required on snow leopard: compiling for 10.4 requires usage of gcc 4.0
# system defaults to version 4.2
CC:=gcc-4.0
CPP:=cpp-4.0
CXX:=g++-4.0
CXXPP:=cpp-4.0

# Placeholders for *.in files
INFILE_SUBST=\
  -e "s,@PREFIX@,${BACULA_PREFIX},g" \
  -e "s,@BACULA_VERSION@,${BACULA_VERSION},g" \
  -e "s,@FD_CONF@,${BACULA_FD_CONF},g" \
  -e "s,@BACULA_DESTDIR@,${BACULA_DESTDIR},g" \
  -e "s,@PACKAGE_ID@,${PACKAGE_ID},g"

dmg: pkg
	hdiutil create -srcfolder "${PACKAGE_DIR}" "${PACKAGE_DMG}"

pkg: ${BACULA_DESTDIR} ${BACULA_PMDOC} ${WORKING_DIR}/resources
	mkdir -p "${PACKAGE_DIR}"

	${PM} --doc "${BACULA_PMDOC}" --id "${PACKAGE_ID}" --no-recommend --out "${PACKAGE_BUNDLE}"

	cp ${WORKING_DIR}/resources/ReadMe.html "${PACKAGE_DIR}/ReadMe.html"

	sed ${INFILE_SUBST} \
		files/uninstall.command.in > "${PACKAGE_DIR}/uninstall.command";
	chmod 0775 "${PACKAGE_DIR}/uninstall.command"

${BACULA_PMDOC}: ${BACULA_DESTDIR} ${WORKING_DIR}/resources
	mkdir -p "${BACULA_PMDOC}"

	for f in index.xml 01destdir.xml; do \
		sed ${INFILE_SUBST} \
			files/installer.pmdoc.in/$$f > "${BACULA_PMDOC}/$$f"; \
	done

	python installer-gencontents.py ${BACULA_DESTDIR} > ${BACULA_PMDOC}/01destdir-contents.xml

${WORKING_DIR}/resources: ${BACULA_DESTDIR}
	mkdir -p "${WORKING_DIR}/resources"

	for res in ${PACKAGE_RESOURCES}; do \
		sed ${INFILE_SUBST} \
			resources/$$res.in > "${WORKING_DIR}/resources/$$res"; \
	done

	for xres in ${PACKAGE_XRESOURCES}; do \
		chmod +x "${WORKING_DIR}/resources/$$xres"; \
	done

	cp "${BACULA_SOURCE}/LICENSE" "${WORKING_DIR}/resources/License.txt"

${BACULA_DESTDIR}: ${BACULA_SOURCE}
	(cd ${BACULA_SOURCE} && ./configure ${CONFIGFLAGS} CPPFLAGS="${CPPFLAGS}" CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" LDFLAGS="${LDFLAGS}" CC="${CC}" CPP="${CPP}" CXX="${CXX}" CXXPP="${CXXPP}")
	${MAKE} -C ${BACULA_SOURCE}
	${MAKE} -C ${BACULA_SOURCE} install DESTDIR="${CURDIR}/${BACULA_DESTDIR}"

	rm -rf "${BACULA_DESTDIR}/tmp"

	for conffile in ${BACULA_DESTDIR}${BACULA_PREFIX}/etc/*.conf; do \
		mv $$conffile $$conffile.example; \
	done

	mkdir -p "${BACULA_DESTDIR}${BACULA_PREFIX}/Library/LaunchDaemons"
	sed ${INFILE_SUBST} files/org.bacula.bacula-fd.plist.in \
		> "${BACULA_DESTDIR}${BACULA_PREFIX}/Library/LaunchDaemons/org.bacula.bacula-fd.plist"

${BACULA_SOURCE}: ${BACULA_TAR}
	mkdir -p "${WORKING_DIR}"
	${TAR} -xzf "${BACULA_TAR}" -C "${WORKING_DIR}"

${BACULA_TAR}:
	mkdir -p "${DL_DIR}"
	${CURL} -L -o "${BACULA_TAR}" "${BACULA_DL_URL}"

.PHONY: distclean
distclean: clean
	rm -rf "${DL_DIR}" "${PRODUCTS_DIR}"

.PHONY: clean
clean:
	rm -rf "${BUILD_DIR}" "${PACKAGE_DIR}" "${PACKAGE_DMG}"

