#!/sbin/openrc-run

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

# This script manages xg_multi usb-gadget setup

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

command_background=true
pidfile="/run/${RC_SVCNAME}.pid"

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

start() {
	ebegin "Enabling gadgets"
	if [ -z "$XG_MULTI_OPTS" ]; then
		[ -n "$XG_MULTI_DEV_MAC" ] && XG_MULTI_OPTS="$XG_MULTI_OPTS -D $XG_MULTI_DEV_MAC"
		[ -n "$XG_MULTI_HOST_MAC" ] && XG_MULTI_OPTS="$XG_MULTI_OPTS -H $XG_MULTI_HOST_MAC"
		[ -n "$XG_MULTI_VOLUME" ] && XG_MULTI_OPTS="$XG_MULTI_OPTS -V $XG_MULTI_VOLUME"
	fi

	/usr/bin/xg_multi -V /dev/mmcblk0p1
	eend $?
}

stop() {
	ebegin "Removing gadgets"
	/usr/bin/xg_multi -r
	eend $?
}

