# Use Ubuntu 18.04 as the base image
FROM ubuntu:18.04

# Update the package list and install basic development tools
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        build-essential \
        software-properties-common \
        wget \
        curl \
        git

# Install CMake with architecture detection
RUN apt-get update && \
    apt-get install -y --no-install-recommends cmake

# Alternative: Install specific CMake version with architecture detection
# RUN apt-get update && \
#     apt-get install -y --no-install-recommends wget && \
#     cd /opt && \
#     if [ "$(uname -m)" = "x86_64" ]; then \
#         wget https://github.com/Kitware/CMake/releases/download/v3.20.6/cmake-3.20.6-linux-x86_64.tar.gz && \
#         tar -zxvf cmake-3.20.6-linux-x86_64.tar.gz && \
#         ln -s /opt/cmake-3.20.6-linux-x86_64/bin/* /usr/local/bin/ && \
#         rm cmake-3.20.6-linux-x86_64.tar.gz; \
#     elif [ "$(uname -m)" = "aarch64" ]; then \
#         wget https://github.com/Kitware/CMake/releases/download/v3.20.6/cmake-3.20.6-linux-aarch64.tar.gz && \
#         tar -zxvf cmake-3.20.6-linux-aarch64.tar.gz && \
#         ln -s /opt/cmake-3.20.6-linux-aarch64/bin/* /usr/local/bin/ && \
#         rm cmake-3.20.6-linux-aarch64.tar.gz; \
#     else \
#         apt-get install -y cmake; \
#     fi

# Set the URL and installation path for the Linaro toolchain
ARG LINARO_URL="https://developer.arm.com/-/cdn-downloads/permalink/legacy-linaro-gnu-toolchains/6.3-2017.05/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu.tar.xz"
ARG TOOLCHAIN_PATH="/opt/linaro-toolchain"

# Create the installation path, download, and extract the Linaro toolchain
RUN mkdir -p ${TOOLCHAIN_PATH} && \
    wget -qO- ${LINARO_URL} | tar -xJ -C ${TOOLCHAIN_PATH} --strip-components=1

# Set environment variables to point to the toolchain directory
ENV ARM_CROSS_COMPILE_TOOLCHAIN=${TOOLCHAIN_PATH}
ENV PATH="${TOOLCHAIN_PATH}/bin:${PATH}"

# Clean temporary files to reduce image size
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set the working directory
WORKDIR /workspace

# Default to running Bash
CMD ["/bin/bash"]
