FROM remnux/remnux-distro:latest
LABEL maintainer="tabledevil"

USER root
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Berlin

# Install additional system packages that REMnux doesn't include
RUN apt-get update && apt-get install -y \
    busybox \
    bat \
    catdoc \
    docx2txt \
    fd-find \
    fish \
    fzf \
    mc \
    pipx \
    ripgrep \
    unrtf \
    mpack \
    pandoc \
    zsh \
    zsh-autosuggestions \
    zsh-syntax-highlighting \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Configure pip
ENV PYTHONDONTWRITEBYTECODE=1
ADD pip.conf /etc/pip.conf

# Install unfurl & dependencies via pipx (for URL analysis)
RUN PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin pipx install --include-deps dfir-unfurl \
    && PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin pipx inject dfir-unfurl requests six maclookup

# Install visidata via pipx (for data exploration)
RUN PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin pipx install --include-deps visidata

# Install offline help and cheat sheet tools
RUN PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin pipx install --include-deps cheat \
    && PIPX_HOME=/opt/pipx PIPX_BIN_DIR=/usr/local/bin pipx install --include-deps tldr

# Install navi (interactive cheatsheet browser, Ctrl+G)
RUN curl -sL https://raw.githubusercontent.com/denisidoro/navi/master/scripts/install | BIN_DIR=/usr/local/bin bash 2>/dev/null \
    || echo "navi installation skipped (offline build)"

# Install zk (wiki browser with [[wiki-links]], fzf search, backlinks)
RUN ZK_URL=$(curl -sL https://api.github.com/repos/zk-org/zk/releases/latest \
    | grep -o '"browser_download_url": "[^"]*linux-amd64[^"]*"' \
    | head -1 | cut -d'"' -f4) \
    && if [ -n "$ZK_URL" ]; then curl -sL "$ZK_URL" | tar xz -C /usr/local/bin/; fi \
    || echo "zk installation skipped (offline build)"

# Create data directory and set permissions
RUN mkdir -p /data \
    && chown remnux:remnux /data

# Add documentation and shell configuration
ADD files/README /opt/README
ADD files/welcome.sh /usr/local/bin/welcome.sh
ADD files/command_help /opt/command_help
ADD files/zshrc /etc/zsh/zshrc
ADD files/fish_config.fish /etc/fish/conf.d/remnux.fish
RUN chmod +x /usr/local/bin/welcome.sh

# Add help system scripts
ADD scripts/find-tool /usr/local/bin/find-tool
ADD scripts/fhelp /usr/local/bin/fhelp
ADD scripts/check-help-coverage.sh /usr/local/bin/check-help-coverage.sh
RUN chmod +x /usr/local/bin/find-tool /usr/local/bin/fhelp /usr/local/bin/check-help-coverage.sh

# Install pre-generated help artifacts (built via: make generate-master)
ADD data/generated/tools.db /opt/remnux-docs/tools.db
ADD data/generated/cheatsheets/ /opt/cheatsheets/personal/
ADD data/generated/workflows/ /opt/remnux-docs/workflows/
ADD cheatsheets/ /opt/cheatsheets/
ADD data/generated/wiki/ /opt/wiki/

# Install legacy help scripts (kept for compatibility)
ADD scripts/create-offline-help-system.sh /usr/local/bin/create-offline-help-system.sh
ADD scripts/import-remnux-cheatsheets.sh /usr/local/bin/import-remnux-cheatsheets.sh
ADD scripts/convert-remnux-cheats.py /usr/local/bin/convert-remnux-cheats.py
ADD scripts/add-tool-cheats.sh /usr/local/bin/add-tool-cheats.sh
RUN chmod +x /usr/local/bin/create-offline-help-system.sh /usr/local/bin/import-remnux-cheatsheets.sh \
    && chmod +x /usr/local/bin/convert-remnux-cheats.py /usr/local/bin/add-tool-cheats.sh

# Install Oh My Zsh for remnux user (minimal installation)
RUN su - remnux -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended' || true \
    && rm -rf /home/remnux/.oh-my-zsh/.git \
    && find /home/remnux/.oh-my-zsh/plugins -mindepth 1 -maxdepth 1 -type d ! -name git ! -name docker ! -name command-not-found ! -name colored-man-pages -exec rm -rf {} + 2>/dev/null || true \
    && rm -rf /home/remnux/.cache/* /tmp/* 2>/dev/null || true

# Configure navi cheatsheet path
RUN mkdir -p /home/remnux/.local/share/navi/cheats \
    && ln -sf /opt/cheatsheets/personal /home/remnux/.local/share/navi/cheats/remnux \
    && chown -R remnux:remnux /home/remnux/.local/share/navi 2>/dev/null || true

# Configure cheat tool — Python cheat v2.x uses DEFAULT_CHEAT_DIR env var
# Create cheat-compatible copies (without .cheat extension) in the cheat directory
RUN mkdir -p /home/remnux/.cheat \
    && for f in /opt/cheatsheets/personal/*.cheat; do \
        base=$(basename "$f" .cheat); \
        cp "$f" "/home/remnux/.cheat/$base"; \
    done \
    && chown -R remnux:remnux /home/remnux/.cheat
ENV DEFAULT_CHEAT_DIR=/home/remnux/.cheat

# Configure bash welcome (login shells only)
RUN echo '# Welcome message (login shells only)' >> /etc/bash.bashrc \
    && echo 'if [ -z "$_WELCOME_SHOWN" ] && [ -f /usr/local/bin/welcome.sh ]; then' >> /etc/bash.bashrc \
    && echo '    source /usr/local/bin/welcome.sh' >> /etc/bash.bashrc \
    && echo 'fi' >> /etc/bash.bashrc \
    && echo 'alias analyse="fhelp"' >> /etc/bash.bashrc \
    && echo 'alias h="fhelp"' >> /etc/bash.bashrc \
    && echo 'alias ?="fhelp"' >> /etc/bash.bashrc \
    && echo '# Navi widget for bash (Ctrl+G)' >> /etc/bash.bashrc \
    && echo 'command -v navi >/dev/null 2>&1 && eval "$(navi widget bash)" 2>/dev/null' >> /etc/bash.bashrc \
    && rm -rf /var/cache/* /tmp/* /var/tmp/* /home/remnux/.cache/* /root/.cache/* 2>/dev/null || true

# Switch to remnux user (REMnux default user)
USER remnux
ENV LANG=en_US.UTF-8
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/remnux/.local/bin
WORKDIR /data

CMD ["/bin/bash"]
