#!/bin/bash
#Description:This is my init sh                                 *
#Author:xhy                                                     *
#Version:v6.0.5                                                 *
#Time:2022/04/21                                                *
#****************************************************************

#-------------------------------------------------------------------------------------
pgversion=v6.0.5
pyversion=3.11.2
platform=$(cat /etc/redhat-release | grep -o "[0-9]*[0-9]" | sed -n '1p;1q')
mac=`/sbin/ip addr | grep link | grep -v loopback | grep -v inet6 | awk '{print $2}'`
netname=`ls -l /etc/sysconfig/network-scripts/ | grep ifcfg-e | awk '{print $9}'`
mydir=`pwd`
#-------------------------------------------------------------------------------------
yum -y install vim wget tar bzip2 xz unzip
yum -y update
result00="Yes,tar bzip vim xz install completed!"
#-------------------------------------------------------------------------------------
myshellinitpk="myshellinit${pgversion}.tar.bz2"
if [ ! -f "${myshellinitpk}" ]; then
    wget http://down.linuxsb.com/myoptions/${myshellinitpk}
fi

tar -jxvf ${myshellinitpk} 2>&1 > /dev/null
/bin/chmod u+x ./myshellinit/*.sh
sleep 1s
result01="Yes,myshellinitpk pk tar completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
echo -n "Please Input Host's Name:"
read hostname
echo -n "Please Input Host's IP:"
read ip
echo -n "Please Input Host's PREFIX:"
read prefix
echo -n "Please Input Host's Gateway:"
read gateway
echo -n "Please Input DNS01:"
read dns01
echo -n "Please Input DNS02:"
read dns02
echo -n "Please Input DNS03:"
read dns03
echo -n "Please Please select the system language,C(Chinese) or E(English):"
read language
echo -n "Do you need to restart this host,Y or N?"
read answer1
echo -n "Do you want update kernel to laset,Y or N?"
read answer2
echo -n "Is it an inland host,Y or N?"
read answer3
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
if [ ${platform} -eq 6 ];then
    case ${language} in
        C | c)
            echo 'LANG="zh_CN.UTF-8"'>/etc/sysconfig/i18n
            sed -i 's/^HOSTNAME=.*/HOSTNAME='${hostname}'/' /etc/sysconfig/network
            hostname ${hostname}
            sed -i 's/^NM_CONTROLLED=.*/NM_CONTROLLED=no/' ./myshellinit/ifcfg-eth0
            ;;
        E | e)
            echo 'LANG="en_US.UTF-8"'>/etc/sysconfig/i18n
            sed -i 's/^HOSTNAME=.*/HOSTNAME='${hostname}'/' /etc/sysconfig/network
            hostname ${hostname}
            ;;
            *)
            echo "Default is English!"
            echo 'LANG="en_US.UTF-8"'>/etc/sysconfig/i18n
            sed -i 's/^HOSTNAME=.*/HOSTNAME='${hostname}'/' /etc/sysconfig/network
            hostname ${hostname}
    esac
else
    case ${language} in
        C | c)
            localectl  set-locale LANG=zh_CN.utf8
            hostnamectl set-hostname ${hostname}
            ;;
        E | e)
            localectl  set-locale LANG=en_US.UTF-8
            hostnamectl set-hostname ${hostname}
            ;;
            *)
            echo "Default is English!"
            localectl  set-locale LANG=en_US.UTF-8
            hostnamectl set-hostname ${hostname}
    esac
