#!/bin/bash 

function Init_Agent {
	is_os_deployment=$1
	agent_root=$2
	agent_id=$3
	deployment_id=$4

	init_rc=0
	if [ "$is_os_deployment" == True ]
	then
		Write_Log "Run Agent initialization for OS Deployment"
		Run_Command "$agent_root/service/Colibri.Agent.Service DeployOS --DeploymentId $deployment_id --ComputerId $agent_id"
		init_rc=$?
		Write_Log "Agent initialization completed with return code: [$init_rc]"
		Run_Command "systemctl restart colibri-agent"
	else
		Write_Log "Run Agent initialization for Agent push"
		Write_Log "Ensure Database removed before initialization"
		[ -d "/var/lib/colibri" ] && rm -f "/var/lib/colibri/Colibri*"
		agent_service_file=/etc/systemd/system/colibri-agent.service
		backup_original_service=$(cat "$agent_service_file")
		sed -i "s/RunAsService/DeployAgent --DeploymentId $deployment_id --ComputerId $agent_id/" "$agent_service_file"
		Run_Command "systemctl daemon-reload"
		Run_Command "systemctl restart colibri-agent"
		sleep 10
		echo "$backup_original_service" > "$agent_service_file"
		Run_Command "systemctl daemon-reload"
	fi
	return $init_rc
}
