#!/bin/bash

# Example:
#    sh install_tools.sh -m on -s on -g on

##################
# Init Parameter #
##################
# Get input parameter
while getopts ':m:s:g:' opt; do
    case "${opt}" in
        m)
          MINDSPORE_MODE=${OPTARG}
          ;;
        s)
          SAULT_MODE=${OPTARG}
          ;;
        g)
          INSTALL_GCC=${OPTARG}
          ;;
        *)
          echo "[ERROR] Unknown option ${opt}."
          exit 1
          ;;
    esac
done

# OS info
OS_ARCH=$(arch)
OS_TYPE=$(cat /etc/os-release|grep "^ID="|awk -F'=' '{print $2}')
OS_VERSION=$(cat /etc/os-release|grep '^VERSION_ID='|awk -F'"' '{print $2}')

# Switch
INSTALL_PYTHON="on"
INSTALL_CMAKE="on"
INSTALL_NODEJS="off"
INSTALL_CCACHE="on"
INSTALL_SSHPASS="on"


##################
# Common Library #
##################
# Color
Red='\e[0;31m'          # Red
Green='\e[0;32m'        # Green
BRed='\e[1;31m'         # Red
BGreen='\e[1;32m'       # Green
BCyan='\e[1;36m'        # Cyan
Purple='\e[0;35m'       # Purple
BPurple='\e[1;35m'      # Bold Purple
Color_Off='\e[0m'       # Text Reset

# Log head
function LOG_HEAD() {
    local assert_msg=${1}
    echo -e "\n${BGreen}[INFO] ${assert_msg}${Color_Off}"
}

# Log info
function LOG_INFO() {
    local assert_msg=${1}
    echo -e "[INFO] ${assert_msg}"
}

# Log error
function LOG_ERROR() {
    local assert_msg=${1}
    echo -e "${BRed}[ERROR] ${assert_msg}${Color_Off}"
}


###############
# Install-lib #
###############
# Install dependency lib of pip package
if [ "${OS_ARCH}" = "aarch64" ]; then
    # Install hdf5
    LOG_HEAD "Install dependency lib(hdf5) of tables(pip)."
    cd /tmp
    rm -f hdf5-1.12.0.tar.gz
    wget http://tools.mindspore.cn/tools/ci/hdf5/hdf5-1.12.0/hdf5-1.12.0.tar.gz
    rm -rf hdf5-1.12.0
    tar -zxf hdf5-1.12.0.tar.gz
    cd /tmp/hdf5-1.12.0
    ./configure --prefix=/usr/local
    make -j16
    make install -j16
    cd ../
    rm -rf /tmp/hdf5-1.12.0
    rm -f /tmp/hdf5-1.12.0.tar.gz
fi


#################
# Install-tools #
#################
# Install gcc
if [ "${INSTALL_GCC}" = "on" ]; then
    LOG_HEAD "Install gcc."
    rm -rf /tmp/gcc
    mkdir -p /tmp/gcc
    cd /tmp/gcc
    wget http://tools.mindspore.cn/tools/build/gcc/gcc-7.3.0/source/gcc-7.3.0.tar.gz
    wget http://tools.mindspore.cn/tools/build/gcc/gcc-7.3.0/source/isl-0.16.1.tar.bz2
    wget http://tools.mindspore.cn/tools/build/gcc/gcc-7.3.0/source/gmp-6.1.0.tar.bz2
    wget http://tools.mindspore.cn/tools/build/gcc/gcc-7.3.0/source/mpfr-3.1.4.tar.gz
    wget http://tools.mindspore.cn/tools/build/gcc/gcc-7.3.0/source/mpc-1.0.3.tar.gz
    tar -zxf gcc-7.3.0.tar.gz
    tar -zxf mpc-1.0.3.tar.gz
    tar -zxf mpfr-3.1.4.tar.gz
    tar -jxf gmp-6.1.0.tar.bz2
    tar -jxf isl-0.16.1.tar.bz2
    mv gmp-6.1.0 gcc-7.3.0/gmp
    mv isl-0.16.1 gcc-7.3.0/isl
    mv mpc-1.0.3 gcc-7.3.0/mpc
    mv mpfr-3.1.4 gcc-7.3.0/mpfr
    mkdir -p /usr/local/gcc/gcc730
    cd /tmp/gcc/gcc-7.3.0
    mkdir build
    cd build
    ../configure --prefix=/usr/local/gcc/gcc730 -enable-checking=release -enable-languages=c,c++ -disable-multilib
    make -j32
    make install-strip
    chmod 755 -R /usr/local/gcc
    cd /usr/local/gcc/gcc730/bin
    ln -s gcc cc
    rm -rf /tmp/gcc
    grep "export PATH=/usr/local/gcc/gcc730/bin" ~/.bashrc
    if [ $? -ne 0 ]; then
        echo "" >> ~/.bashrc
        echo "# GCC env" >> ~/.bashrc
        echo "export PATH=/usr/local/gcc/gcc730/bin:\$PATH" >> ~/.bashrc
        echo "export LD_LIBRARY_PATH=/usr/local/gcc/gcc730/lib64:\$LD_LIBRARY_PATH" >> ~/.bashrc
        export LD_LIBRARY_PATH=/usr/local/gcc/gcc730/lib64:$LD_LIBRARY_PATH
        export PATH=/usr/local/gcc/gcc730/bin:$PATH
    fi
