#!/bin/bash
set -euo pipefail

max_try=3
try_count=0

while (( try_count < max_try )); do
    if lsusb | grep -qE "(6615:0084|6615:0085|6615:0086|6615:0087|6615:0088|6615:0c20)"; then
        modprobe OpticalDrv
        exec /usr/bin/OpticalService
    elif lsusb | grep -qE "(2621:2201|2621:4501)"; then
        modprobe OtdDrv
        exec /usr/bin/OtdTouchServer.$(uname -m)
    fi

    ((try_count++))
    sleep 0.5
done

exit 1