fi
sleep 1s
result02="Yes,language operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
mkdir -p /etc/yum.repos.d/backup
mkdir -p /mnt/cdrom
mkdir -p /media/cdrom
/bin/cp -brpf /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.bak
rm -rf /etc/udev/rules.d/*-persistent-*.rules
sleep 1s
result03="Yes,cdrom and Rules operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
mv -b /etc/sysconfig/network-scripts/${netname} /etc/sysconfig/network-scripts/${netname}\.bak
/bin/cp -brpf ./myshellinit/ifcfg-eth0 /etc/sysconfig/network-scripts/
echo "HWADDR=${mac}" 1>>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "IPADDR=${ip}" >>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "PREFIX=${prefix}" >>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "GATEWAY=${gateway}" >>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "DNS1=${dns01}" >>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "DNS2=${dns02}" >>/etc/sysconfig/network-scripts/ifcfg-eth0
echo "DNS3=${dns03}" >>/etc/sysconfig/network-scripts/ifcfg-eth0
sleep 1s
result04="Yes,network operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
/bin/cp -brpf /etc/selinux/config /etc/selinux/config.bak
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
sed -i 's/^UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
sed -i 's/^#UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
/usr/bin/yum clean all
/usr/bin/yum repolist
sleep 1s
result05="Yes,SElinux and Sshd operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
if [ ${platform} -eq 6 ];then
    /sbin/service network restart
    /sbin/chkconfig iptables off
elif [ ${platform} -eq 7 ];then
    /bin/cp -brpf /etc/default/grub /etc/default/grub.bak
    sed -i 's/GRUB_CMDLINE_LINUX=\"/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0 /g' /etc/default/grub
    grub2-mkconfig -o /boot/grub2/grub.cfg > /dev/null
    /bin/systemctl restart network
    /bin/systemctl disable firewalld
else
    /bin/cp -brpf /etc/default/grub /etc/default/grub.bak
    sed -i 's/GRUB_CMDLINE_LINUX=\"/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0 /g' /etc/default/grub
    systemctl restart NetworkManager
    systemctl enable NetworkManager
    /bin/systemctl disable firewalld
fi
sleep 1s
result06="Yes,network restart operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
mv -b /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
mv -b /etc/yum.repos.d/*.rpmnew /etc/yum.repos.d/backup
sleep 1s
result07="Yes,yum repo backup operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
OS=`cat /etc/redhat-release | awk '{print $1}'`
case ${answer3} in
    y | Y | yes | Yes)
        if [ -z ${OS} ];then
            /bin/cp -rbpf ./myshellinit/CentOS${platform}*-aliyun-ustc-redhat.repo /etc/yum.repos.d/
            /bin/cp -rbpf ./myshellinit/epel${platform}*-aliyun-*.repo /etc/yum.repos.d/
        else
            if [ $OS\X == 'CentOSX' ];then
			    if [ ${platform} -eq 8 ];then
                /bin/cp -rpf ./myshellinit/CentOS${platform}*-aliyun-tencent.repo /etc/yum.repos.d/ 
                /bin/cp -rpf ./myshellinit/epel${platform}*-aliyun-*.repo /etc/yum.repos.d/
                else
                /bin/cp -rpf ./myshellinit/CentOS${platform}*-aliyun-ustc.repo /etc/yum.repos.d/
                /bin/cp -rpf ./myshellinit/epel${platform}*-aliyun-*.repo /etc/yum.repos.d/
				fi
            else
                /bin/cp -rpf ./myshellinit/CentOS${platform}*-aliyun-ustc-redhat.repo /etc/yum.repos.d/
                /bin/cp -rpf ./myshellinit/epel${platform}*-aliyun-*.repo /etc/yum.repos.d/
            fi
        fi
    ;;
    *)
        if [ -z $OS ];then
            /bin/cp -rpf ./myshellinit/CentOS${platform}*-Base-xtom-mit-redhat.repo /etc/yum.repos.d/
            /bin/cp -rpf ./myshellinit/epel${platform}*-xtom-*.repo /etc/yum.repos.d/
        else
            if [ $OS\X == 'CentOSX' ];then
                /bin/cp -rpf ./myshellinit/CentOS${platform}*-Base-xtom-mit.repo /etc/yum.repos.d/
                /bin/cp -rpf ./myshellinit/epel${platform}*-xtom-*.repo /etc/yum.repos.d/
            else
                /bin/cp -rpf ./myshellinit/CentOS${platform}*-Base-xtom-mit-redhat.repo /etc/yum.repos.d/
                /bin/cp -rpf ./myshellinit/epel${platform}*-xtom-*.repo /etc/yum.repos.d/
            fi
        fi
esac
sleep 1s
result08="Yes,yum repo install operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
case ${answer2} in
    y | Y | yes | Yes)
        if [ ${platform} -eq 8 ];then
            /bin/cp -brpf ./myshellinit/CentOS8-Media.repo /etc/yum.repos.d/
            rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
            rpm --import http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-Official
            yum -y install https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm
            mv -b /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/backup
            /bin/cp -rbpf ./myshellinit/elrepo8.repo /etc/yum.repos.d/elrepo.repo
            yum --disablerepo="*" --enablerepo="elrepo-kernel" install -y kernel-ml
            sleep 1s
            grub2-set-default 0
            /bin/cp -rbpf ./myshellinit/removeoldkernel.sh /tmp/
            echo "/tmp/removeoldkernel.sh" >> /etc/rc.d/rc.local
            chmod u+x /etc/rc.d/rc.local
        elif [ ${platform} -eq 7 ];then
            /bin/cp -brpf ./myshellinit/CentOS7-Media.repo /etc/yum.repos.d/
            rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
            yum -y install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
            mv -b /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/backup
            /bin/cp -rbpf ./myshellinit/elrepo7.repo /etc/yum.repos.d/elrepo.repo
            yum --disablerepo="*" --enablerepo="elrepo-kernel" install -y kernel-ml
            sleep 1s
            grub2-set-default 0
            /bin/cp -rbpf ./myshellinit/removeoldkernel.sh /tmp/
            echo "/tmp/removeoldkernel.sh" >> /etc/rc.d/rc.local
            chmod u+x /etc/rc.d/rc.local
        elif [ ${platform} -eq 6 ];then
            /bin/cp -brpf ./myshellinit/CentOS7-Media.repo /etc/yum.repos.d/
            rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
            yum -y install https://www.elrepo.org/elrepo-release-6.el6.elrepo.noarch.rpm
            mv -b /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/backup
            /bin/cp -rbpf ./myshellinit/elrepo6.repo /etc/yum.repos.d/elrepo.repo
            yum --disablerepo="*" --enablerepo="elrepo-kernel" install -y kernel-lt
            sleep 1s
            sed -i 's/^default=.*/default=0/' /etc/grub.conf
            sed -i 's/^default=.*/default=0/' /boot/grub/grub.conf
            /bin/cp -rbpf ./myshellinit/removeoldkernel.sh /tmp/
            echo "/tmp/removeoldkernel.sh" >> /etc/rc.d/rc.local
            chmod u+x /etc/rc.d/rc.local
        fi
        ;;
    *)
            source /root/.bashrc
            echo "Don't update kernel!"