fi

# Install python
if [ "${INSTALL_PYTHON}" = "on" ]; then
    LOG_HEAD "Install python."
    PYTHON_PATH=/usr/local/python/python375
    cd /tmp
    rm -f /tmp/Python-3.7.5.tar.xz
    wget http://tools.mindspore.cn/tools/build/python/python-3.7.5/Python-3.7.5.tar.xz
    rm -rf /tmp/Python-3.7.5
    tar -xf Python-3.7.5.tar.xz
    cd /tmp/Python-3.7.5
    ./configure --prefix=${PYTHON_PATH} --enable-shared
    umask 022 && make -j16 && make install -j16
    ./configure --prefix=${PYTHON_PATH}
    umask 022 && make -j16 && make install -j16
    chmod 755 /usr/local/python
    cd ${PYTHON_PATH}/bin
    rm -f python
    rm -f pip
    ln -s python3.7 python
    ln -s pip3.7 pip
    rm -rf /tmp/Python-3.7.5
    rm -f /tmp/Python-3.7.5.tar.xz
    grep "export PATH=${PYTHON_PATH}/bin" ~/.bashrc
    if [ $? -ne 0 ]; then
        echo "" >> ~/.bashrc
        echo "# Python env" >> ~/.bashrc
        echo "export PATH=${PYTHON_PATH}/bin:\$PATH" >> ~/.bashrc
    fi
    
    # Set pip source
    LOG_HEAD "Set pip source."
    mkdir /root/.pip
    echo "[global]" > /root/.pip/pip.conf
    echo "index-url=https://mirrors.aliyun.com/pypi/simple" >> /root/.pip/pip.conf
    echo "trusted-host=mirrors.aliyun.com" >> /root/.pip/pip.conf
    echo "timeout=120" >> /root/.pip/pip.conf
    cat /root/.pip/pip.conf
fi

# Install pip package for sault
if [ "${SAULT_MODE}" = "on" ]; then
    LOG_HEAD "Install pip package for sault."
    export PATH=/usr/local/python/python375/bin:$PATH
    umask 022 && pip3 install --upgrade pip
    umask 022 && pip3 install paramiko
    umask 022 && pip3 install pyyaml
    umask 022 && pip3 install scp
    umask 022 && pip3 install flask
    umask 022 && pip3 install requests
    umask 022 && pip3 install flask_restful
    umask 022 && pip3 install argparse
    umask 022 && pip3 install pytest
    umask 022 && pip3 install virtualenv
    umask 022 && pip3 install nose
    pip3 list
fi

