#!/bin/false
# For GW6 and GWM devices

initGPSVar() {
    GPSENABLED=no
    PORT=CNone
    . '/etc/telem-gps.conf'
}

initGPSVarAll() {
    initGPSVar
    GPSTYPE='NMEA'
    PORTINDEX="${PORT:1:1}"
    if [ "${PORT}" = "Optical" ]; then
        GPSTYPE='GW6RTC'
        PORTINDEX=14
    fi
    # PPS
    local PPS_NAME="$(ppsfind pps | cut -d: -f1)"
    DEV_PPS="/dev/${PPS_NAME:-pps0}"
    # GPS
    local HW_XML='/usr/local/etc/telem/hw-run.xml'
    local tmp="$(ls -1 "$(grep "index=\"${PORTINDEX}\"" "${HW_XML}" | sed 's/^.*name=\"\([^ ]*\)\".*/\1/')" | grep -F tty)"
    TTY_GPS="$(ls -1 "/dev/$tmp" "$tmp" 2>/dev/null)"
}

isGPSttyEnabled() {
    if [ -f '/etc/telem-gps.conf' ]; then
        (
            initGPSVar
            # test
            [ "${GPSENABLED}" = "yes" ] || return 1
            [ "${PORT}" = "C1" ] || [ "${PORT}" = "Optical" ] || return 1
        )
        return
    fi
    false
}

getPPSdev() {
    # Usually pps0 is pps, but imx6dl has ptp0 as pps0
    # so we need to use ppsfind
    local PPS_NAME="$(ppsfind pps | cut -d: -f1)"
    echo "/dev/${PPS_NAME:-pps0}"
}

getGPStty() {
    # GW6 and GWM have only C1 for GPS (excluding legacy)
    local HW_XML='/usr/local/etc/telem/hw-run.xml'
    ls $(grep index=\"1\" "${HW_XML}" | sed 's/^.*name=\"\([^ ]*\)\".*/\1/') | grep tty
}
