#!/bin/bash

install_version=${1:-"3.7.5"}
install_name=$(echo ${install_version}|sed 's@\.@@g')
install_user=$(whoami)
os_type=$(cat /etc/os-release|grep "^ID="|awk -F'=' '{print $2}')

# Install dependency package
if [ "${install_user}" = "root" ]; then
    if [ "${os_type}" = "ubuntu" ]; then
        sudo apt install -y libffi-dev
        sudo apt install -y libssl-dev
        sudo apt install -y zlib1g-dev
        sudo apt install -y libbz2-dev
        sudo apt install -y libncurses5-dev
        sudo apt install -y libgdbm-dev
        sudo apt install -y liblzma-dev
        sudo apt install -y libreadline-dev
    elif [ "${os_type}" = '"euleros"' ] || [ "${os_type}" = '"centos"' ]; then
        sudo yum install -y libffi-devel
        sudo yum install -y zlib-devel
        sudo yum install -y bzip2-devel
        sudo yum install -y openssl-devel
        sudo yum install -y ncurses-devel
        sudo yum install -y sqlite-devel
        sudo yum install -y readline-devel
        sudo yum install -y tk-devel
        sudo yum install -y gdbm-devel
    fi
fi

# Get install path
if [ "${install_user}" = "root" ]; then
    install_path=/usr/local/python/python${install_name}
    pip_config_path=/root/.pip
else
    install_path=/home/${install_user}/tools/python/python${install_name}
    pip_config_path=/home/${install_user}/.pip
fi

# Download
cd /tmp
rm -f Python-${install_version}.tar.xz
wget https://tools.mindspore.cn/tools/build/python/python-${install_version}/Python-${install_version}.tar.xz
rm -rf Python-${install_version}
tar -xf Python-${install_version}.tar.xz

# Install
cd Python-${install_version}
./configure --prefix=${install_path} --enable-shared
make -j16
umask 022 && make install -j16
cd ..

# Set link
cd ${install_path}/bin
rm -f python
rm -f pip
ln -s python3.7 python
ln -s pip3.7 pip

# Set pip source
mkdir -p ${pip_config_path}
PIP_CONF=${pip_config_path}/pip.conf
echo "[global]" > ${PIP_CONF}
echo "index-url=https://repo.huaweicloud.com/repository/pypi/simple" >> ${PIP_CONF}
echo "trusted-host=repo.huaweicloud.com" >> ${PIP_CONF}
echo "timeout=120" >> ${PIP_CONF}
cat ${PIP_CONF}

# Set env
grep "${install_path}/bin" ~/env.sh
if [ $? -ne 0 ]; then
    if [ "${install_user}" = "root" ]; then
        echo "" >> ~/env.sh
        echo "# Python env" >> ~/env.sh
        echo "export LD_LIBRARY_PATH=${install_path}/lib:\$LD_LIBRARY_PATH" >> ~/env.sh
        echo "export PATH=${install_path}/bin:\$PATH" >> ~/env.sh
    else
        echo "" >> ~/.bashrc
        echo "# Python env" >> ~/.bashrc
        echo "export LD_LIBRARY_PATH=${install_path}/lib:\$LD_LIBRARY_PATH" >> ~/.bashrc
        echo "export PATH=${install_path}/bin:\$PATH" >> ~/.bashrc
    fi
fi

# Install package
export LD_LIBRARY_PATH=${install_path}/lib:$LD_LIBRARY_PATH
export PATH=${install_path}/bin:$PATH
umask 022 && pip3 install --upgrade pip
umask 022 && pip3 install wheel
umask 022 && pip3 install decorator
umask 022 && pip3 install pytest
umask 022 && pip3 install pytest-xdist
pip3 list

# Delete package
rm -rf /tmp/Python-${install_version}
rm -f /tmp/Python-${install_version}.tar.xz
