#!/usr/bin/env bash

echo "This script is going to attempt set up my system and it's dependencies. As such it will ask for root privilages."

read -r -p "Are you sure you want to continue? [Y/n] " input
case $input in
[nN][oO] | [nN])
    exit 0
    ;;
*)
    echo "continuing..."
    ;;
esac
./check

script_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
filter=""
dry="0"

while [[ $# -gt 0 ]]; do
    if [[ $1 == "--dry" ]]; then
        dry="1"
    else
        filter="$1"
    fi
    shift
done

log() {
    if [[ dry -eq "1" ]]; then
        echo "[DRY_RUN]: $@"
    else
        echo "$@"
    fi
}

execute() {
    log "execute $*"
    if [[ dry -eq "1" ]]; then
        return
    fi

    "$@"
}

log "$script_dir -- $filter"

cd "$script_dir" || exit 1
scripts=$(find ./runs -maxdepth 1 -mindepth 1 -executable -type f)

for script in $scripts; do
    if echo "$script" | grep -qv "$filter"; then
        log "filtering $script"
        continue
    fi

    execute "$script"

    if [[ $? -gt 0 ]]; then 
        echo "Something went wrong..."
    fi

    read -r -p "Press [ENTER] to continue"
done
