#!/bin/sh
#
# Copyright (C) 2014-2020, Lantronix, Inc. All Rights Reserved.
#           7535 Irvine Center Drive, Suite 100
#           Irvine, CA 92618 USA

# Permission to use, copy, modify this software for any
# purpose is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.

# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
id=`cat /proc/device-tree/lantronix/product_id 2>/dev/null | tr -d '\n'`
sku=`cat /proc/device-tree/lantronix/hwcfg 2>/dev/null | grep -eG526GP1AS1 -eG526GP12S1 -eG526GP17S1 -eG526GP1CS1`
if [ "$sku" == "" -a "$id" == "U1" ]; then
# replace existing serial ucitrack entry
uci -q batch <<-EOF >/dev/null
    del ucitrack.@serial2[-1]
    add ucitrack serial2
	set ucitrack.@serial2[-1].exec="/etc/init.d/serialinit3 reload"
    commit ucitrack
EOF
else
# replace existing serial ucitrack entry
cp /etc/config/serial2 /etc/config/serial1
uci set serial1.Line.path='/dev/rfcomm0'
uci set serial1.Accept.Local_Port='10002'
uci commit serial1
uci -q batch <<-EOF >/dev/null
    del ucitrack.@serial1[-1]
    add ucitrack serial1
    set ucitrack.@serial1[-1].exec="/etc/init.d/serialinit3 reload"
    commit ucitrack
EOF
fi
# remove LuCI cache
rm -rf /tmp/luci-indexcache /tmp/luci-modulecache

exit 0
