#!/bin/sh

# Un-comment to edit VM without this script running
#exit 0

FILE=/usr/local/nagiosxi/is.installed

if [ -f $FILE ]; then
    echo "Nagios XI is already installed"
else
    setenforce 0
	
	# Get type and set up if VHD
	type=$(curl -XGET "https://assets.nagios.com/downloads/nagiosxi/build.php?product=xi")
	if [ "$type" == "vhd" ]; then
		dracut -f --add-drivers "hv_vmbus hv_storvsc hv_netvsc hv_utils hv_balloon hyperv-keyboard hyperv_fb hid-hyperv"
		echo 'add_drivers+="hv_vmbus hv_storvsc hv_netvsc hv_utils hv_balloon hyperv-keyboard hyperv_fb hid-hyperv"' > /etc/dracut.conf.d/hyperv.conf
	fi

    # Update release
    rm -f /etc/issue
    yum update -y
    #echo "re-install ca-certificates"
    #yum reinstall ca-certificates -y
    yum install wget nano tar openssh-clients -y
   
    # Update time
    ntpdate pool.ntp.org
    ntpdate pool.ntp.org
    ntpdate pool.ntp.org
    cp /etc/issue /etc/issue-standard
    cd /tmp

    # Download the latest XI version
    echo "check if /tmp/nagiosxi exists"
    if [ ! -d /tmp/nagiosxi ]; then
        #wget https://assets.nagios.com/downloads/nagiosxi/xi-latest.tar.gz -O /tmp/xi-latest.tar.gz
		wget https://builds.nagios.com/nagiosxi/releases/xi-5.9.1.tar.gz -O /tmp/xi-latest.tar.gz
        tar xzf /tmp/xi-*.tar.gz
    fi

    # Install Nagios XI
    cd /tmp/nagiosxi
    chmod +x ./fullinstall
    ./fullinstall -n
    pgrep nagios && touch /usr/local/nagiosxi/is.installed

	# Remove generated UUID
    if [ "$type" == "ova" ] || [ "$type" == "vhd" ]; then
        rm -f /usr/local/nagiosxi/var/xi-itype
        rm -f /usr/local/nagiosxi/var/xi-uuid
        echo $type >> /usr/local/nagiosxi/var/xi-itype
        chown nagios:nagios /usr/local/nagiosxi/var/xi-itype
    fi

    if [ ! -f /usr/local/nagiosxi/is.installed ];
    then
        /usr/local/installNagiosXI.sh
    else
        rm /tmp/xi-latest.tar.gz* -f
        rm /tmp/nagiosxi -fr
        rm /usr/local/installNagiosXI.sh -f
    fi
    cd /tmp

    wget https://assets.nagios.com/downloads/nagiosxi/vmgeneration/el9/updateissue.sh
    wget https://assets.nagios.com/downloads/nagiosxi/vmgeneration/el9/update-issue.service
    wget https://assets.nagios.com/downloads/nagiosxi/vmgeneration/el9/get-ip-address.sh

    chmod +x get-ip-address.sh
    mv get-ip-address.sh /usr/local/bin
    if [ "`curl http://169.254.169.254/latest/meta-data/public-ipv4`X" != "X" ];then
        touch /usr/local/nagiosxi/onec2
        sed -i 's/\/usr\/local\/bin\/get-ip-address.sh/curl http:\/\/169.254.169.254\/latest\/meta-data\/public-ipv4/g' updateissue.sh
    else
        # We are not on AWS
        wget https://assets.nagios.com/downloads/nagiosxi/vmgeneration/el9/nagiosxi-banner
        mv nagiosxi-banner /etc/nagiosxi-banner
    fi
	chmod +x updateissue.sh
    mv updateissue.sh /usr/local/bin
    mv -f update-issue.service /etc/systemd/system
	systemctl daemon-reload
	systemctl enable update-issue
    touch /etc/issue-standard

    # echo "Banner /etc/issue" >> /etc/ssh/sshd_config
    (
        cd /tmp
        wget https://assets.nagios.com/downloads/nagiosxi/vmgeneration/el9/regen.sh
        chmod +x regen.sh
        mv -f regen.sh /usr/local/run.sh
    )

    # Additional Cleaning
    rm -rf /tmp/nrds* /tmp/wmic* /tmp/xi*
	rm -f /usr/local/nagiosxi/var/event_handler.lock
	
	# Additional package install
	#yum install -y libxml2-devel xml2 libuuid-devel perl-XML-LibXML perl-Env
	#cpan Crypt::SSLeay
	
	cat /dev/null > /var/log/messages
    history -c && rm -f /root/.bash_history
    if [ ! -f /usr/local/nagiosxi/onec2 ];
    then
        /root/scripts/cleandemo.sh
        /root/scripts/cleardrive.sh
		rm -f /root/scripts/cleandemo.sh
        rm -f /root/scripts/cleardrive.sh
        shutdown -h now
    else
        reboot
    fi
fi
