FROM debian:bookworm-slim

USER root
RUN \
	apt-get update && \
	apt-get install -y --no-install-recommends \
		bzip2 ca-certificates ccache curl flex git libfl2 make tar && \
	rm -rf /var/lib/apt/lists/* && \
	useradd -p locked -m mani

RUN mkdir -p -m 1777 /ccache && \
	mkdir -p /opt/djgpp

ARG DJGPP_RELEASE=v3.4
ARG DJGPP_ARCHIVE=djgpp-linux64-gcc1220.tar.bz2
RUN curl -L --fail \
		https://github.com/andrewwutw/build-djgpp/releases/download/${DJGPP_RELEASE}/${DJGPP_ARCHIVE} | \
	tar -xj --strip-components=1 -C /opt/djgpp

ENV PATH=/opt/djgpp/bin:$PATH
ENV GIT_SSL_NO_VERIFY=1
USER mani

ARG ORIGIN=https://review.sourcearcade.org/flashprog.git
ARG PCIUTILS_TAG=v3.15.0
ARG GETOPT_VERSION=2.5
RUN cd && \
	mkdir .ccache && \
	git clone ${ORIGIN} flashprog && \
	git clone --depth 1 --branch ${PCIUTILS_TAG} \
		https://git.kernel.org/pub/scm/utils/pciutils/pciutils.git && \
	cd pciutils && \
		make ZLIB=no DNS=no HOST=i386-djgpp-djgpp \
			CROSS_COMPILE=i586-pc-msdosdjgpp- \
			PREFIX=/ DESTDIR=$PWD/../ \
			install-lib && \
		cd ../ && \
	curl -L --fail https://ftp.math.utah.edu/pub/getopt/getopt-${GETOPT_VERSION}.tar.gz | tar xz && \
	cd getopt-${GETOPT_VERSION} && \
		printf '%s\n' 'char *gettext(const char *msgid);' >posix/gettext.h && \
		sed -i -e 's,env/[gs]etenv\.c,,g' Makefile.in && \
		AR=i586-pc-msdosdjgpp-ar RANLIB=i586-pc-msdosdjgpp-ranlib \
			./configure --host i586-pc-msdosdjgpp && \
		make && \
		cp libgetopt.a ../lib/ && \
		cp posix/getopt.h ../include/

ARG IDENT=mani
ARG CCACHE_MAX=32M
RUN \
	ccache --set-config cache_dir=/ccache/${IDENT} && \
	ccache --set-config max_size=${CCACHE_MAX}

ENV DEVSHELL /bin/bash
COPY mani-wrapper.sh /home/mani/
ENTRYPOINT ["/bin/sh", "/home/mani/mani-wrapper.sh"]
