#!/bin/bash

. ${TOOLKIT}-functions.sh

server_url=$(jtconf glpi-agent.server_url)
tasks=$(jtconf glpi-agent.tasks)


# packages to install
#
# tasks: RemoteInventory,Inventory,Deploy,NetDiscovery,Collect,NetInventory,ESX,WakeOnLan
#
to_install=

echo "$tasks" | grep -qi "collect"	&& to_install="$to_install glpi-agent-task-collect"
echo "$tasks" | grep -qi "deploy"	&& to_install="$to_install glpi-agent-task-deploy"
echo "$tasks" | grep -qi "esx"		&& to_install="$to_install glpi-agent-task-esx"
echo "$tasks" | grep -qi "net"		&& to_install="$to_install glpi-agent-task-network"

install_pkgs $to_install

# glpi-agent relies on pottashit init, so we install the missing init systemv script
#
file="/etc/init.d/glpi-agent"
installfile "glpi-agent.init" "$file" root:root 770

# 2025-02-02
# udpate rc
#
# I'm builing a kusa porting for pve (proxmox) systems; they are systemd based,
# and I've no time to deal with the evil syntax of this cancer, so I manually
# install files in the classic rc.* structure
#
if which insserv >/dev/null
then
	insserv -v /etc/init.d/glpi-agent
else
	cd /etc
	rm -f rc*.d/???glpi-agent
	for i in $(grep '^# Default-Stop:' $file | sed -e 's/.*Stop://')
	do
		cd rc$i.d
		ln -s ../init.d/glpi-agent K01glpi-agent
		cd ..
	done
	for i in $(grep '^# Default-Start:' $file | sed -e 's/.*Start://')
	do
		cd rc$i.d
		ln -s ../init.d/glpi-agent S04glpi-agent
		cd ..
	done
fi

file="/etc/default/glpi-agent"
[ -f "$file" ] || {
	installfile "glpi-agent.default" "$file" root:root 640
}

# the original installer script don't give us any hint if something
# is changed or not, se we restart the agent anyway
#
##$SOMETHING_CHANGED && {
	# we can't pass the second arg (program name), because
	# the agent overwrites cmdline environment with his own status
	# and 'pidof' function fails to find him
	#
	restart_service glpi-agent
##}

exit 0
