#!/bin/bash

_install() {
    if [[ $OS == "CentOS6" ]];then
        # Enable
        local softs="auditd crond ebtables haldaemon iptables kdump messagebus netfs network ntpd ntpdate rsyslog sshd sysstat udev-post"
    
        for soft in $softs
        do
            service $soft start > /dev/null 2>&1
            chkconfig $soft on > /dev/null 2>&1
        done
    
        # Disable
        local DisableService='ipmi ipmi_port lldpd NetworkManager nfslock postfix rpcbind tuned'
        for soft in $DisableService
        do
            service $soft stop > /dev/null 2>&1
            chkconfig $soft off > /dev/null 2>&1
        done

    elif [[ $OS == "CentOS7" ]];then

        # 镜像的audit损坏，需要重装一下
        yum -y reinstall audit >/dev/null 2>&1
        systemctl daemon-reload >/dev/null 2>&1

        # Enable
        if [[ $BFFLAG -eq 1 ]];then
            local softs="auditd crond dbus ntpd ntpdate rsyslog sshd"
        else
            local softs="auditd crond dbus iptables kdump network ntpd ntpdate rsyslog sshd sysstat"
        fi

        for soft in $softs
        do
            systemctl start $soft > /dev/null 2>&1
            systemctl enable $soft > /dev/null 2>&1
        done
        # Disable
        local DisableService='chronyd firewalld ipmi NetworkManager postfix avahi-daemon lldpd tuned'
        for soft in $DisableService
        do
            systemctl disable $soft > /dev/null 2>&1
            systemctl stop $soft > /dev/null 2>&1
        done

    else
        PRINT_ERR "Not support OS '$OS'"
        return 1
    fi

    return 0

}

_check() {
    _check="PASS"
    if [[ $OS == "CentOS6" ]];then
        local softs="auditd crond ebtables haldaemon iptables kdump messagebus netfs network ntpd ntpdate rsyslog sshd sysstat udev-post"

        for soft in $softs
        do
        if [[ $(chkconfig --list |grep $soft | grep -c :on) -eq 0 ]]; then
            _check="FAIL"
            PRINT_ERR "[ERROR] $soft Not Enabled In Chkconfig List"
        fi
        done

        local DisableService='ipmi ipmi_port NetworkManager postfix lldpd tuned'
        for soft in $DisableService
        do
            if [[ $(chkconfig --list |grep $soft | grep -c :on) -ne 0 ]]; then
                _check="FAIL"
                PRINT_ERR "[ERROR] $soft Enabled In Shkconfig List"
            fi
        done

    elif [[ $OS == "CentOS7" ]];then
        if [[ $BFFLAG -eq 1 ]];then
            local softs="auditd crond dbus ntpd ntpdate rsyslog sshd sysstat"
        elif [[ $ARCH == "aarch64" ]];then
            local softs="auditd crond dbus kdump network ntpd ntpdate rsyslog sshd sysstat"
        else
            local softs="auditd crond dbus iptables kdump network ntpd ntpdate rsyslog sshd sysstat"
        fi

        for soft in $softs
        do
            systemctl is-enabled "$soft" >/dev/null 2>&1
            if [[ $? -ne 0 ]]; then
                _check="FAIL"
                PRINT_ERR "[ERROR] $soft Not Enabled In Chkconfig List"
            fi
        done
        for soft in $softs
        do
            if [[ $soft == 'ntpdate' ]];then
                continue
            fi
            systemctl status "$soft" >/dev/null 2>&1
            if [[ $? -ne 0 ]]; then
                _check="FAIL"
                PRINT_ERR "[ERROR] $soft status not ok"
            fi
        done

        local DisableService='chronyd firewalld ipmi NetworkManager postfix avahi-daemon lldpd tuned'
        for soft in $DisableService
        do
            systemctl is-enabled "$soft" >/dev/null 2>&1
            if [[ $? -eq 0 ]]; then
                _check="FAIL"
                PRINT_ERR "[ERROR] $soft Enabled In systemd"
            fi
        done
    else
        PRINT_ERR "Not support OS '$OS'"
        return 1
    fi


}
