#!/sbin/openrc-run

# SPDX-FileCopyrightText: Copyright 2025-2026, macmpi
# SPDX-License-Identifier: MIT

# This script manages xg_multi usb-gadget setup

description="Extended Multifunction Composite USB-gadget"
name="xg_multi"

depend() {
	use logger
    want dev-settle
    need localmount
    before net agetty.ttyGS0
    after bootmisc hwdrivers modules
}

start_pre() {
	XG_MULTI_OPTS="${XG_MULTI_OPTS:-$DEV_MAC $HOST_MAC $VOLUME $WATCH}"
}

start() {
	ebegin "Enabling gadgets"
	/usr/bin/xg_multi $XG_MULTI_OPTS >/dev/null 2>&1 &
	echo $! > "/run/${RC_SVCNAME}.pid"
	eend 0
}

stop() {
	ebegin "Removing gadgets"
	if [ -s "/run/${RC_SVCNAME}.pid" ]; then
        	kill $(cat "/run/${RC_SVCNAME}.pid") 2>/dev/null
        	rm -f "/run/${RC_SVCNAME}.pid"
	fi
	/usr/bin/xg_multi -r >/dev/null 2>&1
	eend 0
}
