summaryrefslogtreecommitdiffhomepage
path: root/subr/pkgtool_init.subr
blob: b33795ee97d73bfb763ddb07ffe475f0b0488dad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
# 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, -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, -r, -s, or -t must be specified.";
	else	_rc=0; export TMP="${BUILD_WORKDIR}" TMPDIR="${BUILD_WORKDIR}";
	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.rtl -name *.subr) $(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_KIND:="debug"};
	ARG_INFO=0; ARG_MIRROR=0; ARG_MIRROR_DNAME=""; 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:him:rst _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}"; ;;
			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	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;
		fi;
	fi; return "${_rc}";
};

pkgtoolp_init_getopts_help() {
	local _opt="";
	while getopts a:b:hirst _opt 2>/dev/null; do
	case "${_opt}" in
	h)	cat etc/pkgtool.usage; 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