# Multi-Architecture Cross-Compilation Docker Image for Nushell Plugins
# Based on Ubuntu 22.04 LTS for stability and broad compatibility

FROM ubuntu:22.04

# Prevent interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Set timezone to UTC
ENV TZ=UTC

# Install system dependencies and cross-compilation toolchains
RUN apt-get update && \
    apt-get install -y \
        # Build essentials
        build-essential \
        curl \
        wget \
        git \
        pkg-config \
        # SSL and crypto libraries
        libssl-dev \
        # Cross-compilation toolchains
        gcc-x86-64-linux-gnu \
        gcc-aarch64-linux-gnu \
        gcc-arm-linux-gnueabihf \
        # Additional libraries for nushell plugins
        libc6-dev-amd64-cross \
        libc6-dev-arm64-cross \
        libc6-dev-armhf-cross \
        # For clipboard plugin
        libx11-dev \
        libxcb1-dev \
        libxcb-render0-dev \
        libxcb-shape0-dev \
        libxcb-xfixes0-dev \
        # For image processing
        libpng-dev \
        libjpeg-dev \
        # General development tools
        unzip \
        zip \
        tar \
        gzip \
        file \
        # Clean up to reduce image size
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

# Install Rust with rustup
ENV RUST_VERSION=1.75.0
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${RUST_VERSION}

# Add Rust to PATH
ENV PATH="/root/.cargo/bin:$PATH"

# Install cross-compilation targets
RUN rustup target add \
    x86_64-unknown-linux-gnu \
    aarch64-unknown-linux-gnu \
    armv7-unknown-linux-gnueabihf \
    x86_64-pc-windows-gnu \
    x86_64-pc-windows-msvc \
    aarch64-pc-windows-msvc

# Install additional Rust components
RUN rustup component add \
    rustfmt \
    clippy \
    rust-src

# Install cargo tools useful for cross-compilation
RUN cargo install \
    cross \
    cargo-audit \
    cargo-outdated \
    cargo-edit

# Set up cross-compilation environment variables and linkers
ENV CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc
ENV CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++
ENV AR_x86_64_unknown_linux_gnu=x86_64-linux-gnu-ar
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc

ENV CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc
ENV CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++
ENV AR_aarch64_unknown_linux_gnu=aarch64-linux-gnu-ar
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc

ENV CC_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-gcc
ENV CXX_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-g++
ENV AR_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-ar
ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc

# Set up pkg-config for cross-compilation
ENV PKG_CONFIG_ALLOW_CROSS=1
ENV PKG_CONFIG_PATH_x86_64_unknown_linux_gnu=/usr/lib/x86_64-linux-gnu/pkgconfig
ENV PKG_CONFIG_PATH_aarch64_unknown_linux_gnu=/usr/lib/aarch64-linux-gnu/pkgconfig
ENV PKG_CONFIG_PATH_armv7_unknown_linux_gnueabihf=/usr/lib/arm-linux-gnueabihf/pkgconfig

# Install Windows cross-compilation dependencies
RUN apt-get update && \
    apt-get install -y \
        mingw-w64 \
        wine \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

# Set up Windows cross-compilation
ENV CC_x86_64_pc_windows_gnu=x86_64-w64-mingw32-gcc
ENV CXX_x86_64_pc_windows_gnu=x86_64-w64-mingw32-g++
ENV AR_x86_64_pc_windows_gnu=x86_64-w64-mingw32-ar
ENV CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=x86_64-w64-mingw32-gcc

# Create workspace directory
WORKDIR /workspace

# Set up cargo configuration for cross-compilation
RUN mkdir -p /root/.cargo && \
    echo '[target.x86_64-unknown-linux-gnu]' >> /root/.cargo/config.toml && \
    echo 'linker = "x86_64-linux-gnu-gcc"' >> /root/.cargo/config.toml && \
    echo '' >> /root/.cargo/config.toml && \
    echo '[target.aarch64-unknown-linux-gnu]' >> /root/.cargo/config.toml && \
    echo 'linker = "aarch64-linux-gnu-gcc"' >> /root/.cargo/config.toml && \
    echo '' >> /root/.cargo/config.toml && \
    echo '[target.armv7-unknown-linux-gnueabihf]' >> /root/.cargo/config.toml && \
    echo 'linker = "arm-linux-gnueabihf-gcc"' >> /root/.cargo/config.toml && \
    echo '' >> /root/.cargo/config.toml && \
    echo '[target.x86_64-pc-windows-gnu]' >> /root/.cargo/config.toml && \
    echo 'linker = "x86_64-w64-mingw32-gcc"' >> /root/.cargo/config.toml

# Set default environment variables for better build experience
ENV RUST_BACKTRACE=1
ENV CARGO_INCREMENTAL=0
ENV CARGO_NET_RETRY=10
ENV CARGO_NET_TIMEOUT=30

