#!/bin/bash -xe
# Run a ${INFRA_REPO}/build_$1.sh script
# Return the exit status
# Run a ccache -s before and after

: Running $0 "$@"
date '+%D %T (%s)'
component=$1
shift

# errorhandler(){
#     local RC=$?			# Save exit status
#     # bash doesn't call error handlers from within error handlers
#     # but be on the safe side and disable it anyhow.
#     set +e
# }
# trap errorhandler ERR

doit(){
    bash -x "${INFRA_REPO}/build_${component}.sh" "$@" &&
        ${INFRA_REPO}/post_inst_pkg.sh "${component}"
}

final(){
    set +e
    date '+%D %T (%s)'
    : 'Final ccache stats'
    ccache -s || echo ccache failed tto show stats
    : 'Output from "times" builtin'
    times
}
trap final EXIT


: 'Initial ccache stats'
ccache -s || echo ccache failed to show stats
source ${INFRA_REPO}/envsetup.sh
${INFRA_REPO}/build_${component}.sh -c
time doit "$@"
