FROM openjdk:11 as builder

ENV MAVEN_VERSION=3.9.5 \
    GRADLE_VERSION=7.5
ARG FLUTTER_VERSION="3.13.5"

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

RUN apt-get update && apt-get install -y \
    curl zip unzip wget git vim

RUN apt install build-essential -y

# install java 21.0.1-ms
RUN curl -s "https://get.sdkman.io" | bash
RUN bash -c "source $HOME/.sdkman/bin/sdkman-init.sh && sdk install java 21.0.1-ms"
ENV JAVA_HOME /root/.sdkman/candidates/java/21.0.1-ms

# install gradle
RUN wget https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip -P /tmp
RUN unzip -d /opt/gradle /tmp/gradle-*.zip && rm -rf /tmp/gradle-*.zip
ENV GRADLE_HOME=/opt/gradle/gradle-${GRADLE_VERSION}
ENV PATH=$PATH:$GRADLE_HOME/bin

# install maven
RUN wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.zip -P /tmp
RUN unzip -d /opt/maven /tmp/apache-maven-*.zip
ENV MAVEN_HOME=/opt/maven/apache-maven-${MAVEN_VERSION}
ENV PATH=$PATH:$MAVEN_HOME/bin

# install android sdk
ENV ANDROID_HOME=/opt/androidsdk \
    FLUTTER_HOME=/opt/flutter

RUN wget https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip \
    && mkdir -p $ANDROID_HOME/cmdline-tools/latest \
    && unzip commandlinetools-linux-*.zip -d $ANDROID_HOME \
    && rm -f commandlinetools-linux-*.zip \
    && mv $ANDROID_HOME/cmdline-tools/bin $ANDROID_HOME/cmdline-tools/latest \
    && mv $ANDROID_HOME/cmdline-tools/lib $ANDROID_HOME/cmdline-tools/latest

RUN echo "export ANDROID_HOME=$ANDROID_HOME" >> ~/.bashrc \
    && echo 'export PATH=$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$ANDROID_HOME/cmdline-tools/bin:$ANDROID_HOME/platform-tools:$PATH' >> ~/.bashrc

ENV PATH=$PATH:$FLUTTER_HOME/bin:${ANDROID_HOME}/cmdline-tools/latest/bin:${ANDROID_HOME}/platform-tools:${ANDROID_HOME}/tools:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/emulator
RUN yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --verbose "platform-tools" "platforms;android-33"

ENV PUB_HOSTED_URL=https://pub.flutter-io.cn
ENV FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

# install flutter
RUN git clone --branch ${FLUTTER_VERSION} https://github.com/flutter/flutter.git ${FLUTTER_HOME} \
    && cd ${FLUTTER_HOME} \
    && ./bin/flutter precache \
    && ./bin/flutter doctor

# install python 3.12.1
RUN mkdir -p ~/.pip
RUN echo '\n\
    [global] \n\
    trusted-host=mirrors.aliyun.com \n\
    index-url=https://mirrors.aliyun.com/pypi/simple/ \n\
    ' > ~/.pip/pip.conf

