#!/bin/bash
#
#       Copyright (C) MOXA Inc. All rights reserved.
#       Copyright (C) 2016       Wes Huang <Wes.Huang@moxa.com>
#
#       This software is distributed under the terms of the
#       MOXA License.  See the file COPYING-MOXA for details.
#
#       set_module_cfg
#

killall -9 chk_signal 2> /dev/null
count=0
while [ X"$count" != X"3" ]
do
	count=$((count + 1))
	sleep 5
	
	# Sierra MC73x4
	if dmesg | grep "idProduct=68c0" > /dev/null; then
		test -e /etc/moxa-cellular-utils/wvdial/mc73xx.conf.example && ln -sf /etc/moxa-cellular-utils/wvdial/mc73xx.conf.example /etc/wvdial.conf
		
		# Check module's mtu.
	    	cell_mgmt at 'ATE0 2'
	    	cell_mgmt at 'AT!entercnd="A710" 2'
		if ! cell_mgmt at 'AT!scummtu?' | grep 1500; then
			cell_mgmt at 'AT!scummtu=1500 2'
			cell_mgmt at 'AT!reset'	
		fi
		chk_signal >/dev/null 2>&1 &
		exit 0
	fi

	# Sierra MC7455
        if dmesg | grep "idProduct=9071" > /dev/null; then
                # Check module's mtu.
                cell_mgmt at 'ATE0 2'
                cell_mgmt at 'AT!entercnd="A710" 2'
                if ! cell_mgmt at 'AT!scummtu?' | grep 1500; then
                        cell_mgmt at 'AT!scummtu=1500 2'
                        cell_mgmt at 'AT!reset'
                fi
                chk_signal >/dev/null 2>&1 &
                exit 0
        fi

	# YUGA CLM920-CN3
        if dmesg | grep "idProduct=9025" > /dev/null; then
                chk_signal >/dev/null 2>&1 &
                exit 0
        fi
	
	# Sierra MC9090
	if dmesg | grep "idProduct=9011" > /dev/null; then
	    	test -e /etc/moxa-cellular-utils/wvdial/wvdial.conf.template && ln -sf /etc/moxa-cellular-utils/wvdial/wvdial.conf.template /etc/wvdial.conf
	    	chk_signal >/dev/null 2>&1 &
	    	exit 0
	fi

	# U-blox L210 L280
	if dmesg | grep -E "idProduct=1140|idProduct=1141" > /dev/null; then
		test -e /etc/wvdial.conf && rm -rf /etc/wvdial.conf
	    	chk_signal >/dev/null 2>&1 &
	    	exit 0
	fi
	
	# 172GN WiFi Module
	if dmesg | grep "idProduct=5370" > /dev/null; then
	    	chk_signal "wifi" >/dev/null 2>&1 &
	    	exit 0
	fi
done

# No module is supported by wvdial.
# Delete the config file.
test -e /etc/wvdial.conf && rm -rf /etc/wvdial.conf
exit 0

