#!/bin/bash
set -eux

install-packages python-pip build-essential libz-dev libxslt-dev libxml2-dev python-dev

pip install /opt/stack/os-collect-config

# Minimal static config for bootstrapping
cat > /etc/os-collect-config.conf <<eof
[DEFAULT]
command=os-refresh-config
eof

# Upstart
if [ -d /etc/init ] ; then
    cat > /etc/init/os-collect-config.conf <<eof
start on runlevel [2345]
stop on runlevel [016]
respawn
exec os-collect-config
eof

# Systemd
elif [ -d /lib/systemd/system ] ; then
    cat > /lib/systemd/system/os-collect-config.service <<eof
[Unit]
Description=Collect metadata and run hook commands.
After=cloud-final.service
Before=crond.service

[Service]
ExecStart=/bin/os-collect-config
Restart=on-failure

[Install]
WantedBy=multi-user.target
eof

  # Make systemd take notice of it
  systemctl daemon-reload

  # Enable the service
  systemctl enable os-collect-config.service

else
    echo Only systems with systemd or upstart are supported.
    exit 1
fi
