Lucio Andrés Illanes Albornoz 8a27f9
#
Lucio Andrés Illanes Albornoz e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz 8a27f9
#
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz b6a9a1
pkgtoolp_init_args() {
Lucio Andrés Illanes Albornoz 60fba6
	local _rc=0; _status="";
Lucio Andrés Illanes Albornoz b63b9b
	if [ "$((${ARG_INFO:-0} + ${ARG_MIRROR:-0} + ${ARG_RDEPENDS:-0} + ${ARG_SHELL:-0} + ${ARG_TARBALL:-0}))" -gt 1 ]; then
Lucio Andrés Illanes Albornoz 22d139
		cat etc/pkgtool.usage; _rc=1; _status="Error: only one of -i, -m and/or -M, -r, -s, or -t must be specified.";
Lucio Andrés Illanes Albornoz b63b9b
	elif [ "$((${ARG_INFO:-0} + ${ARG_MIRROR:-0} + ${ARG_RDEPENDS:-0} + ${ARG_SHELL:-0} + ${ARG_TARBALL:-0}))" -eq 0 ]\
Lucio Andrés Illanes Albornoz b6a9a1
	&&   [ -z "${ARG_RESTART_AT}" ]\
Lucio Andrés Illanes Albornoz b6a9a1
	&&   [ "${ARG_UPDATE_DIFF:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 22d139
		cat etc/pkgtool.usage; _rc=1; _status="Error: one of -i, -m and/or -M, -r, -s, or -t must be specified.";
Lucio Andrés Illanes Albornoz b1d432
	elif [ "${BUILD_HNAME:+1}" != 1 ]\
Lucio Andrés Illanes Albornoz b1d432
	&& ! BUILD_HNAME="$(hostname)"; then
Lucio Andrés Illanes Albornoz b1d432
		_rc=1; _status="failed to obtain hostname.";
Lucio Andrés Illanes Albornoz 98c928
	else	_rc=0; export TMP="${BUILD_WORKDIR}" TMPDIR="${BUILD_WORKDIR}";
Lucio Andrés Illanes Albornoz 60fba6
	fi; return "${_rc}";
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 2c3589
	local _fname="" _lang="${LANG:-C}" _name="" _rc=0; _status=""; _lang="${_lang%%_*}";
Lucio Andrés Illanes Albornoz e493dc
Lucio Andrés Illanes Albornoz b6a9a1
	if ! cd "${0%/*}"; then
Lucio Andrés Illanes Albornoz b6a9a1
		printf "Error: failed to change working directory to \`${0%/*}'." >&2; exit 1;
Lucio Andrés Illanes Albornoz b6a9a1
	elif ! umask 022; then
Lucio Andrés Illanes Albornoz b6a9a1
		printf "Error: failed to set umask(2).\n" >&2; exit 1;
Lucio Andrés Illanes Albornoz b1d432
	else	for _fname in	\
Lucio Andrés Illanes Albornoz e493dc
				$(find subr.rtl -name *.subr)		\
Lucio Andrés Illanes Albornoz e493dc
				$(find subr -name *.subr)		\
Lucio Andrés Illanes Albornoz e493dc
				etc/pkgtool.theme			\
Lucio Andrés Illanes Albornoz b1d432
				;
Lucio Andrés Illanes Albornoz b1d432
		do
Lucio Andrés Illanes Albornoz 8a27f9
			if ! . "${_fname}"; then
Lucio Andrés Illanes Albornoz b6a9a1
				printf "Error: failed to source \`%s'.\n" "${_fname}" >&2; exit 1;
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
Lucio Andrés Illanes Albornoz b6a9a1
		done;
Lucio Andrés Illanes Albornoz 2c3589
		if [ -e "etc/pkgtool.theme.local" ]; then
Lucio Andrés Illanes Albornoz 2c3589
			if ! . "etc/pkgtool.theme.local"; then
Lucio Andrés Illanes Albornoz 2c3589
				printf "Error: failed to source \`%s'.\n" "etc/pkgtool.theme.local" >&2; exit 1;
Lucio Andrés Illanes Albornoz ff862f
			fi;
Lucio Andrés Illanes Albornoz 2c3589
		fi;
Lucio Andrés Illanes Albornoz 2c3589
		for _name in pkgtool rtl; do
Lucio Andrés Illanes Albornoz 2c3589
			for _lang_ in ${_lang} C; do
Lucio Andrés Illanes Albornoz 2c3589
				_fname="etc/${_name}.msgs.${_lang_}";
Lucio Andrés Illanes Albornoz 2c3589
				if [ -e "${_fname}" ]; then
Lucio Andrés Illanes Albornoz 2c3589
					if ! . "${_fname}"; then
Lucio Andrés Illanes Albornoz 2c3589
						printf "Error: failed to source \`%s'.\n" "${_fname}" >&2; exit 1;
Lucio Andrés Illanes Albornoz 2c3589
					fi;
Lucio Andrés Illanes Albornoz 2c3589
					if [ -e "${_fname}.local" ]; then
Lucio Andrés Illanes Albornoz 2c3589
						if ! . "${_fname}.local"; then
Lucio Andrés Illanes Albornoz 2c3589
							printf "Error: failed to source \`%s'.\n" "${_fname}.local" >&2; exit 1;
Lucio Andrés Illanes Albornoz 2c3589
						fi;
Lucio Andrés Illanes Albornoz 2c3589
					fi; break;
Lucio Andrés Illanes Albornoz 2c3589
				fi;
Lucio Andrés Illanes Albornoz 2c3589
			done;
Lucio Andrés Illanes Albornoz e493dc
		done;
Lucio Andrés Illanes Albornoz 2c3589
Lucio Andrés Illanes Albornoz e493dc
	fi; export LANG=C LC_ALL=C; 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 645bf3
	local _opt="" _shiftfl=0 _rc=0 OPTIND=0; _status=""; : ${ARCH:="nt64"}; : ${BUILD_KIND:="debug"};
Lucio Andrés Illanes Albornoz 5cca78
	ARG_INFO=0; ARG_MIRROR=0; ARG_MIRROR_DNAME=""; ARG_MIRROR_DNAME_GIT=""; ARG_RESTART_AT="";
Lucio Andrés Illanes Albornoz 5cca78
	ARG_RDEPENDS=0; ARG_UPDATE_DIFF=0; ARG_SHELL=0; ARG_TARBALL=0; ARG_VERBOSE=0;
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 5cca78
		elif getopts a:b:him:M:rstv _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 ba50c2
			b)	BUILD_KIND="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 60fba6
			h)	cat etc/pkgtool.usage; exit 0; ;;
