# $NetBSD: post-build-conf,v 1.14 2007/03/06 10:57:11 rillig Exp $
#

# This file is included after the build.conf file by the "build" and
# "pre-build" scripts. It provides functions for printing, checking and
# exporting the configuration variables.

# Note: All functions whose names start with "pbc_" are considered private
# to this file. The "pbc" prefix is an abbreviation for "post-build-conf".

# usage: pbc_showvar varname
pbc_showvar() {
	eval "pbc_isset=\${$1+set}"
	case $pbc_isset in
	"set")	eval "fnv_val=\${$1-}"
		printf "   %-25s = %s\\n" "$1" "${fnv_val}"
		;;
	*)	printf "   %-25s (undefined)\\n" "$1"
		;;
	esac
}

# usage: pbc_section title varname...
pbc_section() {
	printf "%s\\n" "$1"
	shift
	for pbc_var in "$@"; do
		pbc_showvar "${pbc_var}"
	done
	printf "\\n"
}

# usage: show_config_vars
show_config_vars() {
	pbc_section "System information" \
		osrev BULK_BUILD_CONF USR_PKGSRC MAKECONF
	pbc_section "Getting distfiles" \
		PRUNEDISTFILES ftp_proxy http_proxy
	pbc_section "Building the packages" \
		PKGLIST NICE_LEVEL
	pbc_section "Generating the report" \
		ADMIN ADMINSIG \
		REPORTS_URL REPORTS_DIR \
		REPORT_BASEDIR REPORT_HTML_FILE REPORT_TXT_FILE
	pbc_section "Uploading binary packages" \
		UPDATE_VULNERABILITY_LIST PRUNEPACKAGES MKSUMS SIGN_AS \
		RSYNC_DST RSYNC_OPTS
}

# usage: export_config_vars
export_config_vars() {
	export osrev BULK_BUILD_CONF USR_PKGSRC
	export PRUNEDISTFILES ftp_proxy http_proxy
	export PKGLIST NICE_LEVEL ADMIN ADMINSIG
	export UPDATE_VULNERABILITY_LIST PRUNEPACKAGES MKSUMS SIGN_AS
	export RSYNC_DST RSYNC_OPTS
	case ${MAKECONF+set} in
	"set")	export MAKECONF;;
	esac
}

# usage: pbc_die error-message
pbc_die() {
	exec 1>&2
	printf "error: %s\\n" "$@"
	printf "       Please check your bulk build configuration file:\\n"
	case ${BULK_BUILD_CONF+set} in
	"set")	printf "       %s\\n" "${BULK_BUILD_CONF}"
	esac
	exit 1
}

# usage: pbc_checkyesno varname
pbc_checkyesno() {
	eval "pbc_val=\${$1-}"
	case $pbc_val in
	[Yy][Ee][Ss]|[Nn][Oo]) ;;
	*)	pbc_die "$1 must be set to one of YES, yes, NO, no.";;
	esac
}

# usage: pbc_checkpathname varname
pbc_checkpathname() {
	eval "pbc_val=\${$1-}"

	case $pbc_val in
	/*)	;;
	*)	pbc_die "$1 must be an absolute pathname.";;
	esac
}

# usage: pbc_checkbasename varname
pbc_checkbasename() {
	eval "pbc_val=\${$1-}"

	case $pbc_val in
	*/*)	pbc_die "$1 must not contain slashes.";;
	esac
}

# usage: pbc_checkexistingfile varname
pbc_checkexistingfile() {

	pbc_checkpathname "$1"

	eval "pbc_val=\${$1-}"

	test -f "${pbc_val}" \
	|| pbc_die "$1 must be the name of an existing file."
}

# usage: pbc_checkexistingdir varname
pbc_checkexistingdir() {

	pbc_checkpathname "$1"

	eval "pbc_val=\${$1-}"

	test -d "${pbc_val}" \
	|| pbc_die "$1 must be the name of an existing directory."
}

# usage: pbc_checkurl varname
pbc_checkurl() {
	eval "pbc_val=\${$1-}"

	case $pbc_val in
	file:///*|ftp://*|http://*)
		;;
	*)	pbc_die "$1 is not a valid URL.";;
	esac
}

# usage: pbc_checknonempty varname
pbc_checknonempty() {
	eval "pbc_isset=\${$1+set}"
	eval "pbc_val=\${$1-}"

	case $pbc_isset in
	"set")	case $pbc_val in
		"")	pbc_die "$1 must be non-empty.";;
		esac;;
	*)	pbc_die "$1 must be defined and non-empty.";;
	esac
}

# usage: pbc_checkdefined varname
pbc_checkdefined() {
	eval "pbc_val=\${$1+set}"

	case $pbc_val in
	"set")	;;
	*)	pbc_die "$1 must be defined.";;
	esac
}

# usage: check_config_vars
check_config_vars() {

	case ${FTP+set},${REPORTS_DIR+set} in
	set,set)
		pbc_die "FTP and REPORTS_DIR must not be set both.";;
	esac

	# section "System information"
	pbc_checknonempty osrev
	pbc_checkexistingfile BULK_BUILD_CONF
	pbc_checkexistingdir USR_PKGSRC
	case ${MAKECONF+set} in
	"set")	pbc_checkexistingfile MAKECONF;;
	esac

	# section "Getting distfiles"
	case ${PRUNEDISTFILES+set} in
	"set")	pbc_checkyesno PRUNEDISTFILES
	esac
	# no checks for ftp_proxy
	# no checks for http_proxy

	# section "Building the packages"
	# no checks for PKGLIST
	# no checks for NICE_LEVEL

	# section "Generating the report"
	# no checks for ADMIN
	# no checks for ADMINSIG
	pbc_checkurl REPORTS_URL
	pbc_checkpathname REPORTS_DIR
	pbc_checkbasename REPORT_BASEDIR
	pbc_checkbasename REPORT_HTML_FILE
	pbc_checkbasename REPORT_TXT_FILE


	# section "Uploading binary packages"
	pbc_checkyesno UPDATE_VULNERABILITY_LIST
	case ${PRUNEPACKAGES+set} in
	"set")	pbc_checkyesno PRUNEPACKAGES
	esac
	pbc_checkyesno MKSUMS
	# no checks for SIGN_AS
	# no checks for RSYNC_DST
	# no checks for RSYNC_OPTS
}