#!/usr/bin/bash

[ $# -eq 2 ] || exit 1

here=$(dirname "$0")

#shellcheck source=lib/udev/enyx-hfp-udev-functions
. "$here/enyx-hfp-udev-functions"

action=$1
shift

devpath="/sys$1"
shift

handle_mm() {
    local bus_index
    bus_index=$(find_attr 'enyx_hfp_bus[0-9]*' 'index' "$devpath")

    [ -n "$bus_index" ] || exit 1

    if [ "$action" = "add" ]; then
        "$here/enyx-hfp-lock-manager" create mm "$bus_index" 0
    else
        "$here/enyx-hfp-lock-manager" destroy mm "$bus_index" 0
    fi
}

case $(basename "$devpath") in
    enyx_hfp_mm[0-9]*)
        handle_mm
        ;;
    *)
        exit 1
        ;;
esac

