main() {
	# Use colors, but only if connected to a terminal, and that terminal
	# supports them.
	if which tput >/dev/null 2>&1; then
		ncolors=$(tput colors)
	fi
	if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
		RED="$(tput setaf 1)"
		GREEN="$(tput setaf 2)"
		YELLOW="$(tput setaf 3)"
		BLUE="$(tput setaf 4)"
		BOLD="$(tput bold)"
		NORMAL="$(tput sgr0)"
	else
		RED=""
		GREEN=""
		YELLOW=""
		BLUE=""
		BOLD=""
		NORMAL=""
	fi

	# Only enable exit-on-error after the non-critical colorization stuff,
	# which may fail on systems lacking tput or terminfo
	set -e

	# Setup Global Variables
	LATEST_URL="https://www.ortussolutions.com/parent/download/commandbox/type/bin"
	TARGET_VERSION=${1}
	REPO_URL="https://downloads.ortussolutions.com/ortussolutions/commandbox/${TARGET_VERSION}/commandbox-bin-${TARGET_VERSION}.zip"
	DESTINATION="/usr/local/bin"
	COMMANDBOX="${DESTINATION}/box"
	
	# Determine which URL to use
	if [ ${TARGET_VERSION} == "latest" ]; then
		DOWNLOAD_URL=${LATEST_URL}
	else
		DOWNLOAD_URL=${REPO_URL}
	fi
	printf "${BLUE}Downloading CommandBox v${TARGET_VERSION} from ${DOWNLOAD_URL}...${NORMAL}\n"
	
	if [ -f "$COMMANDBOX" ]; then
		printf "${YELLOW}You already have CommandBox installed.${NORMAL}\n"
		printf "You'll need to remove '${COMMANDBOX}' if you want to re-install.\n"
		exit
	fi

	# Check curl exists
	command -v curl >/dev/null 2>&1 || {
		echo "Error: curl is not installed"
		exit 1
	}

	# Make sure tmp folder exists
	mkdir -p /tmp

	# Download CommandBox
	env curl -Lk -o /tmp/box.zip -location ${DOWNLOAD_URL} || {
		printf "Error: Download of CommandBox binary failed\n"
		exit 1
	}

	# Inflate it
	printf "${BLUE}Unziping CommandBox...${NORMAL}\n"
	unzip /tmp/box.zip -d ${DESTINATION}

	# Make it executable
	printf "${BLUE}Making CommandBox Executable...${NORMAL}\n"
	chmod +x ${COMMANDBOX}
	
	# Run it
	printf "${BLUE}Running CommandBox...${NORMAL}\n"
	${COMMANDBOX} version

	printf "${GREEN}"
	echo '  ____                                          _ ____           '
	echo ' / ___|___  _ __ ___  _ __ ___   __ _ _ __   __| | __ )  _____  __ '
	echo '| |   / _ \| ''_ ` _ \| ''_ ` _ \ / _` | ''_ \ / _` |  _ \ / _ \ \/ / '
	echo '| |__| (_) | | | | | | | | | | | (_| | | | | (_| | |_) | (_) >  <  '
	echo ' \____\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|____/ \___/_/\_\ '
	echo ''
	echo 'is now installed, make sure you run "box" to start up the interactive shell!'
	echo ''
	echo 'p.s. Follow us at https://twitter.com/ortussolutions.'
	echo ''
	echo 'p.p.s. Clone us and star us at https://github.com/Ortus-Solutions/commandbox'
	echo ''
	echo 'Please support us via Patreon at https://www.patreon.com/ortussolutions'
	printf "${NORMAL}"
  
}

main ${0:-latest}