#!/bin/sh /etc/rc.common

START=99
STOP=01
SERIAL_PORT="/dev/ttyUSB3"

start() {
    echo "Starting shutdown handler..."
}

stop() {
    BOARD_NAME="$(strings /proc/device-tree/compatible | head -1)"

    # This was `[ x"${BOARD_NAME}" ! = x"..."]`. "! =" is not an operator and
    # there was no space before the "]", so ash aborted the test with
    # "missing ]" and returned 2. The guard never held: every board fell
    # through to the modem check below and printed a shell error on the
    # console on every shutdown.
    [ "${BOARD_NAME}" = "ariaboard,rk3568-photonicat" ] || return 0

    # Only the Quectel EC25 wants an AT power-down. Look for it after the
    # board check so other boards do not pay for the lsusb at all; the FM350
    # fitted to photonicat2 does not match this id and is unaffected.
    BUGGY_MODEM="$(lsusb -d 2c7c:0125)"
    [ -n "${BUGGY_MODEM}" ] || return 0
    [ -c "${SERIAL_PORT}" ] || return 0

    echo "Sending shutdown AT command to module..."
    echo -en "AT+QPOWD\r\n" >"${SERIAL_PORT}"
    sleep 3
}

shutdown() {
    stop
}
