#!/bin/sh

#
# This script is a fake 'agent-smith' program.
#
# When 'upgrade' is sent to 'agent-smith' and it is given a nova-agent
# tarball to upgrade to, it'll download it, and just call agent-smith's
# init script with 'restart'.  agent-smith's init script will de-install
# agent-smith, untar the new tarball, and then try to start
# /usr/sbin/agent-smith
#
# So, we need this program to be installed as /usr/sbin/agent-smith,
# which will take care of removing agent-smith's init script, install
# nova-agent's init script, and then run it.  When done, this script
# will remove itself.

add_to_startup() {
    if [ -f /usr/sbin/update-rc.d ] ; then
        /usr/sbin/update-rc.d nova-agent start 20 2 3 4 5 . stop 20 0 1 6 . > /dev/null 2>&1
    elif [ -f /sbin/chkconfig ] ; then
        /sbin/chkconfig nova-agent on > /dev/null 2>&1
    elif [ -f /sbin/rc-update ] ; then
        /sbin/rc-update add nova-agent default > /dev/null 2>&1
    elif [ -f /etc/arch-release ] ; then
        if [[ "$(basename $(readlink /sbin/init))" == systemd ]]; then
            /usr/bin/systemctl enable nova-agent.service > /dev/null 2>&1
        else
            # Arch is a hybrid BSD and SysV style init system. We just need to
            # install a SysV style script into /etc/rc.d, but then update
            # /etc/rc.conf to enable it
            grep -q '^DAEMONS=.*nova-agent' /etc/rc.conf
            if [ $? -ne 0 ] ; then
                sed -i.backup 's/^DAEMONS=(\(.*\))/DAEMONS=(nova-agent \1)/' /etc/rc.conf
            fi
        fi
    elif [ `uname -s` == 'FreeBSD' ] ; then 
        # FreeBSD needs nova_agent_enable="YES" added to rc.conf
        egrep -q '^nova-agent_enable=.*YES' /etc/rc.conf
        if [ $? -ne 0 ] ; then
            sed -i.backup 's/^.*nova_agent.*//' /etc/rc.conf
            cat >> /etc/rc.conf << EOF 
# DO NOT DISABLE nova_agent or you will lose some functionality
nova_agent_enable="YES"
EOF
        fi      
    elif [ -f /etc/rc.local ] ; then
        grep -q nova-agent /etc/rc.local
        if [ $? -ne 0 ] ; then
            if [ -d /etc/rc.d ]; then
                echo "/etc/rc.d/nova-agent start" >> /etc/rc.local
            elif [ -d /etc/init.d ]; then
                echo "/etc/init.d/nova-agent start" >> /etc/rc.local
            else 
                echo "/etc/nova-agent.init start" >> /etc/rc.local
            fi      
        fi      
    fi              
}

update-rc.d agent-smith remove > /dev/null 2>&1
update-rc.d agent-smith.init remove > /dev/null 2>&1
chkconfig agent-smith --del > /dev/null 2>&1
chkconfig agent-smith.init --del > /dev/null 2>&1
rm -f /etc/init.d/agent-smith
rm -f /etc/init.d/agent-smith.init
rm -f /etc/systemd/system/nova-agent.service


# This forks into the background and looks for the old agent script
# to write out a response into the spool dir.  We catch it, and force
# the response into the xenstore.. and then remove the spool dir
(
    tries=0
    while [ $tries -lt 20 ] ; do
        old_agent_response=`find /var/spool/agent-smith/incoming -type f | tail -1`
        if [ "x${old_agent_response}" != "x" ]; then
            break
        fi
        tries=`expr $tries + 1`
        sleep 1
    done

    old_agent_response=`find /var/spool/agent-smith/incoming -type f | tail -1`

    uuid=`basename ${old_agent_response}`

    xenstore-write data/guest/${uuid} "`cat ${old_agent_response}`"

    rm -rf /var/spool/agent-smith
    rm -f /var/log/agent-smith.log
) &

# Remove /installer.sh from nova-agent tarball
rm -f /installer.sh

# Install the init file symlink
if [ -d /etc/init.d ]; then
    # Linux (CentOS,Debian,Gentoo)
    rm -f /etc/init.d/nova-agent
    init_script=`find /usr/share/nova-agent -name nova-agent.init | head -1`

    ln -s ${init_script} /etc/init.d/nova-agent
elif [ -f /etc/arch-release ] ; then
    if [[ "$(basename $(readlink /sbin/init))" == systemd ]]; then
        rm -f /etc/systemd/system/nova-agent.service
        rm -f /etc/rc.d/nova-agent
        init_script=`find /usr/share/nova-agent -name nova-agent.service | head -1`
        ln -s ${init_script} /etc/systemd/system/nova-agent.service
    else
        rm -f /etc/rc.d/nova-agent
        init_script=`find /usr/share/nova-agent -name nova-agent.init | head -1`
        ln -s ${init_script} /etc/rc.d/nova-agent
    fi
elif [ -d /etc/rc.d ]; then
    # FreeBSD
    rm -f /etc/rc.d/nova-agent
    init_script=`find /usr/share/nova-agent -name nova-agent.freebsd.init | head -1`
    ln -s ${init_script} /etc/rc.d/nova-agent
else
    # Linux (Arch)
    rm -f /etc/nova-agent.init
    init_script=`find /usr/share/nova-agent -name nova-agent.init | head -1`
    ln -s ${init_script} /etc/nova-agent.init
fi

# Make sure we actually run the init script on boot
add_to_startup

${init_script} start

rm -f /usr/sbin/agent-smith

exit 0
