FROM rust:1.84.1 AS lib_builder
WORKDIR /src
COPY rust/ .
RUN cargo build 

FROM ghcr.io/allanger/dumb-downloader as dudo
RUN apt-get update -y && apt-get install unzip -y
ENV RUST_LOG=info
ENV GODOT_VERSION=4.3
WORKDIR /out
RUN dudo -l "https://github.com/godotengine/godot/releases/download/{{ version }}-stable/Godot_v{{ version }}-stable_{{ os }}.{{ arch }}.zip" -d /tmp/godot.zip -p $GODOT_VERSION
RUN cd /tmp && unzip godot.zip && rm -f godot.zip
RUN sh -c 'find /tmp -type f -print0 -name "Godot*"  | xargs --null -I{} mv {} godot'

RUN dudo -l "https://github.com/godotengine/godot/releases/download/{{ version }}-stable/Godot_v{{ version }}-stable_export_templates.tpz" -d /tmp/export_templates.zip -p $GODOT_VERSION
RUN cd /tmp && unzip export_templates.zip && rm -f export_templates.zip
RUN ls /tmp
RUN mkdir -p /out/templates
RUN sh -c 'cd /tmp && find . -type f -print0 -name "Godot*"  | xargs --null -I{} mv {} /out/{}'
RUN chmod +x /out/godot

FROM ubuntu as builder
RUN apt-get update -y && apt-get install fontconfig -y
WORKDIR /src
COPY --from=dudo /out/godot /usr/bin/godot
COPY --from=lib_builder /src/target/debug/libopen_strike_2.so /src/rust/target/debug/libopen_strike_2.so
RUN mkdir -p /root/.local/share/godot/export_templates/4.3.stable/
COPY --from=dudo /out/templates /root/.local/share/godot/export_templates/4.3.stable
COPY . .
RUN mkdir /out
RUN godot --verbose --headless --export-debug "[Dedicated Server] Linux" /out/open_strike

FROM ubuntu
RUN apt-get update -y && apt-get install fontconfig -y
WORKDIR /app
COPY --from=builder /out .
CMD ["/app/open_strike.sh"]
