#!/bin/sh
# Hotplug script for ModemManager events.  Place in /etc/hotplug.d/iface/13-keepalive_modemmanager
# and give execute permission.  
#
# Copyright 2019 Nicholas Smith <mips171@icloud.com>
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

MODEMIFACE="LTE"

modemstatus=$(/usr/bin/mmcli -m 0)
DISABLED=$(echo $modemstatus | grep -w "disabled")
SEARCHING=$(echo $modemstatus | grep -w "searching")
REGISTERED=$(echo $modemstatus | grep -w "registered")
CONNECTED=$(echo $modemstatus | grep -w "connected")
IDLE=$(echo $modemstatus | grep -w "idle")

RUNNING="0" # Used to keep incidental events during the execution of one branch from triggering another branch.

if [ -n "$DISABLED" ]; then
	logger -t INFO	"Modem is disabled. Will attempt to initiate connection."
	logger -t DEBUG "$DISABLED"
	if [ "$RUNNING" -eq "0" ]; then
		$RUNNING="1"
		ifdown $MODEMIFACE && ifup $MODEMIFACE && $RUNNING="0"
	fi
fi

if [ -n "$CONNECTED" ]; then
	if [ "$(/bin/ping -I wwan0 -c 1 8.8.8.8 &> /dev/null ; echo $?)" -eq "1" ]; then # 1 is status code for host unreachable
		if [ "$RUNNING" -eq "0" ]; then
			logger -t INFO "Modem failed ping test. Reconnecting."
			RUNNING="1"
			ifdown $MODEMIFACE && ifup $MODEMIFACE && RUNNING="0"
		else
			logger -t INFO "Modem is connected."
			logger -t INFO "$CONNECTED"
		fi
	fi
fi

if [ -n "$SEARCHING" ]; then
	if [ "$RUNNING" -eq "0" ]; then
		logger -t INFO "Modem is searching.  Attempting to bring modem back online."
		$RUNNING="1"
		ifdown $MODEMIFACE && ifup $MODEMIFACE && RUNNING="0"
	fi
fi

if [ -n "$IDLE" ]; then
        if [ "$RUNNING" -eq "0" ]; then
                $RUNNING="1"
                logger -t INFO "Modem is idle.  Attempting to bring modem back online."
                ifdown $MODEMIFACE && ifup $MODEMIFACE && $RUNNING="0"
        fi
fi

if [ -n "$REGISTERED" ]; then
	if [ "$RUNNING" -eq "0" ]; then
		$RUNNING="1"
		logger -t INFO "Modem is registered. Attempting to bring modem back online."
		ifdown $MODEMIFACE && ifup $MODEMIFACE && $RUNNING="0"
	fi
fi

