FROM debian:bullseye

### FROM debian:bookworm ### to build from bookworm image (for newer version of libs/tools)

ENV CARGO_HOME=/home/mgt/.cargo \
    PATH=/home/mgt/.cargo/bin:$PATH \
    USER=mgt \
    PKG_CONFIG_PATH="/usr/lib/aarch64-linux-gnu/pkgconfig/" \
    PKG_CONFIG_ALLOW_CROSS="true"

####
# Setup base and tools/libs for AVDSP armhf cross compiling
# Rustc/Cargo can be installed here but moved to non-root user section
####

RUN dpkg --add-architecture arm64
RUN apt-get update
RUN apt-get install -y curl git sudo nano pkg-config
RUN apt-get install -y build-essential
RUN apt-get install -y gcc-aarch64-linux-gnu
RUN apt-get install -y libc6-arm64-cross libc6-dev-arm64-cross
RUN apt-get install -y libdbus-1-dev
RUN apt-get install -y libasound2-dev:arm64 openssl:arm64 libssl-dev:arm64
RUN apt-get autoremove -y
RUN apt-get clean

#ENV CARGO_HOME=/home/mgt/.cargo \
#    PATH=/home/mgt/.cargo/bin:$PATH \
#    USER=mgt \
#    PKG_CONFIG_PATH="/usr/lib/arm-linux-gnueabihf/pkgconfig" \
#    PKG_CONFIG_ALLOW_CROSS="true"

####
# Add new user with sudo permission and default pass 12345678a@
####

RUN dpkg --add-architecture armhf && \
    useradd --home-dir /home/mgt --groups sudo --shell /bin/bash --create-home mgt && \
    echo mgt:12345678a@ | chpasswd && \
#    usermod  -aG sudo mgt && \
    chown mgt:mgt -R /home/mgt

####
# Change to newly created user to proceed
####

USER mgt

RUN mkdir -p /home/mgt/.cargo && \
    echo '[target.aarch64-unknown-linux-gnu]' >> /home/mgt/.cargo/config && \
    echo 'linker = "aarch64-linux-gnu-gcc"' >> /home/mgt/.cargo/config && \
    cd /home/mgt && \
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup.sh && \
    chmod +x rustup.sh && \
    ./rustup.sh -y && \
    export source="/home/mgt/.cargo/env" && \
    rustup default stable && \
    rustup target add x86_64-unknown-linux-gnu && \
    rustup target add aarch64-unknown-linux-gnu && \
    rm rustup.sh && \
    echo 'sudo dpkg --add-architecture armhf' >> Rust_Env_CC_Preps.sh && \
    echo 'CARGO_HOME=/home/mgt/.cargo' >> Rust_Env_CC_Preps.sh && \
    echo 'PATH=/home/mgt/.cargo/bin:$PATH' >> Rust_Env_CC_Preps.sh && \
    echo 'USER=mgt' >> Rust_Env_CC_Preps.sh && \
    echo 'source "/home/mgt/.cargo/env" &' >> Rust_Env_CC_Preps.sh && \
    echo '#rustup default stable &' >> Rust_Env_CC_Preps.sh && \
    echo '#rustup target add x86_64-unknown-linux-gnu &' >> Rust_Env_CC_Preps.sh && \
    echo '#rustup target add aarch64-unknown-linux-gnu &' >> Rust_Env_CC_Preps.sh && \
    echo 'PKG_CONFIG_PATH="/usr/lib/aarch64-linux-gnu/pkgconfig/"' >> Rust_Env_CC_Preps.sh && \
    echo 'PKG_CONFIG_ALLOW_CROSS="true"' >> Rust_Env_CC_Preps.sh && \
    chown mgt:mgt -R /home/mgt && \
#    echo 'Work till here' && \
    echo 'Last instruction completed! Plaese wait for finalizing image'
