#!/bin/bash

# Example:
#    Usage: bash install_sysconfig.sh -h ms01
#    Options: 
#        -h: hostname

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

# OS info
OS_ARCH=$(arch)


##################
# 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 Library #
###################
# Set yum source
LOG_HEAD "Set yum source."
repo_path=/etc/yum.repos.d/euleros.repo
rm -f /etc/yum.repos.d/euleros*.repo
if [ "${OS_ARCH}" = "aarch64" ]; then
    echo "[base]" > ${repo_path}
    echo "name=EulerOS-2.0SP8 base" >> ${repo_path}
    echo "baseurl=http://tools.mindspore.cn/productrepo/repo/euleros/aarch64/EulerOS_Server_V200R008C00SPC300B630/" >> ${repo_path}
    echo "enabled=1" >> ${repo_path}
    echo "gpgcheck=1" >> ${repo_path}
    echo "gpgkey=http://tools.mindspore.cn/productrepo/repo/euleros/aarch64/EulerOS_Server_V200R008C00SPC300B630/RPM-GPG-KEY-EulerOS" >> ${repo_path}
else
    echo "[base]" > ${repo_path}
    echo "name=EulerOS-2.0SP5 base" >> ${repo_path}
    echo "baseurl=http://tools.mindspore.cn/productrepo/repo/euleros/x86_64/EulerOS_V200R007C00SPC522B030/" >> ${repo_path}
    echo "enabled=1" >> ${repo_path}
    echo "gpgcheck=1" >> ${repo_path}
    echo "gpgkey=http://tools.mindspore.cn/productrepo/repo/euleros/x86_64/EulerOS_V200R007C00SPC522B030/RPM-GPG-KEY-EulerOS" >> ${repo_path}
fi
cat ${repo_path}
yum clean all
yum makecache
yum update

# Set umask
sed -i 's#umask 0077#umask 0022#g' /etc/bashrc

# Install base tools
LOG_HEAD "Install base tools."
yum install -y vim \
wget \
xz \
net-tools \
openssh-clients \
git \
curl \
subversion \
ntpdate \
unzip \
tzdata \
tcl \
sudo \
bzip2 \
passwd \
tar \
java-1.8.0 \
tree \
nfs-utils

# Install compile tools
LOG_HEAD "Install compile tools."
yum install -y gcc \
gcc-c++ \
gcc-gfortran \
make \
gmp-devel \
patch \
autoconf \
libtool \
automake \
numactl \
numactl-devel

# Fix problem for general user(/usr/bin/ld: Permission denied)
chmod 755 /usr/bin/ld.bfd

# Install dependency lib of python
LOG_HEAD "Install dependency lib of python."
yum install -y libffi-devel \
zlib-devel \
bzip2-devel \
openssl-devel \
ncurses-devel \
sqlite-devel \
readline-devel \
gdbm-devel
if [ "${OS_ARCH}" = "x86_64" ]; then
    yum install -y tk-devel
fi

# Install dependency lib of opencv-python(pip)
LOG_HEAD "Install dependency lib of opencv-python(pip)."
yum install -y libSM

# Install dependency lib of pillow(pip)
LOG_HEAD "Install dependency lib of pillow(pip)."
yum install -y libjpeg-turbo-devel

# Install dependency lib of matplotlib(pip)
LOG_HEAD "Install dependency lib of matplotlib(pip)."
yum install -y freetype-devel

# Install dependency lib of scipy(pip)
LOG_HEAD "Install dependency lib of scipy(pip)."
yum install -y blas \
lapack \
atlas
if [ "${OS_ARCH}" = "aarch64" ]; then
    yum install -y openblas \
    atlas-devel
fi


########################
# System Configuration #
########################
# Set hostname
if [ -n "${NODE_HOSTNAME}" ]; then
    LOG_HEAD "Set hostname."
    hostname ${NODE_HOSTNAME}
    echo "${NODE_HOSTNAME}" > /etc/hostname
    cat /etc/hostname
fi

# Set sshd config
LOG_HEAD "Set sshd config."
sed -i 's@#UseDNS no@UseDNS no@g' /etc/ssh/sshd_config
sed -i 's@GSSAPIAuthentication yes@GSSAPIAuthentication no@g' /etc/ssh/sshd_config
sed -i 's@#   StrictHostKeyChecking ask@    StrictHostKeyChecking no@g' /etc/ssh/ssh_config
sed -i 's@#   StrictHostKeyChecking no@    StrictHostKeyChecking no@g' /etc/ssh/ssh_config
sed -i 's@ClientAliveInterval 300@ClientAliveInterval 60@g' /etc/ssh/sshd_config
sed -i 's@ClientAliveCountMax 0@ClientAliveCountMax 3@g' /etc/ssh/sshd_config
cat /etc/ssh/sshd_config|egrep '^UseDNS no|^GSSAPIAuthentication no|^ClientAliveInterval 60|^ClientAliveCountMax 3'
cat /etc/ssh/ssh_config|egrep 'StrictHostKeyChecking no'
systemctl restart sshd

# Set timeout
LOG_HEAD "Set timeout."
sed -i 's@export TMOUT=300@export TMOUT=0@g' /etc/profile
cat /etc/profile|grep TMOUT=0
source /etc/profile

# Set timezone
LOG_HEAD "Set timezone."
rm -f /etc/localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' > /etc/timezone
grep "export TZ='Asia/Shanghai'" /etc/profile
if [ $? -ne 0 ]; then
    echo "export TZ='Asia/Shanghai'" >> /etc/profile
fi
hwclock --systohc

# Synchronised clock
LOG_HEAD "Synchronised clock."
ntpdate ntp1.aliyun.com

# Stop firewalld
LOG_HEAD "Stop firewalld."
systemctl stop firewalld
systemctl disable firewalld

# Stop SELinux
LOG_HEAD "Stop SELinux(Need reboot)."
sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
setenforce 0
/usr/sbin/sestatus -v

# Change password expires
LOG_HEAD "Change password expires."
chage -M 99999 root
chage -l root
