FROM debian:bookworm
# FROM rustembedded/cross:armv7-unknown-linux-gnueabihf

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"
#ENV PKG_CONFIG_ALLOW_CROSS 1
ENV PKG_CONFIG_PATH /usr/lib/arm-linux-gnueabihf/pkgconfig/

####
# 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 armhf
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 libc6-armhf-cross libc6-dev-armhf-cross gcc-arm-linux-gnueabihf
RUN apt-get install -y libdbus-1-dev libdbus-1-dev:armhf
RUN apt-get install -y libasound2-dev:armhf openssl:armhf libssl-dev:armhf
RUN apt-get autoremove -y
RUN apt-get clean -y

# RUN dpkg --add-architecture armhf
# RUN apt-get update
# RUN apt-get -y install curl git sudo nano
# RUN apt-get install -y curl git sudo nano pkg-config
# RUN apt-get install -y build-essential
# RUN apt-get install libasound2-dev:armhf openssl:armhf libssl-dev:armhf -y
# RUN apt-get autoremove -y
# RUN apt-get clean -y

#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
RUN useradd --home-dir /home/mgt --groups sudo --shell /bin/bash --create-home mgt
RUN echo mgt:12345678a@ | chpasswd
#    usermod  -aG sudo mgt && \
RUN chown mgt:mgt -R /home/mgt

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

USER mgt

RUN mkdir -p /home/mgt/.cargo && \
    echo '[target.armv7-unknown-linux-gnueabihf]' >> /home/mgt/.cargo/config.toml && \
    echo 'linker = "arm-linux-gnueabihf-gcc"' >> /home/mgt/.cargo/config.toml && \
    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 armv7-unknown-linux-gnueabihf && \
    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 armv7-unknown-linux-gnueabihf &' >> Rust_Env_CC_Preps.sh && \
    echo 'PKG_CONFIG_PATH="/usr/lib/arm-linux-gnueabihf/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'