# Install pip package
if [ "${MINDSPORE_MODE}" = "on" ]; then
    LOG_HEAD "Install pip package of mindspore."
    export PATH=/usr/local/python/python375/bin:$PATH
    umask 022 && pip3 install 'wheel==0.34.2'
    umask 022 && pip3 install 'pybind11==2.5.0'
    umask 022 && pip3 install 'pytest==5.4.3'
    umask 022 && pip3 install 'pytest-xdist==1.32.0'
    umask 022 && pip3 install 'numpy>=1.17.0'
    umask 022 && pip3 install 'pillow>=6.2.0'
    umask 022 && pip3 install 'protobuf>=3.8.0'
    umask 022 && pip3 install 'asttokens>=1.1.13'
    umask 022 && pip3 install 'scipy==1.5.3'
    umask 022 && pip3 install 'easydict>=1.9'
    umask 022 && pip3 install 'sympy>=1.4'
    umask 022 && pip3 install 'cffi>=1.13.2'
    umask 022 && pip3 install 'decorator>=4.4.0'
    umask 022 && pip3 install 'astunparse>=1.6.3'
    umask 022 && pip3 install 'packaging>=20.0'
    umask 022 && pip3 install 'bs4'
    umask 022 && pip3 install 'matplotlib>=3.1.3'
    umask 022 && pip3 install 'sklearn>=0.0'
    umask 022 && pip3 install 'pandas>=1.0.4'
    umask 022 && pip3 install 'pycocotools>=2.0.0'
    umask 022 && pip3 install 'tables>=3.6.1'
    umask 022 && pip3 install 'attrdict>=2.0.1'
    umask 022 && pip3 install 'Click>=7.0'
    umask 022 && pip3 install 'Flask>=1.1.1'
    umask 022 && pip3 install 'Flask-Cors>=3.0.8'
    umask 022 && pip3 install 'google-pasta>=0.1.8'
    umask 022 && pip3 install 'gunicorn>=19.9.0'
    umask 022 && pip3 install 'itsdangerous>=1.1.0'
    umask 022 && pip3 install 'Jinja2>=2.10.1'
    umask 022 && pip3 install 'MarkupSafe>=1.1.1'
    umask 022 && pip3 install 'marshmallow>=2.19.2'
    umask 022 && pip3 install 'psutil>=5.6.1'
    umask 022 && pip3 install 'six>=1.12.0'
    umask 022 && pip3 install 'Werkzeug>=1.0.0'
    umask 022 && pip3 install 'tabulate>=0.8.6'
    umask 022 && pip3 install 'grpcio>=1.27.3'
    umask 022 && pip3 install 'treelib>=1.6.1'
    umask 022 && pip3 install 'scikit-learn>=0.21.2'
    umask 022 && pip3 install 'pyyaml>=5.3.1'
    umask 022 && pip3 install 'yapf>=0.30.0'
    
    if [ "${OS_ARCH}" = "x86_64" ]; then
        umask 022 && pip3 install 'opencv-python>=4.1.2.30'
        umask 022 && pip3 install 'mmcv==0.2.14'
        umask 022 && pip3 install 'torch==1.8.1'
        umask 022 && pip3 install 'torchvision==0.6.0'
    else
        # Get url
        if [ "${OS_TYPE}" = "\"euleros\"" ]; then
            PYPI_URL="http://tools.mindspore.cn/pypi/aarch64/euleros2_8"
        elif [ "${OS_TYPE}" = "ubuntu" ]; then
            PYPI_URL="http://tools.mindspore.cn/pypi/aarch64/ubuntu18_04_2"
        fi
        
        # Install
        if [ -n "${PYPI_URL}" ]; then
            # mmcv
            umask 022 && pip3 install ${PYPI_URL}/mmcv-0.2.14-cp37-cp37m-linux_aarch64.whl
            
            # opencv
            cd /usr/local/python/python375/lib/python3.7/site-packages
            rm -f cv2.cpython-37m-aarch64-linux-gnu.so
            wget ${PYPI_URL}/opencv-4.3.0/cv2.cpython-37m-aarch64-linux-gnu.so
            chmod 755 cv2.cpython-37m-aarch64-linux-gnu.so
            
            # torch
            umask 022 && pip3 install torch==1.8.1
            umask 022 && pip3 install 'torchvision==0.2.2.post3'
        fi
    fi
    
    pip3 list
fi

