#!/usr/bin/env bash
set -u
set -e

# update script
# this accepts a single line up input in three args
# it fails at the first of any child script failing (and that branch fails to update)

my_dir=$(dirname $0)
my_exit_code=""

for my_hook in ${my_dir}/update.d/*; do

	# Bail and let the user know if the hook isn't executable
	if [ ! -x "${my_hook}" ] || [ ! -f "${my_hook}" ]; then
		>&2 echo "${my_hook} is not (or does not point to) an executable file"
		exit 155
	fi

	# Allow the script to fail so we can bubble the exit code and ensure an error message
	set +e
	("${my_hook}" $1 $2 $3)
	my_exit_code=$?
	set -e

	# Bail on the first script failure
	if [ "0" != "${my_exit_code}" ]; then
		>&2 echo "${my_hook} failed with exit code ${my_exit_code}"
 		exit ${my_exit_code}
	fi
done
