#!/bin/bash
set -euo pipefail

if [ $# != 1 ] ; then
	echo >&2 "Usage: $0 <stage-directory>"
	exit 1
fi

STAGE=$1

STAGE=/build/$STAGE
BUILD_COMMON=/build/common
BUILD_CONFIG=/build/config-tmp
. $BUILD_COMMON/lib.sh

note "Running $STAGE"

if [ ! -v http_proxy -o ! -v https_proxy ] ; then
	warn "No HTTP(S) proxy is set"
fi

case "$STAGE" in
	*.sh)
		. $STAGE
		exit 0
		;;
	*.d)
		;;
	*)
		die "Unrecognized stage name $STAGE"
esac

cd $STAGE

if [ -f run.sh ] ; then
	. run.sh
else
	for a in [0-9]* ; do
		case "$a" in
			*.sh)
				( . $a )
				;;
			*.d)
				( cd $a && . run.sh )
				;;
			*)
				warn "Unrecognized build step file $a"
		esac
	done
fi