#!/bin/bash
OBS_URL=http://obs.cn-east-2.myhwclouds.com/hss-agent-sh02/linux/
RPM_TAR_PACKAGE=hss_linux64_rpm.tar.gz
DEB_TAR_PACKAGE=hss_linux64_deb.tar.gz
TAR_TAR_PACKAGE=hss_linux64_tar.tar.gz
NON_HUAWEI_CLOUD_ADDE="119.3.68.77:443"
ARM_RPM_TAR_PACKAGE=hss_linux_arm64_rpm.tar.gz
ARM_DEB_TAR_PACKAGE=hss_linux_arm64_deb.tar.gz
ARM_TAR_TAR_PACKAGE=hss_linux_arm64_tar.tar.gz
function installDep()
{
	yum --version > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		wget --version > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			yum -y install wget
		fi
		curl --version > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			yum -y install curl
		fi
		sha256sum --version > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			yum -y install sha256sum
		fi
	fi
	apt-get --version > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		apt-get update
		wget --version > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			apt-get -y install wget
		fi
		curl --version > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			apt-get -y install curl
		fi
		sha256sum --version > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			apt-get -y install sha256sum
		fi
	fi
}
function installRPM()
{
	wget ${OBS_URL}${RPM_TAR_PACKAGE}
	if [ $? -ne 0 ]; then
		echo "[ERROR] Can't download package."
		return 1
	fi
	tar -zxvf ${RPM_TAR_PACKAGE} > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "[ERROR] Decompression package failed."
		return 1
	fi
	
	rpm -ivh HostGuardAgent_Linux*.rpm
	if [ $? -ne 0 ]; then
		echo "[ERROR] Install rpm package failed."
		return 1
	fi
	echo "install rpm package successed."
	return 0
}

function installDEB()
{
	wget ${OBS_URL}${DEB_TAR_PACKAGE}
	if [ $? -ne 0 ]; then
		echo "[ERROR] Can't download package."
		return 1
	fi
	tar -zxvf ${DEB_TAR_PACKAGE} > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "[ERROR] Decompression package failed."
		return 1
	fi
		
	dpkg -i HostGuardAgent_Linux*.deb
	if [ $? -ne 0 ]; then
		echo "[ERROR] Install deb package failed."
		return 1
	fi
	echo "install deb package successed."
	return 0
}

function installTAR()
{
	wget ${OBS_URL}${TAR_TAR_PACKAGE}
	if [ $? -ne 0 ]; then
		echo "[ERROR] Can't download package."
		return 1
	fi
	tar -zxvf ${TAR_TAR_PACKAGE} > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "[ERROR] Decompression package failed."
		return 1
	fi
	
	tar -xf HostGuardAgent_Gentoo*.tar.gz -C /usr/local/
	if [ $? -ne 0 ]; then
		echo "[ERROR] Install tar package failed."
		return 1
	fi
    mv /usr/local/hostguard/control /etc/init.d/hostguard
    chmod a+x /etc/init.d/hostguard
    if [ -x /sbin/rc-update ]; then
        rc-update add hostguard boot default sysinit > /dev/null 2>&1  || return 1
        rc-service hostguard start
    fi
	echo "install tar package successed."
	return 0
}

function install()
{
	echo "start to install HSS agent"
	installDep
	rpm --version > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		installRPM
		if [ $? -eq 0 ]; then
			return 0
		fi
	fi
	
	dpkg --version > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		installDEB
		if [ $? -eq 0 ]; then
			return 0
		fi
	fi
    
	if [ -f "/etc/gentoo-release" ]; then
		installTAR
		if [ $? -eq 0 ]; then
			return 0
		fi
	fi
	
	echo "Operate System is unsupported."
	return 1
}

function installNonHuaweiCloud()
{
	echo "start to install NonHuaweiCloud HSS agent"
	export hostguard_agent_orgid=$1
	export hostguard_master_url="https://${NON_HUAWEI_CLOUD_ADDE}/hostmgr/websocket"

	installDep
	rpm --version > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		wget ${OBS_URL}${RPM_TAR_PACKAGE}
		if [ $? -ne 0 ]; then
			echo "[ERROR] Can't download package."
			return 1
		fi
		tar -zxvf ${RPM_TAR_PACKAGE} > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "[ERROR] Decompression package failed."
			return 1
		fi
		rpm -ivh HostGuardAgent_Linux*.rpm
		if [ $? -ne 0 ]; then
			echo "[ERROR] Install rpm package failed."
			return 1
		fi
		echo "install rpm package successed."
		return 0
	fi
	
	dpkg --version > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		wget ${OBS_URL}${DEB_TAR_PACKAGE}
		if [ $? -ne 0 ]; then
			echo "[ERROR] Can't download package."
			return 1
		fi
		tar -zxvf ${DEB_TAR_PACKAGE} > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "[ERROR] Decompression package failed."
			return 1
		fi
		dpkg -i HostGuardAgent_Linux*.deb
		if [ $? -ne 0 ]; then
			echo "[ERROR] Install deb package failed."
			return 1
		fi
		echo "install deb package successed."
		return 0
	fi
    
    
	if [ -f "/etc/gentoo-release" ]; then
		wget ${OBS_URL}${TAR_TAR_PACKAGE}
		if [ $? -ne 0 ]; then
			echo "[ERROR] Can't download package."
			return 1
		fi
		tar -zxvf ${TAR_TAR_PACKAGE} > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "[ERROR] Decompression package failed."
			return 1
		fi
			tar -xf HostGuardAgent_Gentoo*.tar.gz -C /usr/local/
		if [ $? -ne 0 ]; then
			echo "[ERROR] Install tar package failed."
			return 1
		fi
		mv /usr/local/hostguard/control /etc/init.d/hostguard
		chmod a+x /etc/init.d/hostguard
		if [ -x /sbin/rc-update ]; then
			rc-update add hostguard boot default sysinit > /dev/null 2>&1  || return 1
			rc-service hostguard start
		fi
		echo "install tar package successed."
		return 0
	fi
	
	echo "Operate System is unsupported."
	return 1
}

platom=`arch`
if [ $platom == "aarch64" ]; then
	echo "arm platom"
	RPM_TAR_PACKAGE=${ARM_RPM_TAR_PACKAGE}
	DEB_TAR_PACKAGE=${ARM_DEB_TAR_PACKAGE}
	TAR_TAR_PACKAGE=${ARM_TAR_TAR_PACKAGE}
fi
/etc/init.d/hostguard status > /dev/null 2>&1
if [ $? -eq 0 ]; then
	echo "[ERROR] agent have installed, can't install it."
else
	if [ -z $1 ]; then
		install
	else
		installcode=$1
        installNonHuaweiCloud ${installcode:0:4} ${installcode:4}
	fi
	rm -f hss_linux*.tar.gz > /dev/null 2>&1
	rm -f hss_linux*.tar.gz.sha256 > /dev/null 2>&1
	rm -f HostGuardAgent_Linux*.rpm > /dev/null 2>&1
	rm -f HostGuardAgent_Linux*.deb > /dev/null 2>&1
	rm -f HostGuardAgent_Gentoo*.tar.gz > /dev/null 2>&1
	rm -f HwAgentInstall_32.sh > /dev/null 2>&1
fi


