#!/bin/sh

if [ "$PRODUCT" = 'e8d/7126/1' ] || [ "$PRODUCT" = 'e8d/7127/1' ] &&  [ "${DEVICENAME: -3}" = '1.6' ] && [ "$ACTION" = 'bind' ]; then
    logger -t 'Fibocom FM350' 'has crashed!'

    status_file="/tmp/fm350.status"
    crash_count=$(cat "$status_file" 2>/dev/null || echo 0)
    echo $((crash_count + 1)) > "$status_file"

    wanIface=$(uci show network | awk -F '[.=]' '/proto='\''atc'\''/ {print $2}')
    
    if [ -n "$wanIface" ]; then
        logger -t 'Fibocom FM350' "Restarting $wanIface interface..."
        sleep 2
        ifup "$wanIface"
    else
        logger -t 'Fibocom FM350' 'No WAN interface found for ATC protocol!'
    fi
fi