# -*- mode: sh; sh-basic-offset: 2; indent-tabs-mode: nil; -*-
# vim:set ft=sh et sw=2 ts=2:
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2015-2024 Scott Shambarger
#
# dispatcher_action v1.4.0 - service restart on interface change
# Author: Scott Shambarger <devel@shambarger.net>
#
# This file supports the same functions as 96-interface-action, but allows
# re-ordering the action relative to other dispatchers.  However, it requires
# an extra config step.
#
# The extra step is creating a file named
#
#   /etc/NetworkManager/dispatcher.d/##-ifd-<service>
#
# (or wherever your distro has these files) where <##> is a 2-digit
# number, and <service> is a systemd service name.  The file should be
# executable and contain the following:
#
# --- start
# #!/bin/bash
# . /usr/share/nmutils/dispatcher_action
# --- end
#
# The configuration settings are documented in 96-interface-action, but
# those settings are should instead be placed in:
#
#   /etc/nmutils/ifd-<service>-<interface>.conf
#
# NOTE: If any PRE_DOWN actions are used, the ##-ifd-<service> script
#       should be symlinked to the pre-down.d directory.
#
# shellcheck shell=bash disable=SC1090
interface=${1-}
action=${2-}

########## SCRIPT START

# anything for us to do?
[[ ${interface} && ${action} ]] || exit 0

# check dispatcher name format for ##-ifd-service
base_name=${0##*/}
[[ ${base_name} =~ ^[0-9][0-9]-ifd-([^/]+)$ ]] || {
  echo >&2 "Invalid command name: ${base_name}" && exit 3
}
IFD_UNIT=${BASH_REMATCH[1]}

# shellcheck disable=SC2034
IFD_CONFIG="ifd-${IFD_UNIT}-${interface}.conf"

NMG_TAG=${NMG_TAG-nm-ifd}
IFA_FILE="/usr/lib/NetworkManager/dispatcher.d/96-interface-action"
{ [[ -r "${IFA_FILE}" ]] && . "${IFA_FILE}"; } || {
  echo >&2 "Unable to load ${IFA_FILE}" && exit 2
}

exit 0
