# On Azure/Hyper-V systems start the hv_kvp_daemon
#
description "Hyper-V KVP Protocol Daemon"
author "Ben Howard <ben.howard@canonical.com>"

start on runlevel [2345]
console log

pre-start script
        [ -e "/etc/default/hv-kvp-daemon-init" ] &&
                . /etc/default/hv-kvp-daemon-init

        [ "$RUN_DAEMON" -eq 0 ] &&
                exit 0
        KVP="/usr/sbin/hv_kvp_daemon_$(awk '{ sub(/\.([0-9]*)-[a-z].*/, "" ); sub(/-[a-z].*/, ""); print$3 }' /proc/version)"
        KVPLBM="${KVP}_lbm"

        if [ -x "$KVPLBM" ]; then
                exec $KVPLBM || { stop; exit 1; }
        elif [ -x "$KVP" ]; then
                exec $KVP || { stop; exit 1; }
        else
            stop; exit 1
        fi

end script

post-stop script
        KVP="/usr/sbin/hv_kvp_daemon_$(awk '{ sub(/\.([0-9]*)-[a-z].*/, "" ); sub(/-[a-z].*/, ""); print$3 }' /proc/version)"
        KVPLBM="${KVP}_lbm"

        if [ -x "$KVPLBM" ]; then
                kill -9 `pidof $KVPLBM`
        elif [ -x "$KVP" ]; then
                kill -9 `pidof $KVP`
        fi
end script
