Lucio Andrés Illanes Albornoz 8a27f9
#
Lucio Andrés Illanes Albornoz 8a27f9
# set +o errexit -o noglob is assumed.
Lucio Andrés Illanes Albornoz 8a27f9
#
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_defaults() {
Lucio Andrés Illanes Albornoz 8a27f9
	: ${ARCH:="nt64"}; : ${BUILD:="debug"}; : ${PKG_NAME:=""};
Lucio Andrés Illanes Albornoz 8a27f9
	: ${BUILD_WORKDIR:=""}; : ${PREFIX=""};
Lucio Andrés Illanes Albornoz 8a27f9
	ARG_RESTART_AT=""; ARG_UPDATE_DIFF=0;
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_env() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _fname="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	if ! umask 022; then
Lucio Andrés Illanes Albornoz 8a27f9
		printf "Error: failed to setup environment.\n"; exit 1;
Lucio Andrés Illanes Albornoz 8a27f9
	else	for _fname in $(find subr -name *.subr); do
Lucio Andrés Illanes Albornoz 8a27f9
			if ! . "${_fname}"; then
Lucio Andrés Illanes Albornoz 8a27f9
				printf "Error: failed to source \`%s'.\n" "${_fname}"; exit 1;
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz 8a27f9
		done; fi;
Lucio Andrés Illanes Albornoz 8a27f9
	return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_getopts() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _opt="" _shiftfl=0 _rc=0 OPTIND=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz 8a27f9
		case "${1}" in
Lucio Andrés Illanes Albornoz 8a27f9
		--update-diff)
Lucio Andrés Illanes Albornoz 8a27f9
			ARG_UPDATE_DIFF=1; _shiftfl=1; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		--restart-at)
Lucio Andrés Illanes Albornoz 8a27f9
			if [ "${#}" -lt 2 ]\
Lucio Andrés Illanes Albornoz 8a27f9
			|| [ -z "${2}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_rc=1; _status="Error: missing argument for option --restart-at.";
Lucio Andrés Illanes Albornoz 8a27f9
			else
Lucio Andrés Illanes Albornoz 8a27f9
				ARG_RESTART_AT="${2}"; _shiftfl=2;
Lucio Andrés Illanes Albornoz 8a27f9
			fi; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		*)	_shiftfl=0; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		esac;
Lucio Andrés Illanes Albornoz 8a27f9
		if [ "${_rc:-0}" -ne 0 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
			break;
Lucio Andrés Illanes Albornoz 8a27f9
		elif [ "${_shiftfl:-0}" -gt 0 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
			shift "${_shiftfl}"; continue;
Lucio Andrés Illanes Albornoz 8a27f9
		elif getopts a:b:C:D:Fhp:Pr:R _opt; then
Lucio Andrés Illanes Albornoz 8a27f9
			case "${_opt}" in
Lucio Andrés Illanes Albornoz 8a27f9
			a)	ARCH="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			b)	BUILD="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			h)	pkgtoolp_usage; exit 0; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			*)	pkgtoolp_usage; exit 1; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			esac; shift $((${OPTIND}-1)); OPTIND=1;
Lucio Andrés Illanes Albornoz 8a27f9
		else
Lucio Andrés Illanes Albornoz 8a27f9
			break;
Lucio Andrés Illanes Albornoz 8a27f9
		fi;
Lucio Andrés Illanes Albornoz 8a27f9
	done;
