#!/bin/sh

# ${pb}/patches/applyPatches < [-b build] | [-j jail] | [-p portstree] >
#
# This directory is used to apply patches to either a portstree, build
# or jail which can be done via the supplied script used as a hook
# command. This is useful for patch testing builds or for common local
# patches used by a site.  The layout used by this script for patches is
# as follows:
#
#${pb}/patches/
#		builds/
#			build-1/
#			build-2/
#			GLOBAL
#		jails/
#			jail-1/
#			jail-2/
#			GLOBAL/
#		portstrees/
#			portstree-1/
#			portstree-2/
#			GLOBAL/
#
# In all cases patches in the GLOBAL directory will be applied first.
#
# builds/:
# Patches placed here are for patching the build environement and useful
# to use as a postBuildExtract hook.  These patches should be rooted at
# the root (/) of the build.
#
# jails/:
# Patches placed here are for patching a jail's src/ tree and useful as
# a postJailUpdate or preJailBuild hook. These patches should be rooted
# under src/.
#
# portstrees/:
# Patches placed here are for patching a portstree and useful as a
# postPortsTreeUpdate hook.  These patches should be rooted under
# ports/.
#

# TODO: - A build patch is rooted at '/' while src and ports patches are
#         rooted under their respective trees. There should be a means
#         of indicating a src or ports patch so patches can easilly be
#         reused between builds and portstrees.

# Find out where we're located, and set prefix accordingly
# This is available to the perl code as $ENV{'pb'} so we don't
# need to find it out all over again
pb=$0
[ -z "$(echo "${pb}" | sed 's![^/]!!g')" ] && \
pb=$(type "$pb" | sed 's/^.* //g')
pb=$(realpath $(dirname $pb))
pb=${pb%%/patches}

. ${pb}/scripts/lib/tinderlib.sh

while getopts b:j:p: arg >/dev/null 2>&1
do
	case "${arg}" in
		b)	build="${OPTARG}";
			type="build";;
		j)	jail="${OPTARG}";
			type="jail";;
		p)	portstree="${OPTARG}";
			type="portstree";;
		?)	exit 1;;
	esac
done

if [ -z "${type}" ]; then
	echo "applyPatches: no type specified"
	echo "usage: applyPatches < [-b build] | [-j jail] | [-p portstree] >"
	exit 1
fi

patchDir=${pb}/patches

patchBuild() {
	_build=$1
	buildLoc=$(tinderLoc buildroot $_build)
	patchDirGlobal=${patchDir}/builds/GLOBAL
	patchDirBuild=${patchDir}/builds/${_build}
	(cd ${buildLoc};
	 patches=$(find ${patchDirGlobal} ${patchDirBuild} -type f -print0)
	 for _p in $patches; do
		 patch < $_p
	 done
	 )
}

patchJail() {
	_jail=$1
	jailSrcLoc=$(tinderLoc jailsrc $_jail)
	patchDirGlobal=${patchDir}/jails/GLOBAL
	patchDirJail=${patchDir}/jails/${_jail}
	(cd ${jailSrcLoc};
	 patches=$(find ${patchDirGlobal} ${patchDirJail} -type f -print0)
	 for _p in $patches; do
		 patch < $_p
	 done
	 )

}

patchPortsTree() {
	_portsTree=$1
	portsTreeLoc=$(tinderLoc portstree $_portsTree)/ports
	patchDirGlobal=${patchDir}/portstrees/GLOBAL
	patchDirPortsTree=${patchDir}/portstrees/${_portsTree}
	(cd ${portsTreeLoc};
	 patches=$(find ${patchDirGlobal} ${patchDirPortsTree} -type f -print0)
	 for _p in $patches; do
		 patch < $_p
	 done
	 )
}

if [ "${type}" = "build" ]; then
	echo "patching build: ${build}"
	patchBuild "${build}"

elif [ "${type}" = "jail" ]; then
	echo "patching jail: ${jail}"
	patchJail "${jail}"

elif [ "${type}" = "portstree" ]; then
	echo "patching portstree: ${portstree}"
	patchPortsTree "${portstree}"
fi