# RUN apt-get install -y software-properties-common
# RUN add-apt-repository -y ppa:deadsnakes/ppa
# RUN apt update
# RUN apt-get install -y python3.12 python3.12-distutils
# RUN apt install make
RUN \
    apt-get update && \
    apt-get install -y python3 python3-dev python-dev python-pip python3-pip python-virtualenv && \
    rm -rf /var/lib/apt/lists/*
# RUN wget http://npm.taobao.org/mirrors/python/3.12.1/Python-3.12.1.tgz
# RUN tar -zxf Python-3.12.1.tgz && cd Python-3.12.1 && \
#     mkdir -p /opt/python/3.12.1 && \
#     ./configure --prefix=/opt/python/3.12.1 --enable-shared --with-ssl --enable-optimizations && \
#     make && make install && \
#     cd .. && rm -rf Python-3.12.1*
# RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3

# RUN curl http://fileshare.yoqi.me/d/DevFiles/Develop/Python/get-pip.py?sign=17C5fjx9l5VpBJ6FCHonje5nEEkylSINsPlKsASVkRc=:1702263152 -o get-pip.py && \
#     python3 get-pip.py && \
#     rm get-pip.py

# RUN ln -s /usr/bin/python3.9 /usr/bin/python && \
#     ln -s /usr/bin/pip3 /usr/bin/pip

# install R
RUN apt-get update && apt-get install -y \
    r-base r-base-dev

# install ruby
RUN apt-get update && apt-get install -y \
    ruby-full rubygems ruby-dev

# install rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y

# install .net core 6.0
# RUN wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \
#     dpkg -i packages-microsoft-prod.deb && \
#     rm packages-microsoft-prod.deb && \
#     apt-get update && \
#     apt-get install -y apt-transport-https && \
#     apt-get update && \
#     apt-get install -y dotnet-sdk-6.0

# install go1.21
RUN wget https://go.dev/dl/go1.21.4.linux-amd64.tar.gz && \
    tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz && \
    rm go1.21.4.linux-amd64.tar.gz
ENV PATH=$PATH:/usr/local/go/bin
RUN export GOPROXY="https://goproxy.io,direct"

# install nodejs20
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
RUN apt-get install -y nodejs
RUN npm config set registry http://registry.npm.taobao.org --global
# RUN npm config set ELECTRON_MIRROR https://npmmirror.com/mirrors/electron/ --global
# RUN npm config set ELECTRON_BUILDER_BINARIES_MIRROR https://npmmirror.com/mirrors/electron-builder-binaries/ --global
RUN npm install -g yarn pnpm

LABEL email="liuyuqi<liuyuqi.gov@msn.cn>"
WORKDIR /workspaces

RUN apt-get install -y openssh-server
RUN rm -rf /var/lib/apt/lists/*

# 指定字符集支持命令行输入中文(根据需要选择字符集)
ENV LANG C.UTF-8
ENV LANGUAGE C.UTF-8

# set path
RUN echo '\n\
    export PATH=$PATH:$GRADLE_HOME/bin \n\
    export JAVA_HOME=/usr/local/openjdk-11 \n\
    export PATH=$PATH:$JAVA_HOME/bin \n\
    export PATH=$PATH:$MAVEN_HOME/bin \n\
    export PATH=$PATH:/usr/local/go/bin \n\
    export GOPROXY="https://goproxy.io,direct" \n\
    ' >> ~/.bashrc

# install vscode and extension
RUN curl -fsSL https://code-server.dev/install.sh | sh &&\
    code-server --install-extension redhat.vscode-yaml &&\
    code-server --install-extension orta.vscode-jest &&\
    code-server --install-extension dbaeumer.vscode-eslint &&\
    code-server --install-extension ms-python.python &&\
    code-server --install-extension ms-python.vscode-pylance &&\
    code-server --install-extension ms-azuretools.vscode-docker &&\
    code-server --install-extension mhutchie.git-graph &&\
    code-server --install-extension ardisaurus.gitflow-actions-sidebar &&\
    code-server --install-extension Dart-Code.flutter &&\
    code-server --install-extension golang.go &&\
    code-server --install-extension Vue.volar &&\
    code-server --install-extension eamodio.gitlens &&\
    code-server --install-extension REditorSupport.r &&\
    code-server --install-extension vscjava.vscode-java-pack &&\
    code-server --install-extension vscjava.vscode-java-debug &&\
    code-server --install-extension vscjava.vscode-java-dependency &&\
    code-server --install-extension vscjava.vscode-maven &&\
    code-server --install-extension vscjava.vscode-spring-initializr &&\
    echo done

# code-server --install-extension ms-dotnettools.csharp &&\

VOLUME [ "/workspaces" ]

CMD [ "/bin/bash" ]