Lucio Andrés Illanes Albornoz 8a27f9
	if [ "${_rc}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
		while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz 8a27f9
		case "${1}" in
Lucio Andrés Illanes Albornoz 8a27f9
		*=*)	rtl_set_var_unsafe "${1%%=*}" "${1#*=}"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		*)	if [ "${#}" -ne 1 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_rc=1; _status="Error: invalid argument \`${1}'.";
Lucio Andrés Illanes Albornoz 8a27f9
			fi; break; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		esac; shift; done;
Lucio Andrés Illanes Albornoz 8a27f9
		if [ "${_rc:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
			if [ "${#}" -ne 1 ]\
Lucio Andrés Illanes Albornoz 8a27f9
			&& [ -z "${PKG_NAME}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				_rc=1; _status="Error: missing package name.";
Lucio Andrés Illanes Albornoz 8a27f9
			elif [ "${#}" -eq 1 ]; then
Lucio Andrés Illanes Albornoz 8a27f9
				PKG_NAME="${1}";
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz 8a27f9
		fi;
Lucio Andrés Illanes Albornoz 8a27f9
	fi;
Lucio Andrés Illanes Albornoz 8a27f9
	return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_prereqs() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _cmd="" _cmds_missing="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	for _cmd in	\
Lucio Andrés Illanes Albornoz 8a27f9
			awk bunzip2 cat chmod cmake cp date find flock g++	\
Lucio Andrés Illanes Albornoz 8a27f9
			gcc git grep gunzip gzip hostname install kill		\
Lucio Andrés Illanes Albornoz 8a27f9
			ln lzip make mkdir mkfifo mv paste patch perl		\
Lucio Andrés Illanes Albornoz 8a27f9
			pgrep pkill printf readlink rm sed seq sha256sum	\
Lucio Andrés Illanes Albornoz 8a27f9
			sort stat tail tar test touch tr wget xz zip; do
Lucio Andrés Illanes Albornoz 8a27f9
		if ! which "${_cmd}" >/dev/null 2>&1; then
Lucio Andrés Illanes Albornoz 8a27f9
			_cmds_missing="${_cmds_missing:+${_cmds_missing} }${_cmd}";
Lucio Andrés Illanes Albornoz 8a27f9
		fi;
Lucio Andrés Illanes Albornoz 8a27f9
	done;
Lucio Andrés Illanes Albornoz 8a27f9
	if [ -n "${_cmds_missing}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="Error: missing prerequisite package(s): ${_cmds_missing}";
Lucio Andrés Illanes Albornoz 8a27f9
	elif ! awk -V 2>/dev/null | grep -q "^GNU Awk "; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="Error: awk(1) in \$PATH must be GNU Awk.";
Lucio Andrés Illanes Albornoz 8a27f9
	elif ! sed --version 2>/dev/null | grep -q "^GNU sed "; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="Error: sed(1) in \$PATH must be GNU sed.";
Lucio Andrés Illanes Albornoz 8a27f9
	fi;
Lucio Andrés Illanes Albornoz 8a27f9
	return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_vars() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _rc=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	if ! rtl_lmatch "${ARCH}" "nt32 nt64"; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="Error: invalid architecture \`${ARCH}'.";
Lucio Andrés Illanes Albornoz 8a27f9
	elif ! rtl_lmatch "${BUILD}" "debug release"; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc=1; _status="Error: unknown build type \`${BUILD}'.";
Lucio Andrés Illanes Albornoz 8a27f9
	else	case "${ARCH}" in
Lucio Andrés Illanes Albornoz 8a27f9
		nt32)	DEFAULT_TARGET="i686-nt32-midipix"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		nt64)	DEFAULT_TARGET="x86_64-nt64-midipix"; ;;
Lucio Andrés Illanes Albornoz 8a27f9
		esac;
Lucio Andrés Illanes Albornoz 8a27f9
		rtl_fileop source_opt							\
Lucio Andrés Illanes Albornoz 8a27f9
			"${HOME}/midipix_build.vars" "${HOME}/.midipix_build.vars"	\
Lucio Andrés Illanes Albornoz 8a27f9
			../midipix_build.vars ./midipix.env;
Lucio Andrés Illanes Albornoz 8a27f9
		if [ -z "${PREFIX}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
			_rc=1; _status="Error: \${PREFIX} empty or unset.";
Lucio Andrés Illanes Albornoz 8a27f9
		fi; fi;
Lucio Andrés Illanes Albornoz 8a27f9
	return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_usage() {
Lucio Andrés Illanes Albornoz 8a27f9
	echo "usage: ./pkgtool.sh [-a nt32|nt64] [-b debug|release] name" >&2;
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
pkgtool_init() {
Lucio Andrés Illanes Albornoz 8a27f9
	local _fname="" _rc=0 _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	if ! pkgtoolp_init_env			\
Lucio Andrés Illanes Albornoz 8a27f9
	|| ! pkgtoolp_init_defaults		\
Lucio Andrés Illanes Albornoz 8a27f9
	|| ! pkgtoolp_init_getopts "${@}"	\
Lucio Andrés Illanes Albornoz 8a27f9
	|| ! pkgtoolp_init_prereqs		\
Lucio Andrés Illanes Albornoz 8a27f9
	|| ! pkgtoolp_init_vars; then
Lucio Andrés Illanes Albornoz 8a27f9
		_rc="${?}"; rtl_log_msg fail "${_status}"; exit "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
	elif [ -n "${_status}" ]; then
Lucio Andrés Illanes Albornoz 8a27f9
		rtl_log_msg info "${_status}"; exit 0;
Lucio Andrés Illanes Albornoz 8a27f9
	else
Lucio Andrés Illanes Albornoz 8a27f9
		return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
	fi;
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
# vim:filetype=sh