FROM debian:bullseye
EXPOSE 34197/udp

# you can configure this by passing --build-arg=FACTORIO_VERSION=0.17.79
ARG FACTORIO_VERSION=1.1.104

ADD bin/entrypoint.sh /entrypoint.sh

RUN \
  apt-get --yes update &&\
  apt-get --yes install wget xz-utils &&\
  apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* &&\
  adduser --disabled-password --gecos "" --home /opt/factorio factorio &&\
  chmod 755 /entrypoint.sh &&\
  mkdir -p /opt/factorio/headless /opt/factorio/saves &&\
  chown -R factorio /opt/factorio

USER factorio
RUN \
  wget -O- https://factorio.com/get-download/$FACTORIO_VERSION/headless/linux64 \
  | tar xvJ -C /opt/factorio/headless

RUN mkdir /opt/factorio/headless/factorio/config/
ADD bin/config.ini /opt/factorio/headless/factorio/config/config.ini
RUN sed -i '/write-data=/c\write-data=\/opt\/factorio/' /opt/factorio/headless/factorio/config/config.ini

VOLUME /opt/factorio/saves

WORKDIR /opt/factorio

ENTRYPOINT ["/entrypoint.sh"]
CMD ["--server-settings","/opt/factorio/settings/server-settings.json"]