#!/bin/bash

# Copyright (c) 2013 The CoreOS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# TODO: Make this a service in Go that coordinates between a host group

interface=org.chromium.UpdateEngineInterface
member=StatusUpdate

reboot_tasks() {
  sync
  echo "$0 rebooting machine after update" > /dev/kmsg
  shutdown -r -t 60
}

filter="type=signal,interface='$interface',member='$member'"

dbus-monitor --system ${filter} |
while read -r line; do
  echo $line | grep -q "UPDATE_STATUS_UPDATED_NEED_REBOOT" && reboot_tasks
done
