#!/bin/sh -e

if [ "$1" = "configure" ] ; then
  if [ ! -f /etc/default/instance_configs.cfg ]; then
    cp -a "/usr/share/${DPKG_MAINTSCRIPT_PACKAGE}/instance_configs.cfg" /etc/default/
  fi

  if [ ! -f "/usr/bin/google_guest_compat_manager" ]; then
    if [ -f "/etc/google-guest-agent/core-plugin-enabled" ] && [ ! -z $(grep "true" "/etc/google-guest-agent/core-plugin-enabled") ]; then
      # If the guest agent is disabled because core plugin is enabled, then
      # re-enable the guest agent. Otherwise it stays disabled post-upgrade.
      systemctl enable 'google-guest-agent.service' > /dev/null || true
      systemctl enable 'gce-workload-cert-refresh.timer' > /dev/null || true
    fi
  fi

  if grep -q "false" "/etc/google-guest-agent/core-plugin-enabled"; then
    # If the core plugin was disabled, honor that setting.
    systemctl enable google-guest-agent.service >/dev/null 2>&1 || :
    systemctl start google-guest-agent.service >/dev/null 2>&1 || :
  else
    # Disable and stop this service in favor of core plugin to be run on default.
    systemctl disable 'google-guest-agent.service' > /dev/null || true
    systemctl stop 'google-guest-agent.service' > /dev/null || true
  fi
fi

#DEBHELPER#
