FROM hexpm/elixir:<%= @elixir_version %>-erlang-<%= @erlang_version %>-ubuntu-<%= @ubuntu_version %>-<%= @ubuntu_date_tag %>

ARG TZ
ENV TZ="$TZ"

ARG NODE_MAJOR=20
<%= for {name, default} <- @package_args do %>ARG <%= name %>=<%= default %>
<% end %>
# Install basic development tools, iptables/ipset, and Node.js
RUN apt-get update && apt-get install -y --no-install-recommends \
  ca-certificates \
  curl \
  less \
  git \
  procps \
  sudo \
  fzf \
  zsh \
  man-db \
  unzip \
  gnupg2 \
  gh \
  iptables \
  ipset \
  iproute2 \
  dnsutils \
  aggregate \
  jq \
  nano \
  vim \
  && mkdir -p /etc/apt/keyrings \
  && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
  && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_MAJOR}.x nodistro main" > /etc/apt/sources.list.d/nodesource.list \
  && apt-get update && apt-get install -y --no-install-recommends nodejs \
  && apt-get clean && rm -rf /var/lib/apt/lists/*

# Create non-root user
ARG USERNAME=developer
RUN useradd -m -s /bin/zsh $USERNAME \
  && echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/$USERNAME \
  && chmod 0440 /etc/sudoers.d/$USERNAME

# Persist bash history
RUN SNIPPET="export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \
  && mkdir /commandhistory \
  && touch /commandhistory/.bash_history \
  && chown -R $USERNAME /commandhistory

ENV DEVCONTAINER=true

# Create workspace and config directories
RUN mkdir -p /workspace<%= for dir <- @package_dirs do %> <%= dir %><% end %> \
  && chown -R $USERNAME:$USERNAME /workspace<%= for dir <- @package_dirs do %> <%= dir %><% end %>

WORKDIR /workspace

# Mark workspace as safe for git (ownership differs due to bind mount)
RUN git config --global --add safe.directory /workspace

# Set up non-root user
USER $USERNAME
<%= for {description, commands} <- @package_installs do %>
# Install <%= description %>
<%= for cmd <- commands do %>RUN <%= cmd %>
<% end %><% end %>
ENV PATH=$PATH:/home/$USERNAME/.local/bin:/home/$USERNAME/node_modules/.bin
ENV SHELL=/bin/zsh
ENV EDITOR=vim
ENV VISUAL=vim

# Install zsh with useful plugins
ARG ZSH_IN_DOCKER_VERSION=1.2.0
RUN sh -c "$(wget -O- https://github.com/deluan/zsh-in-docker/releases/download/v${ZSH_IN_DOCKER_VERSION}/zsh-in-docker.sh)" -- \
  -p git \
  -p fzf \
  -a "export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \
  -x

# Install Hex and Rebar
RUN mix local.hex --force && mix local.rebar --force

<%= if @firewall do %># Copy and set up firewall script
COPY init-firewall.sh /usr/local/bin/
USER root
RUN chmod +x /usr/local/bin/init-firewall.sh \
  && echo "$USERNAME ALL=(root) NOPASSWD: /usr/local/bin/init-firewall.sh" > /etc/sudoers.d/$USERNAME-firewall \
  && chmod 0440 /etc/sudoers.d/$USERNAME-firewall
USER $USERNAME
<% end %>