# Create a build script that can be used as entry point
RUN echo '#!/bin/bash' > /usr/local/bin/build-plugin.sh && \
    echo 'set -e' >> /usr/local/bin/build-plugin.sh && \
    echo 'if [ -z "$1" ]; then' >> /usr/local/bin/build-plugin.sh && \
    echo '    echo "Usage: build-plugin.sh <target> [plugin_dir]"' >> /usr/local/bin/build-plugin.sh && \
    echo '    echo "Available targets:"' >> /usr/local/bin/build-plugin.sh && \
    echo '    rustup target list --installed' >> /usr/local/bin/build-plugin.sh && \
    echo '    exit 1' >> /usr/local/bin/build-plugin.sh && \
    echo 'fi' >> /usr/local/bin/build-plugin.sh && \
    echo 'TARGET=$1' >> /usr/local/bin/build-plugin.sh && \
    echo 'PLUGIN_DIR=${2:-.}' >> /usr/local/bin/build-plugin.sh && \
    echo 'echo "Building $PLUGIN_DIR for target $TARGET..."' >> /usr/local/bin/build-plugin.sh && \
    echo 'cd "$PLUGIN_DIR"' >> /usr/local/bin/build-plugin.sh && \
    echo 'cargo build --release --target "$TARGET"' >> /usr/local/bin/build-plugin.sh && \
    echo 'echo "Build completed successfully!"' >> /usr/local/bin/build-plugin.sh && \
    chmod +x /usr/local/bin/build-plugin.sh

# Create a helper script to list available targets
RUN echo '#!/bin/bash' > /usr/local/bin/list-targets.sh && \
    echo 'echo "Installed Rust targets:"' >> /usr/local/bin/list-targets.sh && \
    echo 'rustup target list --installed' >> /usr/local/bin/list-targets.sh && \
    echo '' >> /usr/local/bin/list-targets.sh && \
    echo 'echo "Available cross-compilation linkers:"' >> /usr/local/bin/list-targets.sh && \
    echo 'ls -1 /usr/bin/*-gcc | grep -E "(x86_64|aarch64|arm)-.*-gcc$" || true' >> /usr/local/bin/list-targets.sh && \
    chmod +x /usr/local/bin/list-targets.sh

# Create a helper script for environment information
RUN echo '#!/bin/bash' > /usr/local/bin/env-info.sh && \
    echo 'echo "=== Rust Environment ==="' >> /usr/local/bin/env-info.sh && \
    echo 'rustc --version' >> /usr/local/bin/env-info.sh && \
    echo 'cargo --version' >> /usr/local/bin/env-info.sh && \
    echo '' >> /usr/local/bin/env-info.sh && \
    echo 'echo "=== Installed Targets ==="' >> /usr/local/bin/env-info.sh && \
    echo 'rustup target list --installed' >> /usr/local/bin/env-info.sh && \
    echo '' >> /usr/local/bin/env-info.sh && \
    echo 'echo "=== Cross-Compilation Toolchains ==="' >> /usr/local/bin/env-info.sh && \
    echo 'echo "GCC versions:"' >> /usr/local/bin/env-info.sh && \
    echo 'x86_64-linux-gnu-gcc --version 2>/dev/null | head -1 || echo "x86_64-linux-gnu-gcc: not found"' >> /usr/local/bin/env-info.sh && \
    echo 'aarch64-linux-gnu-gcc --version 2>/dev/null | head -1 || echo "aarch64-linux-gnu-gcc: not found"' >> /usr/local/bin/env-info.sh && \
    echo 'arm-linux-gnueabihf-gcc --version 2>/dev/null | head -1 || echo "arm-linux-gnueabihf-gcc: not found"' >> /usr/local/bin/env-info.sh && \
    echo 'x86_64-w64-mingw32-gcc --version 2>/dev/null | head -1 || echo "x86_64-w64-mingw32-gcc: not found"' >> /usr/local/bin/env-info.sh && \
    echo '' >> /usr/local/bin/env-info.sh && \
    echo 'echo "=== Environment Variables ==="' >> /usr/local/bin/env-info.sh && \
    echo 'env | grep -E "(CC_|CXX_|AR_|CARGO_TARGET_|PKG_CONFIG)" | sort' >> /usr/local/bin/env-info.sh && \
    chmod +x /usr/local/bin/env-info.sh

# Add helpful aliases to bashrc
RUN echo 'alias ll="ls -la"' >> /root/.bashrc && \
    echo 'alias targets="list-targets.sh"' >> /root/.bashrc && \
    echo 'alias envinfo="env-info.sh"' >> /root/.bashrc && \
    echo 'alias build="build-plugin.sh"' >> /root/.bashrc

# Verify installation and show information
RUN echo "=== Docker Image Build Complete ===" && \
    echo "Rust version:" && rustc --version && \
    echo "Cargo version:" && cargo --version && \
    echo "Installed targets:" && rustup target list --installed && \
    echo "Cross-compilation toolchains:" && \
    (x86_64-linux-gnu-gcc --version | head -1) && \
    (aarch64-linux-gnu-gcc --version | head -1) && \
    (arm-linux-gnueabihf-gcc --version | head -1) && \
    (x86_64-w64-mingw32-gcc --version | head -1) && \
    echo "=== Ready for cross-compilation! ==="

# Set default command to show environment info
CMD ["env-info.sh"]

# Metadata labels
LABEL maintainer="Nushell Plugins Team"
LABEL description="Cross-compilation environment for Nushell plugins"
LABEL version="1.0.0"
LABEL rust.version="1.75.0"
LABEL targets="x86_64-unknown-linux-gnu,aarch64-unknown-linux-gnu,armv7-unknown-linux-gnueabihf,x86_64-pc-windows-gnu"