# Install cmake
if [ "${INSTALL_CMAKE}" = "on" ]; then
    LOG_HEAD "Install cmake."
    cd /tmp
    rm -f /tmp/cmake-3.18.3.tar.gz
    wget http://tools.mindspore.cn/tools/build/cmake/cmake-3.18.3/cmake-3.18.3.tar.gz
    rm -rf /tmp/cmake-3.18.3
    tar -zxf cmake-3.18.3.tar.gz
    cd /tmp/cmake-3.18.3
    ./bootstrap --prefix=/usr/local/cmake --parallel=16
    make -j16
    make install -j16
    cd ..
    chmod 755 -R /usr/local/cmake
    rm -rf /tmp/cmake-3.18.3
    rm -f /tmp/cmake-3.18.3.tar.gz
    grep "export PATH=/usr/local/cmake/bin" ~/.bashrc
    if [ $? -ne 0 ]; then
        echo "" >> ~/.bashrc
        echo "# Cmake env" >> ~/.bashrc
        echo "export PATH=/usr/local/cmake/bin:\$PATH" >> ~/.bashrc
    fi
fi

# Install nodejs
if [ "${INSTALL_NODEJS}" = "on" ]; then
    LOG_HEAD "Install nodejs."
    if [ "${OS_ARCH}" =  "aarch64" ]; then
        PACKAGE_NAME="node-v12.18.4-linux-arm64"
    else
        PACKAGE_NAME="node-v12.18.4-linux-x64"
    fi
    cd /tmp
    rm -f /tmp/${PACKAGE_NAME}.tar.gz
    wget https://mirrors.huaweicloud.com/nodejs/v12.18.4/${PACKAGE_NAME}.tar.gz
    rm -rf /tmp/${PACKAGE_NAME}
    tar -zxf ${PACKAGE_NAME}.tar.gz
    rm -rf /usr/local/nodejs
    mkdir -p /usr/local/nodejs
    mv /tmp/${PACKAGE_NAME} /usr/local/nodejs/
    chmod 755 -R /usr/local/nodejs
    ln -sf /usr/local/nodejs/${PACKAGE_NAME}/bin/node /usr/bin/node
    ln -sf /usr/local/nodejs/${PACKAGE_NAME}/bin/npm /usr/bin/npm
    npm config set registry https://mirrors.huaweicloud.com/repository/npm
    npm config get registry
    npm info express
    npm cache clean -f
    rm -f /tmp/${PACKAGE_NAME}.tar.gz
fi

# Install ccache
if [ "${INSTALL_CCACHE}" = "on" ]; then
    LOG_HEAD "Install ccache."
    cd /tmp
    rm -f /tmp/ccache-3.7.12.tar.gz
    wget http://tools.mindspore.cn/tools/build/ccache/ccache-3.7.12/ccache-3.7.12.tar.gz
    rm -rf /tmp/ccache-3.7.12
    tar -zxf ccache-3.7.12.tar.gz
    cd /tmp/ccache-3.7.12
    ./configure --prefix=/usr/local/ccache
    make -j16
    make install -j16
    cd ..
    chmod 755 -R /usr/local/ccache
    rm -f /usr/local/bin/gcc
    rm -f /usr/local/bin/g++
    rm -f /usr/local/bin/cc
    rm -f /usr/local/bin/c++
    ln -s /usr/local/ccache/bin/ccache /usr/local/bin/gcc
    ln -s /usr/local/ccache/bin/ccache /usr/local/bin/g++
    ln -s /usr/local/ccache/bin/ccache /usr/local/bin/cc
    ln -s /usr/local/ccache/bin/ccache /usr/local/bin/c++
    /usr/local/ccache/bin/ccache -C
    /usr/local/ccache/bin/ccache --max-size=8
    /usr/local/ccache/bin/ccache -p
    rm -rf /tmp/ccache-3.7.12
    rm -f /tmp/ccache-3.7.12.tar.gz
fi

# Install sshpass
if [ "${INSTALL_SSHPASS}" = "on" ]; then
    LOG_HEAD "Install sshpass."
    if [ "${OS_TYPE}" = '"euleros"' ]; then
        cd /tmp
        rm -f sshpass-1.06.tar.gz
        wget http://tools.mindspore.cn/tools/ci/sshpass/sshpass-1.06/sshpass-1.06.tar.gz
        rm -rf sshpass-1.06
        tar -zxf sshpass-1.06.tar.gz
        cd /tmp/sshpass-1.06
        ./configure
        make -j16
        make install
        cd ..
        rm -rf /tmp/sshpass-1.06
        rm -f /tmp/sshpass-1.06.tar.gz
    elif [ "${OS_TYPE}" = 'ubuntu' ]; then
        apt install -y sshpass
    fi
fi
