summaryrefslogtreecommitdiffhomepage
path: root/build.subr
blob: e8a9fca125181f830ccd9b774296c0d3b50f0c4a (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
TIMESTAMP_FMT="${TIMESTAMP_FMT:-"%Y/%m/%d %H:%M:%S"}";

date() {
	command date +"${TIMESTAMP_FMT}";
};

fetch() {
	wget --no-check-certificate -N "${@}";
};

fetch_git() {
	local _subdir="${1}" _repo="${2}";
	if [ -d "${_subdir}" ]; then
		(cd "${_subdir}" && git pull origin main);
	else
		git clone "${_repo}" "${_subdir}";
	fi;
};

is_build_script_done() {
	local _op="${1}" _script_fname="${SCRIPT_FNAME##*/}";
	[ -f "${WORKDIR}/.${_script_fname%.build}.${_op}" ];
};
set_build_script_done() {
	local _script_fname="${SCRIPT_FNAME##*/}" _done_fname;
	_done_fname="${WORKDIR}/.${_script_fname%.build}";
	while [ $# -ge 1 ]; do
		if [ "x${1#-}" != "x${1}" ]; then
			rm -f "${_done_fname}.${1#-}";
		else
			printf "" >| "${_done_fname}.${1}";
			log_msg info "Finished build step ${1} of build script \`${_script_fname}'.";
		fi; shift;
	done;
};

log_msg() {
	local _lvl="${1}"; shift;
	case "${_lvl}" in
		fail) printf "\033[91m"; ;;
		info) printf "\033[97m"; ;;
		succ) printf "\033[92m"; ;;
	esac;
	if [ $# -gt 1 ]; then
		printf "==> %s %s %s\033[0m\n" "$(date +"${TIMESTAMP_FMT}")" "${1}" "$*";
	else
		printf "==> %s %s\033[0m\n" "$(date +"${TIMESTAMP_FMT}")" "${1}";
	fi;
};

parse_with_pkg_name() {
	_pkg_name="${1}"; local _envvs _envv; shift;
	while [ $# -ge 0 ]; do
		_pkg_url="${1}"; _envvs="${2}"; _pkg_fname="${_pkg_url##*/}";
		if [ "x${_pkg_fname%%-*}" = "x${_pkg_name}" ]; then
			IFS=:; for _envv in ${_envvs}; do
				export ${_envv};
			done;
			_pkg_subdir="${_pkg_fname%%.tar*}";
			return;
		fi; shift 2;
	done; exit 1;
};

rm_if_exists() {
	while [ $# -gt 1 ]; do [ "x${1%[a-z]}" = "x-" ] &&\
		eval local _${1#-}flag=1; shift; done;
	local _dir="${1}";
	if [ -d "${_dir}" ]; then
		rm -rf "${_dir}" || return 1;
	fi;
	if [ ${_mflag:-0} -eq 1 ]; then
		mkdir "${_dir}" || return 2;
	fi;
	if [ ${_cflag:-0} -eq 1 ]; then
		cd "${_dir}" || return 3;
	fi;
};

run_configure_with_extra_args() {
	local IFS="," _script="${1}" _extra_args="${2}";
	shift 2; "${_script}" "${@}" ${_extra_args};
};

set_build_dir() {
	export _build_dir="${1}-build-${2}";
};

set_env_vars() {
	local _val="${1}"; shift;
	while [ $# -ge 1 ]; do
		export "${1}=${_val}"; shift;
	done;
};

split() {
	local _IFS="${IFS}" IFS="${1}" _txt="${2}";
	set -- ${_txt}; IFS="${_IFS}"; echo "${*}";
};

# vim:filetype=sh