#!/bin/bash
set -eux

install-packages -m openvswitch openvswitch-switch_package

if [ "$DIB_INIT_SYSTEM" == "systemd" ] ; then
    os-svc-enable -n openvswitch-switch
fi

if [ "$DIB_INIT_SYSTEM" == "upstart" ] ; then
    echo "start on starting cloud-init-nonet" >> /etc/init/openvswitch-switch.override
    if [ ! -f /etc/init/openvswitch-switch.conf ] ; then
        cat << 'EOF' > /etc/init/openvswitch-switch.conf
# openvswitch-switch
# the purpose of this job is
#  * start openvwitch-switch in upstart rather than SysV startup
pre-start script
      export RUNLEVEL=2
      /etc/init.d/openvswitch-switch start
end script
post-stop exec /etc/init.d/openvswitch-switch stop
EOF
    fi
fi
