lalbornoz 340f4c
#!/bin/sh
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
#
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
lalbornoz 340f4c
{
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
. ./build.subr;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
while getopts hr:t CURRENT_ARG; do
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
case ${CURRENT_ARG} in
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
r)	ARG_BUILD_SCRIPTS="${OPTARG%%:*}";
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	[ -z "${ARG_BUILD_STEPS:="${ARG_BUILD_SCRIPTS##*:}"}" ] &&\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
		ARG_BUILD_STEPS=ALL; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
t)	ARG_TARBALL=1; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
h|\?)	exec cat build.usage; ;;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
esac; done; shift $((${OPTIND} - 1));
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
while [ ${#} -gt 0 ]; do
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	if [ "x${1#*=*}" != "x${1}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
		set_var_unsafe "$(get_prefix_lrg "${1}" =)"				\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
			"$(get_postfix "${1}" =)"; shift;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
done; . ./build.vars;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
clear_env_with_except HOME PATH SHELL TERM USER;
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
check_path_vars PREFIX PREFIX_NATIVE WORKDIR;
Lucio Andrés Illanes Albornoz (arab, vxp) c13296
check_prereqs git make mktemp openssl patch sed sort tar tr wget;
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
log_msg info "Build started by ${BUILD_USER:=${USER}}@${BUILD_HNAME:=$(hostname)} at ${BUILD_DATE:=$(date %Y-%m-%d-%H-%M-%S)}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
log_env_vars ${LOG_ENV_VARS};
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
(mkdir -p ${PREFIX} ${PREFIX_NATIVE} ${PREFIX_TARGET} ${WORKDIR};
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
touch BUILD_IN_PROGRESS ${BUILD_PROGRESS_FNAME:=${PREFIX}/BUILD_STARTED_AT_${BUILD_DATE}};
Lucio Andrés Illanes Albornoz (arab, vxp) 96b309
BUILD_NFINI=${BUILD_NSKIP:=${BUILD_NFAIL:=${BUILD_NBUILT:=0}}};
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
BUILD_SECS=$(command date +%s);
Lucio Andrés Illanes Albornoz (arab, vxp) 75a203
for BUILD_LVL in 0 1 2 3; do
lalbornoz 340f4c
	for BUILD_SCRIPT_FNAME in ${BUILD_LVL}[0-9][0-9].*.build; do
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
		if [ -n "${ARG_BUILD_SCRIPTS}" ]					\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
		&& [ "${ARG_BUILD_SCRIPTS}" != "ALL" ]					\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
		&& ! match_list "${ARG_BUILD_SCRIPTS}"					\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
				, "${BUILD_SCRIPT_FNAME}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			log_msg info "Skipped build script \`${BUILD_SCRIPT_FNAME}' (--build-scripts policy.)";
lalbornoz 340f4c
			continue;
Lucio Andrés Illanes Albornoz (arab, vxp) 29df05
		elif [ ! -f ${BUILD_SCRIPT_FNAME} ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			log_msg info "Build script \`${BUILD_SCRIPT_FNAME}' non-existent or not a file.";
lalbornoz 340f4c
			continue;
lalbornoz 340f4c
		else
Lucio Andrés Illanes Albornoz (arab, vxp) 96b309
			unset BUILD_SCRIPT_RC; : $((BUILD_NBUILT+=1));
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			if [ "x${ARG_BUILD_SCRIPTS}" != "xALL" ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			&& is_build_script_done finish "${BUILD_SCRIPT_FNAME%.build}"; then
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
				log_msg info "Skipped build script \`${BUILD_SCRIPT_FNAME}' (already built.)";
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
					: $((BUILD_NSKIP+=1)); BUILD_SCRIPT_RC=0; continue;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
			log_msg info "Invoking build script${ARG_BUILD_SCRIPTS:+ (forcibly)} \`${BUILD_SCRIPT_FNAME}'${ARG_BUILD_STEPS:+ at build step ${ARG_BUILD_STEPS}}.";
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			(set -o errexit -- $(split . ${BUILD_SCRIPT_FNAME%%.build*});	\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 SCRIPT_FNAME=${BUILD_SCRIPT_FNAME};				\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 SCRIPT_NAME=${SCRIPT_FNAME%%.build*};				\
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
			 export CFLAGS="$(eval echo \${CFLAGS_LVL${BUILD_LVL}})";	\
Lucio Andrés Illanes Albornoz (arab, vxp) f9c386
			 export PREFIX_LVL="$(eval echo \${PREFIX_LVL${BUILD_LVL}})";	\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 _PWD=$(pwd); cd ${WORKDIR};					\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 for SCRIPT_SOURCE in build.subr ${SCRIPT_NAME}.vars		\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
					${BUILD_SCRIPT_FNAME}; do			\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
			 	[ -f ${_PWD}/${SCRIPT_SOURCE} ] &&			\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
					 . ${_PWD}/${SCRIPT_SOURCE};			\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
			 done);
lalbornoz 340f4c
			case ${BUILD_SCRIPT_RC:=${?}} in
lalbornoz 340f4c
			0) log_msg succ "Finished build script \`${BUILD_SCRIPT_FNAME}'.";
lalbornoz 340f4c
				: $((BUILD_NFINI+=1)); continue; ;;
lalbornoz 340f4c
			*) log_msg fail "Build failed in build script \`${BUILD_SCRIPT_FNAME}' (last return code ${BUILD_SCRIPT_RC}.).";
lalbornoz 340f4c
				: $((BUILD_NFAIL+=1)); break; ;;
lalbornoz 340f4c
			esac;
lalbornoz 340f4c
		fi;
lalbornoz 340f4c
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	if [ ${BUILD_SCRIPT_RC:-0} -ne 0 ]; then
lalbornoz 340f4c
		break;
lalbornoz 340f4c
	fi;
lalbornoz 340f4c
done;
Lucio Andrés Illanes Albornoz (arab, vxp) 96b309
log_msg info "${BUILD_NFINI} finished, ${BUILD_NSKIP} skipped, and ${BUILD_NFAIL} failed builds in ${BUILD_NBUILT} build script(s).";
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
: $((BUILD_SECS=$(command date +%s)-${BUILD_SECS}));
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
: $((BUILD_HOURS=${BUILD_SECS}/3600));
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
: $((BUILD_MINUTES=(${BUILD_SECS}%3600)/60));
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
: $((BUILD_SECS=(${BUILD_SECS}%3600)%60));
Lucio Andrés Illanes Albornoz (arab, vxp) fb434b
log_msg info "Build time: ${BUILD_HOURS} hour(s), ${BUILD_MINUTES} minute(s), and ${BUILD_SECS} second(s).";
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
[ -f ${BUILD_PROGRESS_FNAME} ] && rm -f ${BUILD_PROGRESS_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
touch ${BUILD_FINISH_FNAME:=${PREFIX}/BUILD_FINISHED_AT_$(date %Y-%m-%d-%H-%M-%S)}	\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	${TARBALL_PROGRESS_FNAME:=${PREFIX}/TARBALL_STARTED_AT_$(date %Y-%m-%d-%H-%M-%S)};
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
ln -sf ${BUILD_FINISH_FNAME} ${PREFIX}/BUILD_FINISHED_AT;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
# rotate BUILD_FINISH_FNAME files
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
if [ $(( ${BUILD_NFINI} + ${BUILD_NSKIP} )) -ge 0 ]					\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
&& [ ${BUILD_NFAIL} -eq 0 ]\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
&& [ ${ARG_TARBALL:-0} -eq 1 ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	log_msg info "Building distribution tarball.";
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	(cd ${PREFIX};
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	DISTRIB_FNAME=midipix.${BUILD_USER}@${BUILD_HNAME}-${BUILD_DATE}.tar.bz2;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	PREFIX_BASENAME=${PREFIX_NATIVE##*/}; WORKDIR_BASENAME=${WORKDIR##*/};
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	rm_if_exists -m ${PREFIX_BASENAME}/lib.bak; rm_if_exists ${DISTRIB_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	tar -C ${PREFIX_BASENAME}/lib -cpf - . | tar -C ${PREFIX_BASENAME}/lib.bak -xpf -;
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
	(cd native/lib &&
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 find . -maxdepth 1 -type l							\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 	-exec sh -c 'dest=$(readlink -- "$0") && rm -- "$0" && ln -- "$dest" "$0"' {} \;);
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 wait;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 find .	-maxdepth 2 -type d							\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path .								\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./src/midipix_build						\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./src/midipix_build/\*					\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./${WORKDIR_BASENAME}					\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./${WORKDIR_BASENAME}/\*					\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./${PREFIX_BASENAME}						\
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	 	-not -path ./${PREFIX_BASENAME}/lib.bak					|\
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 tar -T - -cpf - | bzip2 -9c - > ${DISTRIB_FNAME}
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 rm -rf ${PREFIX_BASENAME}/lib;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
	 mv ${PREFIX_BASENAME}/lib.bak ${PREFIX_BASENAME}/lib); wait;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
	# rotate tarballs
Lucio Andrés Illanes Albornoz (arab, vxp) d30b8f
fi;
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
[ -f ${TARBALL_PROGRESS_FNAME} ] && rm -f ${TARBALL_PROGRESS_FNAME};
Lucio Andrés Illanes Albornoz (arab, vxp) 090fd4
[ -f BUILD_IN_PROGRESS ] && rm -f BUILD_IN_PROGRESS;
Lucio Andrés Illanes Albornoz (arab, vxp) 1d0876
exit ${BUILD_SCRIPT_RC})} 2>&1 | tee build.log;
lalbornoz 340f4c
lalbornoz 340f4c
# vim:filetype=sh