#!/bin/sh

linkup_fibocom=$(uci get system.@system[0].linkup_fibocom 2>/dev/null)
if [ -z "$linkup_fibocom" ]; then
    uci set system.@system[0].linkup_fibocom=1
    uci commit system
elif [ "$linkup_fibocom" = "0" ]; then
    exit 0
fi

####################################
# Section fancontrol
####################################

current_fancontrol_modem_temp_method="$(uci get fancontrol.@fancontrol[0].modem_temp_method 2>/dev/null)"
if [ "$current_fancontrol_modem_temp_method" != "AT" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set fancontrol.@fancontrol[0].modem_temp_method='AT'
fi

####################################
# Section 3ginfo
####################################

current_3ginfo_network="$(uci get 3ginfo.@3ginfo[0].network 2>/dev/null)"
if [ "$current_3ginfo_network" != "wan" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set 3ginfo.@3ginfo[0].network='wan'
fi

current_3ginfo_device="$(uci get 3ginfo.@3ginfo[0].device 2>/dev/null)"
if [ "$current_3ginfo_device" != "/dev/ttyUSB1" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set 3ginfo.@3ginfo[0].device='/dev/ttyUSB1'
fi

####################################
# Section modemband
####################################

current_modemband_iface="$(uci get modemband.@modemband[0].iface 2>/dev/null)"
if [ "$current_modemband_iface" != "wan" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set modemband.@modemband[0].iface='wan'
fi

current_modemband_port="$(uci get modemband.@modemband[0].set_port 2>/dev/null)"
if [ "$current_modemband_port" != "/dev/ttyUSB1" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set modemband.@modemband[0].set_port='/dev/ttyUSB1'
fi

current_modemband_wan_restart="$(uci get modemband.@modemband[0].wanrestart 2>/dev/null)"
if [ "$current_modemband_wan_restart" != "1" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set modemband.@modemband[0].wanrestart='1'
fi

####################################
# Section sms_tool_js
####################################

current_sms_tool_js_readport="$(uci get sms_tool_js.@sms_tool_js[0].readport 2>/dev/null)"
if [ "$current_sms_tool_js_readport" != "/dev/ttyUSB3" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set sms_tool_js.@sms_tool_js[0].readport='/dev/ttyUSB3'
fi

current_sms_tool_js_sendport="$(uci get sms_tool_js.@sms_tool_js[0].sendport 2>/dev/null)"
if [ "$current_sms_tool_js_sendport" != "/dev/ttyUSB3" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set sms_tool_js.@sms_tool_js[0].sendport='/dev/ttyUSB3'
fi

current_sms_tool_js_ussdport="$(uci get sms_tool_js.@sms_tool_js[0].ussdport 2>/dev/null)"
if [ "$current_sms_tool_js_ussdport" != "/dev/ttyUSB3" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set sms_tool_js.@sms_tool_js[0].ussdport='/dev/ttyUSB3'
fi

current_sms_tool_js_atport="$(uci get sms_tool_js.@sms_tool_js[0].atport 2>/dev/null)"
if [ "$current_sms_tool_js_atport" != "/dev/ttyUSB1" ] && [ "$linkup_fibocom" = "1" ]; then
    uci set sms_tool_js.@sms_tool_js[0].atport='/dev/ttyUSB1'
fi

# Commit all changes
uci commit fancontrol
uci commit 3ginfo
uci commit modemband
uci commit sms_tool_js

exit 0
