#!/bin/false
# For AGC-BASE and AGC-BASE-SFP devices

initGPSVar() {
    GPSENABLED=no
    GPSFLAG3=0
    . '/etc/telem-gps.conf'
    PORT="${PORT:-C4}"
}

initGPSVarAll() {
    initGPSVar
    GPSTYPE='NMEA'
    PORTINDEX="${PORT:1:1}"
    # 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'
    #TTY_GPS="$(ls -1 $(grep "index=\"${PORTINDEX}\"" "${HW_XML}" | sed 's/^.*name=\"\([^ ]*\)\".*/\1/') | grep tty)"
    case "${PORT}" in
        C1) TTY_GPS="/dev/ttymxc1"
        ;;
        *)  TTY_GPS="/dev/ttymxc4"
        ;;
    esac
}

isGPSttyEnabled() {
    if [ -f '/etc/telem-gps.conf' ]; then
        (
            initGPSVar
            # test
            [ "${GPSENABLED}" = "yes" ] || return 1
            # AGC-BASE has C4 and C1 (rev5+) for GPS
            # AGC-BASE-SFP has C3 port
            # C3 & C4 are internally ttymxc4
            [ "${PORT}" = "C1" ] ||
            [ "${PORT}" = "C3" ] ||
            [ "${PORT}" = "C4" ] || 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() {
    initGPSVar
    #local HW_XML='/usr/local/etc/telem/hw-run.xml'
    #ls $(grep "index=\"${PORT:1:1}\"" "${HW_XML}" | sed 's/^.*name=\"\([^ ]*\)\".*/\1/') | grep tty
    case "${PORT}" in
        C1) echo "/dev/ttymxc1"
        ;;
        *)  echo "/dev/ttymxc4"
        ;;
    esac
}
