Blame subr/pkgtool_init.subr

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 b6a9a1
	if [ "$((${ARG_INFO:-0} + ${ARG_RDEPENDS:-0} + ${ARG_SHELL:-0} + ${ARG_TARBALL:-0}))" -gt 1 ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		cat etc/pkgtool.usage; _rc=1; _status="Error: only one of -i, -r, -s, or -t must be specified.";
Lucio Andrés Illanes Albornoz b6a9a1
	elif [ "$((${ARG_INFO:-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 b6a9a1
		cat etc/pkgtool.usage; _rc=1; _status="Error: one of -i, -r, -s, or -t must be specified.";
Lucio Andrés Illanes Albornoz b6a9a1
	else	_rc=0;
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 8a27f9
	local _fname="" _rc=0; _status="";
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 8a27f9
	else	for _fname in $(find subr -name *.subr); 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 b6a9a1
	fi; 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 b6a9a1
	local _opt="" _shiftfl=0 _rc=0 OPTIND=0; _status=""; : ${ARCH:="nt64"}; : ${BUILD:="debug"};
Lucio Andrés Illanes Albornoz b6a9a1
	ARG_INFO=0; ARG_RESTART_AT=""; ARG_RDEPENDS=0; ARG_UPDATE_DIFF=0; ARG_SHELL=0; ARG_TARBALL=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 60fba6
		elif getopts a:b:hirst _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 60fba6
			h)	cat etc/pkgtool.usage; exit 0; ;;
Lucio Andrés Illanes Albornoz 60fba6
			i)	ARG_INFO=1; ;;
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 60fba6
			*)	cat etc/pkgtool.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 b6a9a1
			case "${1}" in
Lucio Andrés Illanes Albornoz b6a9a1
			*=*)	rtl_set_var_unsafe "${1%%=*}" "${1#*=}"; ;;
Lucio Andrés Illanes Albornoz b6a9a1
			*)	if [ "${#}" -ne 1 ]; then
Lucio Andrés Illanes Albornoz b6a9a1
					_rc=1; _status="Error: invalid argument \`${1}'.";
Lucio Andrés Illanes Albornoz b6a9a1
				fi; break; ;;
Lucio Andrés Illanes Albornoz b6a9a1
			esac; shift;
Lucio Andrés Illanes Albornoz b6a9a1
		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 11ef38
			&& [ -z "${PKGTOOL_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 b6a9a1
				PKGTOOL_PKG_NAME="${1}"; export PKGTOOL_PKG_NAME;
Lucio Andrés Illanes Albornoz 8a27f9
			fi;
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 151e05
	h)	cat etc/pkgtool.usage; 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