#!/bin/bash

phy_from_ifname() {
    local ifname=$1
    local device="/sys/class/net/$ifname"

    if ! [ -d "$device" ]; then
        echo "not exist: ifname '$ifname', device '$device'" >&2
        return 1
    fi

    device="$(realpath "$device")"
    while true;
    do
        local a

        a="$(dirname "$device")"
        if [ "$(basename "$a")" == "net" ]; then
            device="$(dirname "$a")"
            continue
        fi

        a="$device/subsystem"
        if [ -d "$a" ] && [ "$(realpath "$a")" == "/sys/bus/virtio" ]; then
            device="$(dirname "$device")"
            continue
        fi

        break
    done

    local subsystem="$(realpath "$device/subsystem")"
    local device_path

    case $subsystem in
        "/sys/bus/platform" | "/sys/bus/sdio")
            device_path="$(basename "$device")"
            ;;
        "/sys/bus/pci")
            device_path="$(basename "$device")"
            ;;
        "/sys/bus/usb")
            device_path="$(basename "$(dirname "$device")")"
            ;;
        *)
            echo "unknown subsystem: device '$device', subsystem '$subsystem'" >&2
            return 1
            ;;
    esac
    device="$subsystem/devices/$device_path"

    echo "$device"
    return 0
}
usb_get_vendor_product_id() {
    local device="$1"
    local vendor_id="$(cat $device/idVendor 2> /dev/null)"
    local product_id="$(cat $device/idProduct 2> /dev/null)"

    if [ "$vendor_id" == "" ] || [ "$product_id" == "" ]; then
        echo "invalid device: device '$device'" >&2
        return 1
    fi

    echo "$vendor_id:$product_id"
    return 0
}
main() {
    local ifname="$1"
    local phy="$(phy_from_ifname "$ifname")"

    if [ "$phy" == "" ]; then
        return 1
    fi

    local phy_subsystem="$(realpath "$phy/subsystem")"

    if [ "$phy_subsystem" != "/sys/bus/usb" ]; then
        echo "unsupported phy_subsystem: ifname '$ifname', phy '$phy', phy_subsystem '$phy_subsystem'" >&2
        return 1
    fi

    local vendor_product_id="$(usb_get_vendor_product_id "$phy")"

    if [ "$vendor_product_id" == "" ]; then
        return 1
    fi

    local at_interface

    case $vendor_product_id in
        "1e0e:9011") # A7600C
            at_interface="1.4"
            ;;
        "2c7c:0306") # EM06, EP06
            at_interface="1.3"
            ;;
        "2c7c:0800") # RM500Q-GL
            at_interface="1.3"
            ;;
        "2c7c:0801") # RM520N-CN, RM520N-GL
            at_interface="1.3"
            ;;
        "2c7c:0900") # RG200U-CN, RM500U-CN
            at_interface="1.5"
            ;;
        "2c7c:6005") # EC200A-CN
            at_interface="1.4"
            ;;
        *)
            echo "unknown model: ifname '$ifname', phy '$phy', vendor_product_id '$vendor_product_id'" >&2
            return 1
            ;;
    esac

    local at_tty_name

    at_tty_name="$(ls -d $phy/$(basename $phy):$at_interface/ttyUSB*)"
    if [ "$at_tty_name" == "" ]; then
        echo "get at_tty_name failed: ifname '$ifname', phy '$phy', vendor_product_id '$vendor_product_id'"
        return 1
    fi
    at_tty_name="$(basename "$at_tty_name")"

    local at_tty="/dev/$at_tty_name"

    echo "$at_tty"
    return 0
}
main "$1"

