Blob Blame History Raw
#
# set +o errexit -o noglob -o nounset is assumed.
#

pkgtoolp_init_args() {
	local _rc=0; _status="";
	if [ "$((${ARG_INFO:-0} + ${ARG_MIRROR:-0} + ${ARG_RDEPENDS:-0} + ${ARG_SHELL:-0} + ${ARG_TARBALL:-0}))" -gt 1 ]; then
		cat etc/pkgtool.usage; _rc=1; _status="Error: only one of -i, -m and/or -M, -r, -s, or -t must be specified.";
	elif [ "$((${ARG_INFO:-0} + ${ARG_MIRROR:-0} + ${ARG_RDEPENDS:-0} + ${ARG_SHELL:-0} + ${ARG_TARBALL:-0}))" -eq 0 ]\
	&&   [ -z "${ARG_RESTART_AT}" ]\
	&&   [ "${ARG_UPDATE_DIFF:-0}" -eq 0 ]; then
		cat etc/pkgtool.usage; _rc=1; _status="Error: one of -i, -m and/or -M, -r, -s, or -t must be specified.";
	elif [ "${BUILD_HNAME:+1}" != 1 ]\
	&& ! BUILD_HNAME="$(hostname)"; then
		_rc=1; _status="failed to obtain hostname.";
	else	_rc=0; export TMP="${BUILD_WORKDIR}" TMPDIR="${BUILD_WORKDIR}";
	fi; return "${_rc}";
};

pkgtoolp_init_env() {
	local _fname="" _lang="${LANG:-C}" _rc=0; _status=""; _lang="${_lang%%_*}";

	if ! cd "${0%/*}"; then
		printf "Error: failed to change working directory to \`${0%/*}'." >&2; exit 1;
	elif ! umask 022; then
		printf "Error: failed to set umask(2).\n" >&2; exit 1;
	else	for _fname in	\
				$(find subr.rtl -name *.subr)		\
				$(find subr -name *.subr)		\
				etc/pkgtool.theme			\
				"etc/pkgtool.msgs.${_lang}"		\
				"etc/rtl.msgs.${_lang}"			\
				;
		do
			if ! . "${_fname}"; then
				printf "Error: failed to source \`%s'.\n" "${_fname}" >&2; exit 1;
			fi;
		done;
		for _fname in	\
				"etc/pkgtool.theme.local"		\
				"etc/pkgtool.msgs.${_lang}.local"	\
				"etc/rtl.msgs.${_lang}.local"		\
				;
		do
			if [ -e "${_fname}" ]; then
				printf "Error: failed to source \`%s'.\n" "${_fname}" >&2; exit 1;
			fi;
		done;
	fi; export LANG=C LC_ALL=C; return "${_rc}";
};

pkgtoolp_init_getopts() {
	local _opt="" _shiftfl=0 _rc=0 OPTIND=0; _status=""; : ${ARCH:="nt64"}; : ${BUILD_KIND:="debug"};
	ARG_INFO=0; ARG_MIRROR=0; ARG_MIRROR_DNAME=""; ARG_MIRROR_DNAME_GIT=""; ARG_RESTART_AT="";
	ARG_RDEPENDS=0; ARG_UPDATE_DIFF=0; ARG_SHELL=0; ARG_TARBALL=0; ARG_VERBOSE=0;
	while [ "${#}" -gt 0 ]; do
		case "${1}" in
		--update-diff)
			ARG_UPDATE_DIFF=1; _shiftfl=1; ;;
		--restart-at)
			if [ "${#}" -lt 2 ]\
			|| [ -z "${2}" ]; then
				_rc=1; _status="Error: missing argument for option --restart-at.";
			else
				ARG_RESTART_AT="${2}"; _shiftfl=2;
			fi; ;;
		*)	_shiftfl=0; ;;
		esac;
		if [ "${_rc:-0}" -ne 0 ]; then
			break;
		elif [ "${_shiftfl:-0}" -gt 0 ]; then
			shift "${_shiftfl}"; continue;
		elif getopts a:b:him:M:rstv _opt; then
			case "${_opt}" in
			a)	ARCH="${OPTARG}"; ;;
			b)	BUILD_KIND="${OPTARG}"; ;;
			h)	cat etc/pkgtool.usage; exit 0; ;;
			i)	ARG_INFO=1; ;;
			m)	ARG_MIRROR=1; ARG_MIRROR_DNAME="${OPTARG}"; ;;
			M)	ARG_MIRROR=1; ARG_MIRROR_DNAME_GIT="${OPTARG}"; ;;
			r)	ARG_RDEPENDS=1; ;;
			s)	ARG_SHELL=1; ;;
			t)	ARG_TARBALL=1; ;;
			v)	ARG_VERBOSE=1; ;;
			*)	cat etc/pkgtool.usage; exit 1; ;;
			esac; shift $((${OPTIND}-1)); OPTIND=1;
		else	case "${1}" in
			*=*)	rtl_set_var_unsafe "${1%%=*}" "${1#*=}"; ;;
			*)	PKGTOOL_PKG_NAME="${1}"; ;;
			esac; shift;
		fi;
	done;
	if [ "${_rc}" -eq 0 ]; then
		if [ -z "${PKGTOOL_PKG_NAME:-}" ]\
		&& [ "${ARG_MIRROR:-0}" -eq 0 ]; then
			_rc=1; _status="Error: missing package name.";
		else	export PKGTOOL_PKG_NAME;
			case "${ARG_VERBOSE:-0}" in
			0)	rtl_log_enable_tags "${LOG_TAGS_normal}"; ;;
			1)	rtl_log_enable_tags "${LOG_TAGS_verbose}"; ;;
			*)	_rc=1; _status="Error: invalid verbosity level (max. -v)"; ;;
			esac;
		fi;
	fi; return "${_rc}";
};

pkgtoolp_init_getopts_help() {
	local _opt="";
	while getopts a:b:hirst _opt 2>/dev/null; do
	case "${_opt}" in
	h)
		if [ -t 1 ]; then
			cat etc/pkgtool.usage;
		else
			sed 's/\[[0-9]\+m//g' etc/pkgtool.usage;
		fi; exit 0; ;;
	esac; done; shift $((${OPTIND}-1));
};

pkgtoolp_init_prereqs() {
	if ! rtl_check_prereqs \
			awk bzip2 cat chmod cp date find grep hostname mkdir	\
			mktemp mv paste printf readlink rm sed sort tar test	\
			touch tr uniq; then
		printf "%s\n" "${_status}" >&2; exit 1;
	elif ! (FNAME="$(mktemp)" && { trap "rm -f \"\${FNAME}\"" EXIT;		\
			sed -i'' -e '' "${FNAME}" >/dev/null 2>&1; }); then
		printf "Error: sed(1) in \${PATH} does not support the \`-i' option.\n" >&2; exit 1;
	fi;
};

pkgtool_init() {
	local _fname="" _rc=0; _status="";
	if ! pkgtoolp_init_env			\
	|| ! pkgtoolp_init_getopts_help "${@}"	\
	|| ! pkgtoolp_init_prereqs		\
	|| ! pkgtoolp_init_getopts "${@}"	\
	|| ! ex_pkg_load_vars			\
	|| ! pkgtoolp_init_args; then
		_rc=1; _status="${_status}";
	fi; return "${_rc}";
};

# vim:filetype=sh