Blob Blame History Raw
#
# set -o errexit -o noglob are assumed.
#

basename() { echo "${1##*/}"; };
date() { command date "+${1:-${TIMESTAMP_FMT}}"; };
dirname() { echo "${1%/*}"; };
get_var_unsafe() { eval echo \${${1}}; };
set_var_unsafe() { [ -n "${2}" ] && eval ${1}=\"${2}\" || return 0; };
pop_IFS() { IFS="${_pI_IFS}"; unset _pI_IFS; };
push_IFS() { _pI_IFS="${IFS}"; IFS="${1}"; };
set_build_dir() { PKG_BUILD_DIR="${1}-${2}-${PKG_TARGET}"; };
test_cmd() { command -v "${1}" >/dev/null; };
toupper() { echo "${1}" | tr a-z A-Z; };

build_fileop() {
	local _op="${1}"; shift;
	if [ "${_op}" = cd ]; then
		log_msg varn "Changing working directory to \`${1}'.";
		[ \( -n "${1}" \) -a \( -L "${1}" -o -e "${1}" \) ] && cd -- "${1}";
	elif [ "${_op}" = cp ]; then
		log_msg varn "Copying \`${1}' to \`${2}' w/ -pPR.";
		[ ${#} -ge 2 ] && cp -pPR -- "${@}";
	elif [ "${_op}" = ln_symbolic ]; then
		log_msg varn "Linking \`${1}' to \`${2}' w/ -fs";
		[ \( -n "${1}" \) -a \( -n "${2}" \) ] && ln -fs -- "${1}" "${2}";
	elif [ "${_op}" = mv ]; then
		log_msg varn "Moving \`${1}' to \`${2}' w/ -fs";
		[ \( -n "${1}" \) -a \( -n "${2}" \) ] && mv -f -- "${1}" "${2}";
	elif [ "${_op}" = mkdir ]\
	||   [ "${_op}" = rm ]; then
		while [ ${#} -gt 0 ]; do
			if [ -z "${1}" ]; then
				return 1;
			elif [ "${_op}" = mkdir ]\
			&&   [ ! -e "${1}" ]; then
				log_msg varn "Making directory \`${1}'.";
				mkdir -p -- "${1}";
			elif [ "${_op}" = rm ]\
			&&   [ \( -L "${1}" \) -o \( -e "${1}" \) ]; then
				log_msg varn "Removing directory or file \`${1}'.";
				rm -rf -- "${1}";
			fi; shift;
		done;
	else
		log_msg failexit "Error: build_fileop() called w/ invalid parameter(s) \`${@}'.";
	fi;
};

build_files_fini() {
	: $((BUILD_TIMES_SECS=$(command date +%s)-${BUILD_TIMES_SECS}));
	: $((BUILD_TIMES_HOURS=${BUILD_TIMES_SECS}/3600));
	: $((BUILD_TIMES_MINUTES=(${BUILD_TIMES_SECS}%3600)/60));
	: $((BUILD_TIMES_SECS=(${BUILD_TIMES_SECS}%3600)%60));
	if [ -f "${BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
		build_fileop rm ${BUILD_STATUS_IN_PROGRESS_FNAME};
	fi;
};
build_files_init() {
	if [ -e ${BUILD_STATUS_IN_PROGRESS_FNAME} ]; then
		log_msg failexit "Error: another build targeting this architecture and build type is currently in progress.";
	else
		touch ${BUILD_STATUS_IN_PROGRESS_FNAME};
	fi;
	if [ -e ${BUILD_LOG_FNAME} ]; then
		mv -- ${BUILD_LOG_FNAME} ${BUILD_LOG_LAST_FNAME};
	fi;
	BUILD_DATE_START="$(date %Y-%m-%d-%H-%M-%S)";
	BUILD_NFINI=${BUILD_NSKIP:=${BUILD_NFAIL:=${BUILD_NBUILT:=0}}};
	BUILD_TIMES_SECS=$(command date +%s);
	BUILD_PKGS_FAILED="";
};

is_build_script_done() {
	local _done_fname="${WORKDIR}/.${1}.${2}" _restart_at="${3}";
	if [ -z "${_restart_at}" ]; then
		if [ -e "${_done_fname}" ]; then
			return 0;	# Skip
		else
			return 1;	# Build
		fi;
	elif [ "${_restart_at}" = "ALL" ]; then
		return 1;		# Build
	elif lmatch "${_restart_at}" , "${2}"; then
		return 1;		# Build
	else
		return 0;		# Skip
	fi;
};

set_build_script_done() {
	local _pkg_fname="${1}";
	local _done_fname_pfx="${WORKDIR}/.${_pkg_fname}";
	shift; while [ ${#} -ge 1 ]; do
		if [ "${1#-}" != "${1}" ]; then
			build_fileop rm "${_done_fname_pfx}.${1#-}";
		else
			touch "${_done_fname_pfx}.${1}";
			log_msg info "Finished build step ${1} of package \`${_pkg_fname}'.";
		fi; shift;
	done;
};

lfilter() {
	[ "x${1}" = "x-not" ] && { local _notfl=1; shift; }
	local _list="${1}" _filter="${2}" _lnew _litem _lfilter _filterfl;
	[ -z "${_filter}" ] && { echo "${_list}"; return 0; };
	for _litem in ${_list}; do
		_filterfl=0;
		for _lfilter in ${_filter}; do
			if [ "${_notfl:-0}" -eq 0 ]\
			&& [ "${_lfilter}" = "${_litem}" ]; then
				_lnew="${_lnew:+${_lnew} }${_litem}"; break;
			elif [ "${_notfl:-0}" -eq 1 ]\
			&& [ "${_lfilter}" = "${_litem}" ]; then
				_filterfl=1; break;
			fi;
		done;
		if [ "${_notfl:-0}" -eq 1 ]\
		&& [ "${_filterfl:-0}" -eq 0 ]; then
			_lnew="${_lnew:+${_lnew} }${_litem}";
		fi;
	done;
	echo "${_lnew}";
};

lmatch() {
	local _cmp="${3}"; push_IFS "${2}"; set -- ${1}; pop_IFS;
	while [ ${#} -gt 0 ]; do
		if [ "${1}" = "${_cmp}" ]; then
			return 0;
		fi; shift;
	done; return 1;
};

log_env_vars() {
	local _nvar=1 _arg _arg_len_max=0;
	log_msg info "Variables for this ${1:-build}:"; shift;
	while [ ${_nvar} -le ${#} ]; do
		_arg="$(eval echo \${${_nvar}})";
		_arg="${_arg%%=*}";
		if [ ${#_arg} -gt ${_arg_len_max} ]; then
			_arg_len_max=${#_arg};
		fi; : $((_nvar+=1));
	done;
	while [ ${#} -gt 0 ]; do
		log_msg info "$(printf					\
			"%${_arg_len_max}.${_arg_len_max}s=%s"	\
			"${1%%=*}" "$(get_var_unsafe ${1#*=})")";
		shift;
	done;
};

log_msg() {
	local _lvl="${1}"; shift;
	if [ "${_lvl#v}" != "${_lvl}" ]\
	&& [ ${ARG_VERBOSE:-0} -eq 0 ]; then
		return;
	fi;
	case "${_lvl}" in
		failexit) printf "\033[0m\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
		fail) printf "\033[0m\033[${LOG_MSG_FAIL_COLOUR}m"; ;;
		info) printf "\033[0m\033[${LOG_MSG_INFO_COLOUR}m"; ;;
		vnfo) printf "\033[0m\033[${LOG_MSG_INFO_COLOUR}m"; ;;
		succ) printf "\033[0m\033[${LOG_MSG_SUCC_COLOUR}m"; ;;
		warn) printf "\033[0m\033[${LOG_MSG_WARN_COLOUR}m"; ;;
		varn) printf "\033[0m\033[${LOG_MSG_WARN_COLOUR}m"; ;;
	esac;
	if [ ${#} -gt 1 ]; then
		printf "==> %s %s %s\033[0m\n" "$(date)" "${1}" "$*";
	else
		printf "==> %s %s\033[0m\n" "$(date)" "${1}";
	fi; [ ${_lvl} = failexit ] && exit 1 || return 0;
};

run_cmd_unsplit() {
	local _cmd="${1}" _cmdline _rc; shift;
	while [ ${#} -gt 0 ]; do
		[ -n "${1}" ] &&\
			_cmdline="${_cmdline:+${_cmdline}:}${1}";
		shift;
	done;
	push_IFS :; ${_cmd} ${_cmdline}; _rc=$?; pop_IFS;
	return ${_rc};
};

set_env_vars_with_sep() {
	local _sep="${1}"; shift; push_IFS ${_sep}; set -- ${1};
	while [ ${#} -gt 0 ]; do
		export "${1}"; shift;
	done; pop_IFS;
};

subst_tgts() {
	local _pkgs;
	while [ ${#} -ge 1 ]; do
	_pkgs="$(get_var_unsafe "$(toupper "${1}")_TARGET")";
	if [ -n "${_pkgs}" ]; then
		echo "${_pkgs}";
	else
		echo "${1}";
	fi; shift;
	done;
};

# vim:filetype=sh