midipix / build / midipix_build

Forked from build/midipix_build 4 years ago
Clone
Blob Blame History Raw
#
# set +o errexit -o noglob is assumed.
#

pkgtoolp_init_args() {
	local _rc=0; _status="";
	if [ "$((${ARG_INFO:-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, -r, -s, or -t must be specified.";
	elif [ "$((${ARG_INFO:-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, -r, -s, or -t must be specified.";
	else	_rc=0;
	fi; return "${_rc}";
};

pkgtoolp_init_env() {
	local _fname="" _rc=0; _status="";
	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 -name *.subr); do
			if ! . "${_fname}"; then
				printf "Error: failed to source \`%s'.\n" "${_fname}" >&2; exit 1;
			fi;
		done;
	fi; return "${_rc}";
};

pkgtoolp_init_getopts() {
	local _opt="" _shiftfl=0 _rc=0 OPTIND=0; _status=""; : ${ARCH:="nt64"}; : ${BUILD:="debug"};
	ARG_INFO=0; ARG_RESTART_AT=""; ARG_RDEPENDS=0; ARG_UPDATE_DIFF=0; ARG_SHELL=0; ARG_TARBALL=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:hirst _opt; then
			case "${_opt}" in
			a)	ARCH="${OPTARG}"; ;;
			b)	BUILD="${OPTARG}"; ;;
			h)	cat etc/pkgtool.usage; exit 0; ;;
			i)	ARG_INFO=1; ;;
			r)	ARG_RDEPENDS=1; ;;
			s)	ARG_SHELL=1; ;;
			t)	ARG_TARBALL=1; ;;
			*)	cat etc/pkgtool.usage; exit 1; ;;
			esac; shift $((${OPTIND}-1)); OPTIND=1;
		else
			break;
		fi;
	done;
	if [ "${_rc}" -eq 0 ]; then
		while [ "${#}" -gt 0 ]; do
			case "${1}" in
			*=*)	rtl_set_var_unsafe "${1%%=*}" "${1#*=}"; ;;
			*)	if [ "${#}" -ne 1 ]; then
					_rc=1; _status="Error: invalid argument \`${1}'.";
				fi; break; ;;
			esac; shift;
		done;
		if [ "${_rc:-0}" -eq 0 ]; then
			if [ "${#}" -ne 1 ]\
			&& [ -z "${PKGTOOL_PKG_NAME}" ]; then
				_rc=1; _status="Error: missing package name.";
			elif [ "${#}" -eq 1 ]; then
				PKGTOOL_PKG_NAME="${1}"; export PKGTOOL_PKG_NAME;
			fi;
		fi;
	fi; return "${_rc}";
};

pkgtoolp_init_prereqs() {
	local _cmd="" _cmds_missing="" _rc=0; _status="";
	for _cmd in	\
			awk bzip2 cat chmod cp date find grep hostname mkdir	\
			mktemp mv paste printf readlink rm sed sort tar test	\
			touch tr uniq; do
		if ! which "${_cmd}" >/dev/null 2>&1; then
			_cmds_missing="${_cmds_missing:+${_cmds_missing} }${_cmd}";
		fi;
	done;
	if [ -n "${_cmds_missing}" ]; then
		_rc=1; _status="Error: missing prerequisite package(s): ${_cmds_missing}";
	elif ! (FNAME="$(mktemp)" && { trap "rm -f \"\${FNAME}\"" EXIT;		\
			sed -i'' -e '' "${FNAME}" >/dev/null 2>&1; }); then
		_rc=1; _status="Error: sed(1) in \${PATH} does not support the \`-i' option.";
	fi; return "${_rc}";
};

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

# vim:filetype=sh