FROM ubuntu:22.04
# SHELL ["/bin/bash", "-c"]

LABEL email="liuyuqi<liuyuqi.gov@msn.cn>"
ENV key=value
WORKDIR /app

# apt change to aliyun
RUN sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
RUN sed -i s@/security.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list

RUN apt update && apt install -y curl wget

# COPY .cargo/config ~/.cargo/config
# COPY .cargo/credentials ~/.cargo/credentials

# install nodejs
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash
RUN apt install -y nodejs
RUN echo '\n\
    registry=http://registry.npm.taobao.org \n\
    sass_binary_site=https://npmmirror.com/mirrors/node-sass/ \n\
    electron_mirror=https://npmmirror.com/mirrors/electron/ \n\
    ELECTRON_BUILDER_BINARIES_MIRROR=https://npmmirror.com/mirrors/electron-builder-binaries/ \n\
    ' > ~/.npmrc
RUN npm install -g yarn pnpm serve

# install rust
# 安装必要依赖与开发工具
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list && \
    apt-get update && apt-get install -y \
    gcc-riscv64-unknown-elf gdb-multiarch dosfstools cmake \
    git wget python3 vim file curl \
    autoconf automake autotools-dev  libmpc-dev libmpfr-dev libgmp-dev \
    gawk build-essential bison flex texinfo gperf libtool patchutils bc \
    zlib1g-dev libexpat-dev \
    ninja-build pkg-config libglib2.0-dev libpixman-1-dev libsdl2-dev
# && rm -rf /var/lib/apt/lists/*

#tauri deps
RUN apt install -y libwebkit2gtk-4.0-dev \
    libssl-dev \
    libgtk-3-dev \
    libayatana-appindicator3-dev \
    librsvg2-dev

# 安装 QEMU
ARG QEMU_VERSION=7.0.0
RUN cd /tmp && \
    wget https://download.qemu.org/qemu-${QEMU_VERSION}.tar.xz && \
    tar xf qemu-${QEMU_VERSION}.tar.xz && \
    cd qemu-${QEMU_VERSION} && \
    ./configure --target-list=riscv64-softmmu,riscv64-linux-user && \
    make -j && \
    make install && \
    cd .. && \
    rm -rf qemu-${QEMU_VERSION} qemu-${QEMU_VERSION}.tar.xz

ARG RUST_VERSION=nightly
ENV RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
ENV RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

RUN mkdir -p ~/.cargo && \
    echo '[source.crates-io]' >> ~/.cargo/config && \
    echo 'replace-with = "aliyun"' >> ~/.cargo/config && \
    echo '[source.aliyun]' >> ~/.cargo/config && \
    echo 'registry = "sparse+https://mirrors.aliyun.com/crates.io-index/"' >> ~/.cargo/config && \
    echo '[source.ustc]' >> ~/.cargo/config && \
    echo 'registry = "https://mirrors.ustc.edu.cn/crates.io-index"' >> ~/.cargo/config && \
    echo '[source.sjtu]' >> ~/.cargo/config && \
    echo 'registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index/"' >> ~/.cargo/config && \
    echo '[source.tuna]' >> ~/.cargo/config && \
    echo 'registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"' >> ~/.cargo/config && \
    echo '[source.rustcc]' >> ~/.cargo/config && \
    echo 'registry = "https://code.aliyun.com/rustcc/crates.io-index.git"' >> ~/.cargo/config

# RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain stable && \
#     cargo install cargo-binutils && \
#     rustup component add llvm-tools-preview && \
#     rustup component add rust-src

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup.sh &&\
    sh rustup.sh -y --profile minimal &&\
    PATH=$PATH:/root/.cargo/bin &&\
    rustup target add riscv32i-unknown-none-elf &&\
    rustup component add llvm-tools-preview