esac
sleep 1s
result09="Yes,kernel update operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
if [ ${platform} -eq 8 ];then
    /usr/bin/yum -y install wget
    rpm --import http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-Official
    rpm --import https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-8
    dnf -y install tar bzip2 git gcc gcc-c++ make wget openssl-devel openssh-clients bash-completion net-tools vim chrony screen lrzsz epel-release tree jq curl lvm2 yum-utils zlib zlib-devel libffi-devel perl-Pod-Html --skip-broken
else
    pyversionold=`python -V 2>&1 | awk '{ print $2 }' | awk -F '.' '{ print $1"."$2 }'`
    sed -i "s/bin\/python/bin\/python${pyversionold}/g" /usr/bin/yum
    if [ ${platform} -eq 7 ];then
      sed -i "s/bin\/python/bin\/python${pyversionold}/g" /usr/libexec/urlgrabber-ext-down
      sed -i "s/bin\/python/bin\/python${pyversionold}/g" /usr/bin/yum-config-manager
      sed -i "s/bin\/python/bin\/python${pyversionold}/g" /usr/sbin/yum-complete-transaction
    fi
    yum -y install tar bzip2 git gcc gcc-c++ make wget openssl-devel openssh-clients bash-completion net-tools vim ntp screen lrzsz bash-completion-extras epel-release openssl tree jq curl lvm2 yum-utils zlib zlib-devel libffi-devel tcping fping --skip-broken
fi

cd ${mydir}

