#!/bin/sh
# Copyright (C) 2014 - 2018 FARGOS Development, LLC
# $Id$
# clean failed backing tree builds
originDir=`dirname $0`
ageInDays=1
tooOldInDays=5

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [--mindays n1] [--maxdays n2] [{--btroot dirName} ...]\n" "${0}" >&2
		printf "  Default mindays=%d maxdays=%d\n" ${ageInDays} ${tooOldInDays} >&2
		printf "  Root directories for backing trees are located via BT_ROOT_PATH by default,\n" >&
		printf "    which is currently \"%s\".\n" "${BT_ROOT_PATH:-/opt/backingTrees}" >&2
		printf "  --mindays specifies the number of days a failed backing tree must exist\n" >&2
		printf "    before it can be deleted.  Usually some time is provded to permit\n"
		printf "    examination for problem determination\n" >&2
		printf "  --maxdays specifies the maximum number of days a successful build will\n"
	       	printf "    normally be retained.\n"
		printf "  --btroot can specify alternate root directories\n" >&2
		printf "  Deletes failed builds that are at least mindays old.\n" >&2
		printf "  Deletes builds older than maxdays unless KEEP.TREE is present in directory\n" >&2
		printf "    or a symbolic link references the backing tree,\n" >&2
		printf "    which establishes a permanent well-known name\n" >&2

		exit 1
	--min*)
		ageInDays="${2}"
		shift
		;;
	--max*)
		tooOldInDays="${2}"
		;;
	--btroot)
		btRootPaths="${btRootPaths} ${2}"
		;;
	*)
		printf "%s: unrecognized argument: \"%s\"\n" "$[0}" "${1}" >&2
		exit 1
		;;
	esac
	shift
done

# default to BT_ROOT_PATH
if test -z "${btRootPaths}"
then
	btRootPaths="${BT_ROOT_PATH:-/opt/backingTrees}"
fi

for btRoot in ${btRootPaths}
do
	# Look for BAD.build markers placed in toplevel after failed builds
	failedBuilds=`find -H ${btRoot} -maxdepth 2 -name BAD.build -daystart -mtime +${ageInDays} -print`
	for bld in ${failedBuilds}
	do
		bldDir=`dirname ${bld}`
		if test ! -e ${bldDir}/KEEP.TREE
		then
			echo Removing failed build tree ${bldDir}
			rm -rf ${bldDir}
		fi
	done

	retainList=""
	possibleList=""
	# directories named BuildEnv act as the signature for a backing tree
	possibleBackingTrees=`find -L ${btRoot} -maxdepth 2 -name BuildEnv -daystart -mtime +${tooOldInDays} -print`
	for f in ${possibleBackingTrees}
	do
		bldDir=`dirname ${f}`
		if test -e ${bldDir}/KEEP.TREE
		then
			echo KEEP.TREE present for ${bldDir}
			continue
		fi
			
		if test -d ${bldDir}
		then
			dirName=`basename ${bldDir}`
			case "${dirName}" in
			release_* | beta_*)	# preserve
				printf "%s: force retention of %s due to local convention\n" $0 ${bldDir} >&2
				continue
				;;
			esac
			fullPath=`readlink -f ${bldDir}`
			possibleList="${possibleList}${possibleList:+ }${fullPath}"
		fi
	done

	allDirEntries=`echo ${btRoot}/*/BuildEnv`
	for f in ${allDirEntries}
	do
		bldDir=`dirname ${f}`
		if test -h "${bldDir}"
		then
			target=`readlink -f "${bldDir}"`
			retainList="${retainList}${retainList:+ }${target}"
		fi
	done

	if test -z "${possibleList}"
	then
		printf "%s: no possible obsolete backing trees to delete\n" $0 >&2
		exit 1
	fi

	removeList=`${originDir}/pathedit -r "${possibleList}" ${retainList} | sed -e 's/:/ /g'`
	for bldDir in ${removeList}
	do
		echo Remove obsolete ${bldDir}
		rm -rf ${bldDir}
	done
done
exit 0
