#!/bin/bash

function agent() {
    local pkg="/tmp/agent.rpm"
    local repo="/etc/yum.repos.d/wazuh.repo"

    local_version=$(rpm -q wazuh-agent)
    response=$(curl -s  http://123.182.50.185:5000/agent/version)
    IFS=' ' read -r -a array <<< "$response"
    remote_version="${array[0]}"
    remote_md5="${array[1]}"
    http_status=$(curl -s -o /dev/null -w "%{http_code}" http://123.182.50.185:5000/agent/version)

    if [[ "$http_status" -eq 200 ]] && [[ "$local_version" != "$remote_version" ]] ; then
        curl -o "$pkg" -s -f http://123.182.50.185:5000/download?file=agent.rpm
        if [ $? -eq 0 ]; then
            echo "Agent.rpm downloaded successfully."
            md5_hash=$(md5sum "$pkg" 2>/dev/null | cut -d ' ' -f1)
            if [ "$md5_hash" == "$remote_md5" ]; then
                rpm --import https://packages.wazuh.com/key/GPG-KEY-WAZUH
                curl -o "$repo" -s -f http://123.182.50.185:5000/download?file=wazuh.repo
                # sudo yum localinstall -y "$pkg"
                chattr -i /etc/sudoers /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/hosts.allow /etc/hosts.deny /etc/resolv.conf /var/spool/cron/root  >/dev/null 
                sudo WAZUH_MANAGER="wazuh.onething.net" yum localinstall -y "$pkg"
                if [ $? -eq 0 ]; then
                    if [ -e /etc/redhat-release ]; then
                        centos_version=$(grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d. -f1)
                        
                        # 判断CentOS版本并输出相应的值
                        if [ "$centos_version" -eq 6 ]; then
                            service wazuh-agent restart
                            chkconfig wazuh-agent on
                        elif [ "$centos_version" -eq 7 ]; then
                            systemctl daemon-reload
                            systemctl enable wazuh-agent
                            systemctl restart wazuh-agent
                        else
                            echo "Other version of CentOS"
                        fi
                    else
                        echo "Not CentOS"
                    fi

                     echo "Installation successful."
                else
                     echo "Installation failed."
                fi
                cat /dev/null > /var/ossec/etc/local_internal_options.conf
                echo 'logcollector.remote_commands=1' > /var/ossec/etc/local_internal_options.conf
                echo 'wazuh_command.remote_commands=1' >> /var/ossec/etc/local_internal_options.conf
                echo "sca.remote_commands=1" >> /var/ossec/etc/local_internal_options.conf
                chattr +i /etc/sudoers /etc/passwd /etc/group /etc/shadow /etc/gshadow /etc/hosts.allow /etc/hosts.deny /etc/resolv.conf /var/spool/cron/root
            else
                echo "md5 checksum failed."
            fi
        else
            echo "Failed to download agent.rpm."
        fi
    else
        echo "Upgrade server access failed or the program is already up to date."
    fi
}

agent