#!/bin/bash
set -eux

if [ "$DIB_INIT_SYSTEM" == "upstart" ] ; then
    cat > /etc/init/tripleo-cd.conf <<eof
start on runlevel [2345]
stop on runlevel [016]
respawn
exec tripleo-cd.sh
eof

elif [ "$DIB_INIT_SYSTEM" == "systemd" ] ; then
    cat > /lib/systemd/system/tripleo-cd.service <<eof
[Unit]
Description=Deploy an overcloud forever.
After=cloud-final.service
Before=crond.service

[Service]
ExecStart=/usr/local/bin/tripleo-cd.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
eof

  # Enable the service
  systemctl enable tripleo-cd.service

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

mkdir -p ~root/.cache/image-create
ln -s /var/cache/pypi-mirror/pypi ~root/.cache/image-create

for f in `ls $(dirname $0)/../bin`; do
  install -m 0755 -o root -g root $(dirname $0)/../bin/$f /usr/local/bin/$f
done
