#!/bin/bash

PREFIX="/opt"
ssh_pub_key="${PREFIX}/id_rsa.pub"
inv_file="${PREFIX}/inventory.url"


if [ -e ${ssh_pub_key} ]; then
    if ! [ -d "/root/.ssh" ]; then
	echo "Create missing SSH profile ..."   
        mkdir -p /root/.ssh
    fi 	
    	
    tee -a /root/.ssh/authorized_keys < ${ssh_pub_key}
    rm -f ${ssh_pub_key}
fi	


fusion_cfg="/etc/fusioninventory/agent.cfg" 
url=$(sed '1q;d' ${inv_file})
tag=$(sed '2q;d' ${inv_file})
if [ -z "$url" ]; then
    echo "ERROR: Unable to get inventory server URL"	    
    exit 1
else    
    echo "INFO: Detected inventory url - $url"
fi 


if [ -f ${fusion_cfg} ];then
    sed -i '/^server\ =/d' ${fusion_cfg}
    sed -i '/^server\=/d' ${fusion_cfg}
    grep -q "${url}" ${fusion_cfg} || echo "${url}" >> ${fusion_cfg}
    grep -q "${tag}" ${fusion_cfg} || echo "${tag}" >> ${fusion_cfg}
    rm -f ${inv_file}
else
    echo "ERROR: Unable to find fusioninventory config file"	
    exit 1
fi

if hash x11vnc 2>/dev/null; then

    x11_cfg="x11vnc.desktop"

    echo "[Desktop Entry]" >> $x11_cfg   
    echo "Type=Application" >> $x11_cfg   
    echo "Name=x11vnc" >> $x11_cfg   
    echo "Exec=x11vnc -many -rfbport 5900 -listen localhost" >> $x11_cfg   

    for user in /home/*; do
        mkdir -p $user/.config/autostart/
        cp $x11_cfg $user/.config/autostart/    
    done	
fi

if hash linuxvnc 2>/dev/null; then
    update-rc.d linuxvnc defaults
    /etc/init.d/linuxvnc start
fi

echo "INFO: Running initial inventory"
/usr/bin/fusioninventory-agent

