#!/bin/bash

# Save this file as ~/.config/yadm/bootstrap and make it executable. It will
# execute all executable files (excluding templates and editor backups) in the
# ~/.config/yadm/bootstrap.d directory when run.

set -eu

# Directory to look for bootstrap executables in
BOOTSTRAP_D="${BASH_SOURCE[0]}.d"

if [[ ! -d "$BOOTSTRAP_D" ]]; then
    echo "Error: bootstrap directory '$BOOTSTRAP_D' not found" >&2
    exit 1
fi

declare -a bootstraps
while IFS= read -r bootstrap; do
    if [[ -x "$bootstrap" && ! "$bootstrap" =~ "##" && ! "$bootstrap" =~ ~$ ]]; then
        bootstraps+=("$bootstrap")
    fi
done < <(find -L "$BOOTSTRAP_D" -type f | sort)

for bootstrap in "${bootstraps[@]}"; do
    if ! "$bootstrap"; then
        echo "Error: bootstrap '$bootstrap' failed" >&2
        exit 1
    fi
done