#!/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.@serial3[-1]
   	add ucitrack serial3
    set ucitrack.@serial3[-1].exec="/etc/init.d/serialinit4 reload"
    commit ucitrack
EOF
else
cp /etc/config/serial3 /etc/config/serial2
uci set serial2.Line.path='/dev/rfcomm1'
uci set serial2.Accept.Local_Port='10003'
uci commit serial2
# 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/serialinit4 reload"
    commit ucitrack
EOF
fi
# remove LuCI cache
rm -rf /tmp/luci-indexcache /tmp/luci-modulecache

exit 0