Lucio Andrés Illanes Albornoz 60fba6
			i)	ARG_INFO=1; ;;
Lucio Andrés Illanes Albornoz b63b9b
			m)	ARG_MIRROR=1; ARG_MIRROR_DNAME="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 22d139
			M)	ARG_MIRROR=1; ARG_MIRROR_DNAME_GIT="${OPTARG}"; ;;
Lucio Andrés Illanes Albornoz 60fba6
			r)	ARG_RDEPENDS=1; ;;
Lucio Andrés Illanes Albornoz 60fba6
			s)	ARG_SHELL=1; ;;
Lucio Andrés Illanes Albornoz 60fba6
			t)	ARG_TARBALL=1; ;;
Lucio Andrés Illanes Albornoz 5cca78
			v)	ARG_VERBOSE=1; ;;
Lucio Andrés Illanes Albornoz 60fba6
			*)	cat etc/pkgtool.usage; exit 1; ;;
Lucio Andrés Illanes Albornoz 8a27f9
			esac; shift $((${OPTIND}-1)); OPTIND=1;
Lucio Andrés Illanes Albornoz 286c43
		else	case "${1}" in
Lucio Andrés Illanes Albornoz 286c43
			*=*)	rtl_set_var_unsafe "${1%%=*}" "${1#*=}"; ;;
Lucio Andrés Illanes Albornoz 40e5e8
			*)	PKGTOOL_PKG_NAME="${1}"; ;;
Lucio Andrés Illanes Albornoz 286c43
			esac; shift;
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 b63b9b
		if [ -z "${PKGTOOL_PKG_NAME:-}" ]\
Lucio Andrés Illanes Albornoz b63b9b
		&& [ "${ARG_MIRROR:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 286c43
			_rc=1; _status="Error: missing package name.";
Lucio Andrés Illanes Albornoz 286c43
		else	export PKGTOOL_PKG_NAME;
Lucio Andrés Illanes Albornoz 5cca78
			case "${ARG_VERBOSE:-0}" in
Lucio Andrés Illanes Albornoz b1d432
			0)	rtl_log_enable_tags "${LOG_TAGS_normal}"; ;;
