#!/usr/bin/env bash
# Produce an MPLAB IPE install for an AT1000 project container, on any x86-64 Linux PC
# with Docker or Podman. Microchip's installer is x86-64 only and its files cannot be
# shipped in the AT1000 image, so each user builds this once and copies it in
# (docs/pickit5.md).
#
#   install-mplab.sh --families 8bit[,16bit,32bit,other] [--version 6.35] [--output FILE]
#
# Downloads MPLABX-v<version>-linux-installer.tar from Microchip into the working
# directory (reused when already there), runs the unattended installer in a throwaway
# debian:trixie-slim container with IPE only and the chosen device families, and writes
# FILE (default mplab-ipe-v<version>.tar): the tree opt/microchip/mplabx/v<version>.
# Nothing is installed on the PC itself.
set -euo pipefail

version=6.35
families=
output=
usage() {
	sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//' >&2
	exit 64
}
while [[ $# -gt 0 ]]; do
	case "$1" in
	--version)
		version=${2:?}
		shift 2
		;;
	--families)
		families=${2:?}
		shift 2
		;;
	--output)
		output=${2:?}
		shift 2
		;;
	-h | --help) usage ;;
	*)
		echo "install-mplab.sh: unknown argument $1" >&2
		usage
		;;
	esac
done
[[ -n "$families" ]] || {
	echo "install-mplab.sh: --families is required (8bit, 16bit, 32bit, other; comma-separated)" >&2
	exit 64
}
output=${output:-mplab-ipe-v$version.tar}

declare -A family=([8bit]=0 [16bit]=0 [32bit]=0 [other]=0)
IFS=, read -ra chosen <<<"$families"
for f in "${chosen[@]}"; do
	[[ -v "family[$f]" ]] || {
		echo "install-mplab.sh: unknown family '$f' (8bit, 16bit, 32bit, other)" >&2
		exit 64
	}
	family[$f]=1
done

[[ "$(uname -m)" == x86_64 ]] || {
	echo "install-mplab.sh: Microchip's installer runs on x86-64 Linux only (this is $(uname -m))" >&2
	exit 1
}
engine=$(command -v docker || command -v podman) || {
	echo "install-mplab.sh: needs docker or podman" >&2
	exit 1
}

tarball=MPLABX-v$version-linux-installer.tar
if [[ ! -f "$tarball" ]]; then
	# Microchip's CDN answers 403 without the download page as referer.
	echo "Downloading $tarball from Microchip..."
	curl -fL --retry 3 -o "$tarball.part" \
		-e https://www.microchip.com/en-us/tools-resources/develop/mplab-x-ide \
		"https://ww1.microchip.com/downloads/aemDocuments/documents/DEV/ProductDocuments/SoftwareTools/$tarball"
	mv "$tarball.part" "$tarball"
fi

echo "Installing MPLAB IPE v$version (families: $families) in a throwaway container..."
staged=.install-mplab-$$.tar.part
# The installer insists on USER=root, and checks for procps and the x86-64 libusb.
"$engine" run --rm --platform linux/amd64 -e USER=root -e DEBIAN_FRONTEND=noninteractive \
	-v "$PWD:/work" -w /work debian:trixie-slim bash -euo pipefail -c '
		apt-get update -qq
		apt-get install -y -qq --no-install-recommends procps libusb-1.0-0 >/dev/null
		tar -xf "$1" -C /tmp
		/tmp/"${1%.tar}".sh --nox11 -- --mode unattended --unattendedmodeui none \
			--installdir /opt/microchip/mplabx/v"$2" --ide 0 --ipe 1 \
			--8bitmcu "$3" --16bitmcu "$4" --32bitmcu "$5" --othermcu "$6" \
			--collectInfo 0 --collectMyMicrochipInfo 0
		test -f /opt/microchip/mplabx/v"$2"/mplab_platform/mplab_ipe/ipecmd.jar
		tar -C / -cf "$7" opt/microchip/mplabx/v"$2"
		chown "$8" "$7"
	' _ "$tarball" "$version" "${family[8bit]}" "${family[16bit]}" "${family[32bit]}" \
	"${family[other]}" "$staged" "$(id -u):$(id -g)"
mv "$staged" "$output"
echo "Wrote $output ($(du -h "$output" | cut -f1)). Copy it into the project: see docs/pickit5.md."
