summaryrefslogtreecommitdiffhomepage
path: root/subr/pkgtool_init.subr
blob: 9d77d8d94ff35b56563e2502a6322826ca8cc5ee (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#
# 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}" _name="" _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			\
				;
		do
			if ! . "${_fname}"; then
				printf "Error: failed to source \`%s'.\n" "${_fname}" >&2; exit 1;
			fi;
		done;
		if [ -e "etc/pkgtool.theme.local" ]; then
			if ! . "etc/pkgtool.theme.local"; then
				printf "Error: failed to source \`%s'.\n" "etc/pkgtool.theme.local" >&2; exit 1;
			fi;
		fi;
		for _name in pkgtool rtl; do
			for _lang_ in ${_lang} C; do
				_fname="etc/${_name}.msgs.${_lang_}";
				if [ -e "${_fname}" ]; then
					if ! . "${_fname}"; then
						printf "Error: failed to source \`%s'.\n" "${_fname}" >&2; exit 1;
					fi;
					if [ -e "${_fname}.local" ]; then
						if ! . "${_fname}.local"; then
							printf "Error: failed to source \`%s'.\n" "${_fname}.local" >&2; exit 1;
						fi;
					fi; break;
				fi;
			done;
		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