#!/usr/bin/env bash

COMPOSE_DOCKER_CLI_BUILD=1
DOCKER_BUILDKIT=1

#########################
# Handy Color Variables #
#########################

# Reset
Reset="\033[0m"; # Text Reset

# Regular Colors
Black="\033[0;30m"; # Black
Red="\033[0;31m"; # Red
Green="\033[0;32m"; # Green
Yellow="\033[0;33m"; # Yellow
Blue="\033[0;34m"; # Blue
Purple="\033[0;35m"; # Purple
Cyan="\033[0;36m"; # Cyan
White="\033[0;37m"; # White

# Bold
BBlack="\033[1;30m"; # Black
BRed="\033[1;31m"; # Red
BGreen="\033[1;32m"; # Green
BYellow="\033[1;33m"; # Yellow
BBlue="\033[1;34m"; # Blue
BPurple="\033[1;35m"; # Purple
BCyan="\033[1;36m"; # Cyan
BWhite="\033[1;37m"; # White

# Arguments
cmd=${1};
allArgs=${@};
allArgsExceptFirst=${@:2};
secondArg=${2};

# Variables
composeFiles="-f docker-compose.yml -f docker-compose.dev.yml -f docker-compose.env.yml";
nodeDockerImage="node:16.10.0";

if [[ ! -f "docker-compose.env.yml" ]]; then
    cp docker-compose.env.yml.example docker-compose.env.yml;
fi

# Ensure .env.local exists
if [[ ! -f ".env.local" ]]; then
    touch .env.local;
fi

# Source all of our executable files — these should contain functions that can be run
for f in $(find devBin -name '*.sh'); do
    chmod +x ${f};
    source ${f};
done

# If no command/function name to run was provided, list out the functions available
if [[ -z "${cmd}" || ${secondArg} == "help" ]]; then
    source devBin/.help;
    exit 0;
fi

# If the specified command is provided by our scripts, then run it
if _function_exists "${cmd}"; then
    # Run the specified function (${cmd}) and pass it any arguments specified (${allArgsExceptFirst})
    ${cmd} "${allArgsExceptFirst}";
    exit;
fi

# If the specified command doesn't exist, we did an oops
printf "${Red}The specified command does not exist\n${Reset}";
