#!/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)
#OS_VERSION=$(cat /etc/os-release|grep '^VERSION_ID='|awk -F'"' '{print $2}')


##################
# 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 apt source
LOG_HEAD "Set apt source."
cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
if [ "${OS_ARCH}" = "aarch64" ]; then
    sed -i "s@http://ports.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
    sed -i "s@http://us.ports.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
else
    sed -i "s@http://.*archive.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
    sed -i "s@http://.*security.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
fi
apt update

# Install base tools
LOG_HEAD "Install base tools."
DEBIAN_FRONTEND=noninteractive apt install -y vim \
wget \
xz-utils \
net-tools \
openssh-client \
git \
curl \
subversion \
ntpdate \
unzip \
tzdata \
tcl \
sudo \
bzip2 \
default-jre \
tree \
nfs-common

# Install compile tools
LOG_HEAD "Install compile tools."
DEBIAN_FRONTEND=noninteractive apt install -y gcc  \
g++ \
gfortran \
make \
libgmp-dev \
patch \
autoconf \
libtool \
automake \
flex \
libnuma1 \
libnuma-dev

# Install dependency lib of python
LOG_HEAD "Install dependency lib of python."
DEBIAN_FRONTEND=noninteractive apt install -y libffi-dev \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libncurses5-dev \
libgdbm-dev \
liblzma-dev \
libreadline-dev \
libsqlite3-dev

# Install dependency lib of opencv-python(pip)
LOG_HEAD "Install dependency lib of opencv-python(pip)."
DEBIAN_FRONTEND=noninteractive apt install -y libsm6

# Install dependency lib of pillow(pip)
LOG_HEAD "Install dependency lib of pillow(pip)."
DEBIAN_FRONTEND=noninteractive apt install -y libjpeg-dev

# Install dependency lib of matplotlib(pip)
LOG_HEAD "Install dependency lib of matplotlib(pip)."
DEBIAN_FRONTEND=noninteractive apt install -y libfreetype6-dev

# Install dependency lib of scipy(pip)
LOG_HEAD "Install dependency lib of scipy(pip)."
DEBIAN_FRONTEND=noninteractive apt install -y libblas-dev \
liblapack-dev \
libopenblas-dev \
libatlas-base-dev

# Install dependency lib of opencv-python(pip)
#if [ "${OS_ARCH}" = "aarch64" ]; then
#    DEBIAN_FRONTEND=noninteractive apt install -y libgtk2.0-dev
#fi

# Install dependency lib of ascend910 driver
LOG_HEAD "Install dependency lib of ascend910 driver."
DEBIAN_FRONTEND=noninteractive apt install -y dkms


########################
# System Configuration #
########################
# Set bash
LOG_HEAD "Set bash."
echo "dash dash/sh boolean false" | debconf-set-selections
DEBIAN_FRONTEND=noninteractive dpkg-reconfigure dash
ls -l /bin/sh

# 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."
grep "^UseDNS no" /etc/ssh/sshd_config
if [ $? -ne 0 ]; then
    echo "UseDNS no" >> /etc/ssh/sshd_config
fi
sed -i 's@#   StrictHostKeyChecking ask@    StrictHostKeyChecking no@g' /etc/ssh/ssh_config
cat /etc/ssh/sshd_config|egrep '^UseDNS no'
cat /etc/ssh/ssh_config|egrep 'StrictHostKeyChecking no'
systemctl restart sshd

# 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 ufw
LOG_HEAD "Stop ufw."
ufw disable
ufw status

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