#!/usr/bin/env bash

# The most comprehensive way of figuring this script's absoute path,
# even when we're dealing with multi-level symlinks
SOURCE="${BASH_SOURCE[0]}"
DIR="$( dirname "$SOURCE" )"
while [ -h "$SOURCE" ]
do
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
  DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd )"
done
# DIR is now pointing to the bin dir, BASE_PATH is one level up
BASE_PATH="$(cd "$DIR/.." && pwd)"

# All functions starting with __ are private ones
# You can overwrite them, but ideally they should be left alone.
# Remember Demeter: http://ablogaboutcode.com/2012/02/27/understanding-the-law-of-demeter/

source "$BASE_PATH/libexec/defaults"
source "$BASE_PATH/libexec/output"
source "$BASE_PATH/libexec/deprecations"
source "$BASE_PATH/libexec/core"
source "$BASE_PATH/libexec/common"
source "$BASE_PATH/libexec/generated"

trap __graceful_stop SIGINT SIGTERM

__capture_runtime_configs
__find_all_strategies

source "$BASE_PATH/libexec/init"

__load_app_config
__default_app_config
__apply_runtime_configs
__load_strategy
__remote_hosts

__check_config

# Run the loaded strategy
# Can be overwritten by each strategy, e.g. custom output
#
begin
run
finish

exit 0 # if we reached this point, everything went according to plan, use the appropriate exit status
