# syntax=docker/dockerfile:1
ARG BASE_OS=ubuntu
ARG BASE_CODENAME=noble
FROM $BASE_OS:$BASE_CODENAME AS build-stage
ARG TARGETARCH

# Download build dependencies
RUN <<EOR
	set -eu

	# Workaround for outstanding fix of https://bugs.launchpad.net/ubuntu/+source/python-build/+bug/1992108
	. /etc/os-release

	export DEBIAN_FRONTEND=noninteractive
	apt-get update
	# 24.04 does not package gtk4 layer shell, we build it ourselves
	if [ "${UBUNTU_CODENAME:-}" = "noble" ]; then
		case "${TARGETARCH}" in
			amd64) package_revision=24.04.1 ;;
			arm64) package_revision=24.04.2 ;;
			*) echo "Unsupported Noble architecture: ${TARGETARCH}" >&2; exit 1 ;;
		esac
		apt-get install -y curl
		for package in gir1.2-gtk4layershell-1.0 libgtk4-layer-shell0 libgtk4-layer-shell-dev; do
			file="${package}_1.3.0-1.${package_revision}_${TARGETARCH}.deb"
			curl -fL "https://github.com/C0rn3j/sc-controller/releases/download/v0.0.0_extras/${file}" --output "${file}"
		done
		apt-get install -y ./*_1.3.0-1."${package_revision}"_"${TARGETARCH}".deb
		rm ./*_1.3.0-1."${package_revision}"_"${TARGETARCH}".deb
	fi
	apt-get install -y --no-install-recommends \
		cmake \
		gir1.2-gtk4layershell-1.0 \
		gir1.2-rsvg-2.0 \
		libcairo2-dev \
		libgirepository-2.0-dev \
		libgtk-4-dev \
		libgtk4-layer-shell-dev \
		gcc \
		git \
		librsvg2-bin \
		libxfixes3 \
		linux-headers-generic \
		python3-build \
		python3-dev \
		python3-setuptools \
		python3-usb \
		python3-venv \
		python-is-python3

	apt-get clean && rm -rf /var/lib/apt/lists/*
EOR
# Prepare working directory and target
COPY . /work
WORKDIR /work
ARG TARGET=/build

# Build and install
RUN <<EOR
	set -eu

	python -m build --wheel
	python -m venv .venv
	. .venv/bin/activate
	# TODO(Martin): Replace URL with just 'hidraw-pure' when https://github.com/vpelletier/python-hidraw/issues/7 is resolved
	pip install evdev https://github.com/C0rn3j/python-hidraw/archive/modernize.zip ioctl-opt libusb1 pytest vdf
	# Install into the active environment for tests
	pip install dist/*.whl
	python -m pytest tests

	pip install --prefix "${TARGET}/usr" --no-warn-script-location --force-reinstall dist/*.whl

	# Save version
	PYTHONPATH=$(find "${TARGET}" -type d -name site-packages) \
	python -c "from scc.constants import DAEMON_VERSION; print('VERSION=' + DAEMON_VERSION)" >>/build/.build-metadata.env

	# Fix shebangs of scripts from '#!/work/.venv/bin/python' - note that AppImage builder will strip the leading /
	find "${TARGET}/usr/bin" -type f | xargs sed -i 's:work/.venv/bin/:usr/bin/env :'

	# Provide input-event-codes.h as fallback for runtime systems without linux headers
	cp -a \
		"$(find /usr -type f -name input-event-codes.h -print -quit)" \
		"$(find "${TARGET}" -type f -name uinput.py -printf '%h\n' -quit)"

	# Create short name symlinks for static libraries
	suffix=".cpython-*-$(uname -m)-linux-gnu.so"
	find "${TARGET}" -type f -path "*/site-packages/*${suffix}" \
		| while read -r path; do ln -sfr "${path}" "${path%${suffix}}.so"; done

	share="${TARGET}/usr/share"

	# Put AppStream metadata to required location according to https://wiki.debian.org/AppStream/Guidelines
	metainfo="${share}/metainfo"
	mkdir -p "${metainfo}"
	cp -a scripts/io.github.c0rn3j.sc-controller.metainfo.xml "${metainfo}"

	# Convert icon to png format (required for icons in .desktop file)
	iconpath="${share}/icons/hicolor/512x512/apps"
	mkdir -p "${iconpath}"
	rsvg-convert --background-color none -o "${iconpath}/sc-controller.png" images/sc-controller.svg
EOR

# Store build metadata
ARG TARGETOS TARGETARCH TARGETVARIANT
RUN export "TARGETMACHINE=$(uname -m)" && printenv | grep ^TARGET >>/build/.build-metadata.env

# Keep only files required for runtime
FROM scratch AS export-stage
COPY --from=build-stage /build /