Lucio Andrés Illanes Albornoz b1d432
			1)	rtl_log_enable_tags "${LOG_TAGS_verbose}"; ;;
Lucio Andrés Illanes Albornoz 5cca78
			*)	_rc=1; _status="Error: invalid verbosity level (max. -v)"; ;;
Lucio Andrés Illanes Albornoz 5cca78
			esac;
Lucio Andrés Illanes Albornoz 8a27f9
		fi;
Lucio Andrés Illanes Albornoz 60fba6
	fi; return "${_rc}";
Lucio Andrés Illanes Albornoz 60fba6
};
Lucio Andrés Illanes Albornoz 60fba6
Lucio Andrés Illanes Albornoz 11ef38
pkgtoolp_init_getopts_help() {
Lucio Andrés Illanes Albornoz 11ef38
	local _opt="";
Lucio Andrés Illanes Albornoz 11ef38
	while getopts a:b:hirst _opt 2>/dev/null; do
Lucio Andrés Illanes Albornoz 11ef38
	case "${_opt}" in
Lucio Andrés Illanes Albornoz 171651
	h)
Lucio Andrés Illanes Albornoz 171651
		if [ -t 1 ]; then
Lucio Andrés Illanes Albornoz 171651
			cat etc/pkgtool.usage;
Lucio Andrés Illanes Albornoz 171651
		else
Lucio Andrés Illanes Albornoz 171651
			sed 's/?\[[0-9]\+m//g' etc/pkgtool.usage;
Lucio Andrés Illanes Albornoz 171651
		fi; exit 0; ;;
Lucio Andrés Illanes Albornoz 11ef38
	esac; done; shift $((${OPTIND}-1));
Lucio Andrés Illanes Albornoz 11ef38
};
Lucio Andrés Illanes Albornoz 11ef38
Lucio Andrés Illanes Albornoz 8a27f9
pkgtoolp_init_prereqs() {
Lucio Andrés Illanes Albornoz 11ef38
	if ! rtl_check_prereqs \
Lucio Andrés Illanes Albornoz 3e295f
			awk bzip2 cat chmod cp date find grep hostname mkdir	\
Lucio Andrés Illanes Albornoz 3e295f
			mktemp mv paste printf readlink rm sed sort tar test	\
Lucio Andrés Illanes Albornoz 11ef38
			touch tr uniq; then
Lucio Andrés Illanes Albornoz 11ef38
		printf "%s\n" "${_status}" >&2; exit 1;
Lucio Andrés Illanes Albornoz 60fba6
	elif ! (FNAME="$(mktemp)" && { trap "rm -f \"\${FNAME}\"" EXIT;		\
Lucio Andrés Illanes Albornoz 60fba6
			sed -i'' -e '' "${FNAME}" >/dev/null 2>&1; }); then
Lucio Andrés Illanes Albornoz 11ef38
		printf "Error: sed(1) in \${PATH} does not support the \`-i' option.\n" >&2; exit 1;
Lucio Andrés Illanes Albornoz 11ef38
	fi;
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 b6a9a1
	local _fname="" _rc=0; _status="";
Lucio Andrés Illanes Albornoz 8a27f9
	if ! pkgtoolp_init_env			\
Lucio Andrés Illanes Albornoz 11ef38
	|| ! pkgtoolp_init_getopts_help "${@}"	\
Lucio Andrés Illanes Albornoz 8a27f9
	|| ! pkgtoolp_init_prereqs		\
Lucio Andrés Illanes Albornoz 11ef38
	|| ! pkgtoolp_init_getopts "${@}"	\
Lucio Andrés Illanes Albornoz b6a9a1
	|| ! ex_pkg_load_vars			\
Lucio Andrés Illanes Albornoz b6a9a1
	|| ! pkgtoolp_init_args; then
Lucio Andrés Illanes Albornoz b6a9a1
		_rc=1; _status="${_status}";
Lucio Andrés Illanes Albornoz b6a9a1
	fi; return "${_rc}";
Lucio Andrés Illanes Albornoz 8a27f9
};
Lucio Andrés Illanes Albornoz 8a27f9
Lucio Andrés Illanes Albornoz 8a27f9
# vim:filetype=sh