#!/bin/sh

# Use the USB role class: ash does not expand globs in redirection targets.
# Select only the OneCloud OTG controller; the capture port stays in host mode.
role=/sys/class/usb_role/c9040000.usb-role-switch/role
for attempt in 1 2 3 4 5; do
	if [ -f "$role" ]; then
		[ "$(cat "$role")" = device ] && exit 0
		if printf '%s\n' device > "$role" && [ "$(cat "$role")" = device ]; then
			logger -t one-kvm-platform 'OneCloud OTG role set to device'
			exit 0
		fi
	fi
	sleep 1
done

logger -t one-kvm-platform 'Failed to set OneCloud OTG role to device'
exit 1