sleep 1s
result10="Yes,rpm install operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
/bin/cp -brpf /root/.vimrc /root/.vimrc.bak
/bin/cp -brpf ./myshellinit/my_Vimrc_Diy01_v1 /root/.vimrc
/bin/cp -brpf ./myshellinit/my_Vimrc_Diy01_v1 /etc/skel/.vimrc
sleep 1s
result11="Yes,Vim backup and install operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
if [ ${platform} -eq 8 ];then
    /bin/cp -brpf /etc/chrony.conf /etc/chrony.conf.bak
    /bin/cp -brpf ./myshellinit/Aliyunchrony.conf /etc/chrony.conf
    systemctl start chronyd
    systemctl enable chronyd
elif [ ${platform} -eq 7 ];then
    /bin/cp -brpf /etc/ntp.conf /etc/ntp.conf.bak
    /bin/cp -brpf ./myshellinit/Aliyunntp.conf /etc/ntp.conf
    /bin/systemctl enable ntpd
    /bin/systemctl start ntpd
else
    /bin/cp -brpf /etc/nntp.conf /etc/ntp.conf.bak
    /bin/cp -brpf ./myshellinit/Aliyunntp.conf /etc/ntp.conf
    /sbin/service ntpd start
    /sbin/chkconfig ntpd on
fi
result12="Yes,ntp set and start operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
/bin/cp -brpf /root/.bashrc /root/.bashrc.bak
cat ./myshellinit/my_Bashrc_Diy01_v1 >> /etc/profile.d/myalias.sh
sleep 1s
result13="Yes,bashrc operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
if [ ${platform} -eq 8 ];then
    echo "CentOS 8 don't need update!"
    result14="Warn,CentOS 8 openssl don't need update!"
else
    wget -c http://down.linuxsb.com/software/openssl-1.1.1m.tar.gz -P ./myshellinit --no-check-certificate
    tar -zxvf ./myshellinit/openssl-1.1.1m.tar.gz -C ./myshellinit/
    cd ./myshellinit/openssl-1.1.1m
    ./config --prefix=/usr/local/openssl no-zlib
    make && make install
    mv /usr/bin/openssl /usr/bin/openssl.bak
    ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
    ln -snf /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
    ln -snf /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
    mv /usr/include/openssl/ /usr/include/openssl.bak
    ln -s /usr/local/openssl/include/openssl /usr/include/openssl
    ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
    echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
    echo "CentOS openssl update!"
    result14="Yes,openssl update operation completed!"
fi
cd ${mydir}
sleep 1s
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
wget -c https://mirrors.huaweicloud.com/python/${pyversion}/Python-${pyversion}.tar.xz -P ./myshellinit --no-check-certificate
tar -Jxvf ./myshellinit/Python-${pyversion}.tar.xz -C ./myshellinit/
cd ./myshellinit/Python-${pyversion}/
if [ ${platform} -eq 8 ];then
    dnf -y install tar bzip2 git gcc gcc-c++ make wget openssl-devel openssh-clients bash-completion net-tools vim chrony screen lrzsz epel-release tree jq curl lvm2 yum-utils zlib zlib-devel libffi-devel perl-Pod-Html --skip-broken
    ./configure --prefix=/usr/local/python3 --with-ssl
else
    yum -y install tar bzip2 git gcc gcc-c++ make wget openssl-devel openssh-clients bash-completion net-tools vim ntp screen lrzsz bash-completion-extras epel-release openssl tree jq curl lvm2 yum-utils zlib zlib-devel libffi-devel --skip-broken
    ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
fi
make && make install
sleep 1s
pyversionnew=`echo ${pyversion} | awk -F '.' '{ print $1"."$2 }'`
cp -brpf /usr/local/python3/bin/{python,pip,pydoc}${pyversionnew} /usr/bin/
cp -brpf /usr/local/python3/bin/python${pyversionnew}-config /usr/bin/
if [ ${platform} -eq 8 ];then
    alternatives --install /usr/bin/python3 python3 /usr/bin/python${pyversionnew} 2000000 --slave /usr/bin/pip3 pip3 /usr/bin/pip${pyversionnew}  \
    --slave /usr/bin/pydoc3 pydoc3 /usr/bin/pydoc${pyversionnew}  --slave /usr/bin/pydoc-3 pydoc-3 /usr/bin/pydoc${pyversionnew}  \
    --slave /usr/bin/pip-3 pip-3 /usr/bin/pip-${pyversionnew} --slave /usr/bin/python3-config python3-config /usr/bin/python${pyversionnew}-config \
    --slave /usr/bin/pyvenv-3 pyvenv-3 /usr/bin/pyvenv-3.6
    alternatives --auto python
    echo 'export PATH=$PATH:/usr/local/python3/bin/' >> /root/.bashrc
    source /root/.bashrc
