#!/usr/bin/env bash

# 
# summary
# 
#     this file is used to run commands
#     it loads the PROJECTR env variables first (./settings/projectr_core)
#     then in looks inside the commands folder for commands to run
#     no arguments will start the automatic environment
#     providing an argument (ex: clean) will run whatever is under commands/project/ (ex: commands/project/clean)

# 
# find the projectr_core
# 
path_to_projectr_core=""
file_name="settings/projectr_core"
folder_to_look_in="$PWD"
while :
do
    # check if file exists
    if [ -f "$folder_to_look_in/$file_name" ]
    then
        path_to_projectr_core="$folder_to_look_in/$file_name"
        break
    else
        if [ "$folder_to_look_in" = "/" ]
        then
            break
        else
            folder_to_look_in="$(dirname "$folder_to_look_in")"
        fi
    fi
done
if [ -z "$path_to_projectr_core" ]
then
    #
    # what to do if file never found
    #
    echo "Im a script running with a pwd of:$PWD"
    echo "Im looking for settings/projectr_core in a parent folder"
    echo "Im exiting now because I wasnt able to find it"
    echo "thats all the information I have"
    exit
fi
source "$path_to_projectr_core"


# 
# if given no args
# 
if [[ "$#" = "0" ]]
then
    # start the shell
    "$PROJECTR_COMMANDS_FOLDER/shell"
# 
# otherwise run the respective project command
# 
else
    source "$PROJECTR_COMMANDS_FOLDER/manual_setup"
    # enable globbing
    shopt -s globstar &>/dev/null
    shopt -s dotglob &>/dev/null
    
    # 
    # recursively drill down
    # 
    search_path="$PROJECTR_COMMANDS_FOLDER/project"
    argument_combination="$search_path/$1"
    while [[ -n "$@" ]]
    do
        # pop the first argument off
        shift 1
        for each in "$search_path/"**/*
        do
            if [[ "$argument_combination" = "$each" ]]
            then
                # if its a folder, then we need to go deeper
                if [[ -d "$each" ]]
                then
                    search_path="$each"
                    argument_combination="$argument_combination/$1"
                    
                    # if there is no next argument
                    if [[ -z "$1" ]]
                    then
                        printf "\nThat is a sub folder, not a command\nValid sub-options are\n" 1>&2
                        ls -1 --color -F "$each" | sed 's/^/    /' 1>&2
                        exit 1 # error, no command
                    fi
                    
                    break
                # if its a file, run it with the remaining arguments
                elif [[ -f "$each" ]]
                then
                    "$each" "$@"
                    # make exit status identical to executed program
                    exit $?
                fi
            fi
        done
    done
    printf "\nI could not find that project command\n" 1>&2
    printf "Valid options are:\n" 1>&2
    ls -1 --color -F "$search_path" | sed 's/^/    /' 1>&2
    exit 1 # error, no command
fi