#!/bin/sh
# shellcheck disable=SC2034

# This script installs dev tools (build tools & toolchain)

# https://github.com/xpack-dev-tools/windows-build-tools-xpack/releases
# https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases


build_tools_version="4.4.1-2"		# For Windows only
toolchain_version="12.3.1-1.2"		# For all OS

###############################################################################################

install_path=~/.xpack-dev-tools		# For all OS
api_build_tools_url=https://api.github.com/repos/xpack-dev-tools/windows-build-tools-xpack/releases
api_toolchain_url=https://api.github.com/repos/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases

Extract() {
	archive="$1"

	if echo "$archive" | grep -E -- ".zip$" >/dev/null 2>&1; then
		unzip -o "$archive"
	fi

	if echo "$archive" | grep -E -- ".tar.gz$" >/dev/null 2>&1; then
		tar xvf "$archive"
	fi
}

# Usage: Install [api_url] [version] [file_ending]
Install() {
	api_url="$1"
	version="$2"
	file_ending="$3"

	install_folder="xpack-"$(echo "$api_url" | cut -d '/' -f6 | sed "s/xpack/$version/")
	# echo "Install folder: $install_folder"

	if [ -d "$install_path/$install_folder" ]; then
		echo "Use: $install_folder"
		return
	fi

	url=$(curl --ssl-no-revoke -s -L "$api_url" | grep -- "browser_download_url" | grep -- "$version" | grep -- "$file_ending\"" | cut -d '"' -f4)
	# echo "Download URL: $url"
	archive=$(basename "$url")
	# echo "Archive: $archive"

	mkdir -p "$install_path"
	cd "$install_path" || exit 1

	curl --ssl-no-revoke -L -O "$url"
	Extract "$archive"
	rm "$archive"
}

# Windows
if uname | grep -- "MINGW64" >/dev/null 2>&1; then
	Install "$api_build_tools_url" "$build_tools_version" "win32-x64.zip"
	Install "$api_toolchain_url" "$toolchain_version" "win32-x64.zip"
	exit
fi

# Linux
if uname | grep -- "Linux" >/dev/null 2>&1; then
	if uname -a | grep -- "x86_64" >/dev/null 2>&1; then
		Install "$api_toolchain_url" "$toolchain_version" "linux-x64.tar.gz"
	elif uname -a | grep -- "aarch64" >/dev/null 2>&1; then
		Install "$api_toolchain_url" "$toolchain_version" "linux-arm64.tar.gz"
	elif uname -a | grep -- "arm" >/dev/null 2>&1; then
		Install "$api_toolchain_url" "$toolchain_version" "linux-arm.tar.gz"
	else
		echo "Unknown Linux architecture"; exit 1
	fi
	if ( ! xxd --version > /dev/null 2>&1 ) || ( ! srec_cat --version > /dev/null 2>&1 ); then
		sudo apt update
	fi
	if ! xxd --version > /dev/null 2>&1; then
		sudo apt install -y xxd
	else
		echo "Use: xxd"
	fi
	if ! srec_cat --version > /dev/null 2>&1; then
		sudo apt install -y srecord
	else
		echo "Use: srecord"
	fi
	exit
fi

# MAC
if uname | grep -- "Darwin" >/dev/null 2>&1; then
	if uname -a | grep -- "arm64" >/dev/null 2>&1; then
		Install "$api_toolchain_url" "$toolchain_version" "darwin-arm64.tar.gz"
	elif uname -a | grep -- "x86_64" >/dev/null 2>&1; then
		Install "$api_toolchain_url" "$toolchain_version" "darwin-x64.tar.gz"
	else
		echo "Unknown Darwin architecture"; exit 1
	fi
	brew install make srecord coreutils
	exit
fi