else
    pyversionold=`python -V 2>&1 | awk '{ print $2 }' | awk -F '.' '{ print $1"."$2 }'`
    pyversionnew=`echo ${pyversion} | awk -F '.' '{ print $1"."$2 }'`
    mv /usr/bin/python /usr/bin/python.bak
    alternatives --install /usr/bin/python python /usr/bin/python${pyversionold} 30
    alternatives --install /usr/bin/python python /usr/local/python3/bin/python${pyversionnew} 50
    alternatives --auto python
    ln -s /usr/bin/python${pyversionnew} /usr/bin/python3
    ln -s /usr/bin/pip${pyversionnew} /usr/bin/pip3
    ln -s /usr/bin/python${pyversionnew}-config /usr/bin/python3-config3
    sleep 1s
    echo 'export PATH=$PATH:/usr/local/python3/bin/' >> /root/.bashrc
    source /root/.bashrc
fi
cd ${mydir}
sleep 1s
pyversioncheck=`python3 -V 2>&1 | awk '{ print $2 }' | awk -F '.' '{ print $1 }'`
if [ ${pyversioncheck}X = 3X ];then
    result15="Yes,python update operation completed!"
else
    result15="Yes.python update operation failed!"
fi
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
mkdir -p ~/.pip
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple/ 
[install]
trusted-host = pypi.mirrors.ustc.edu.cn
EOF
result16="Yes,pip repo install operation completed!"
pip3 install --upgrade pip
pip install pyreadline
pip completion --bash >> ~/.bashrc
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
chattr -a /var/log/messages
history -w
echo "">/root/.bash_history 
history -c
find . -maxdepth 1  -regex ".*init.*" | xargs rm -rf
sleep 1s
result17="Yes,history clear operation completed!"
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
sleep 10s
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
case ${answer1} in
    y | Y | yes | Yes)
        sleep 1s
        source /root/.bashrc
        bash ${mydir}/myshellinit/clean.sh
        rm -rf ${mydir}/*myshellinit*
        result18="Yes,everything is done,enjoy it!"
        result19="Yes,begin to restart computer!"
        echo -e "result00: ${result00}\nresult01: ${result01}\nresult02: ${result02}
result03: ${result03}\nresult04: ${result04}\nresult05: ${result05}
result06: ${result06}\nresult07: ${result07}\nresult08: ${result08}
result09: ${result09}\nresult10: ${result10}\nresult11: ${result11}
result12: ${result12}\nresult13: ${result13}\nresult14: ${result14}
result15: ${result15}\nresult16: ${result16}\nresult17: ${result17}
result18: ${result18}\nresult19: ${result19}\n" > myshellinit.log
        echo "Everything is done,enjoy it!"
        /sbin/shutdown -r now
    ;;
    *)
        sleep 1s
        source /root/.bashrc
        bash ${mydir}/myshellinit/clean.sh
        rm -rf ${mydir}/*myshellinit*
        result18="Yes,everything is done,enjoy it!"
        echo -e "result00: ${result00}\nresult01: ${result01}\nresult02: ${result02}
result03: ${result03}\nresult04: ${result04}\nresult05: ${result05}
result06: ${result06}\nresult07: ${result07}\nresult08: ${result08}
result09: ${result09}\nresult10: ${result10}\nresult11: ${result11}
result12: ${result12}\nresult13: ${result13}\nresult14: ${result14}
result15: ${result15}\nresult16: ${result16}\nresult17: ${result17}
result18: ${result18}" > myshellinit.log
        echo "Everything is done,enjoy it!"
    ;;
esac
#-------------------------------------------------------------------------------------
