#!/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.
#
# replace existing discovery ucitrack entry

if [ -f "/etc/config/consoleflow" ]; then
    host=`uci get consoleflow.Connection_1.Host 2>/dev/null`
    if [ "$host" == "api.consoleflow.com" ]; then
        uci set consoleflow.Connection_1.Host="api.percepxion.ai"
    fi
    host=`uci get consoleflow.Connection_2.Host 2>/dev/null`
    if [ "$host" == "api.consoleflow.com" ]; then
        uci set consoleflow.Connection_2.Host="api.percepxion.ai"
    fi
    host=`uci get consoleflow.Connection_1.MQTT_Host 2>/dev/null`
    if [ "$host" == "mqtt.consoleflow.com" ]; then
        uci set consoleflow.Connection_1.MQTT_Host="mqtt.percepxion.ai"
    fi
    host=`uci get consoleflow.Connection_2.MQTT_Host 2>/dev/null`
    if [ "$host" == "mqtt.consoleflow.com" ]; then
        uci set consoleflow.Connection_2.MQTT_Host="mqtt.percepxion.ai"
    fi
    uci commit consoleflow
    mv /etc/config/consoleflow /etc/config/percepxion

fi

uci -q batch <<-EOF >/dev/null
    del ucitrack.@consoleflow[-1]
    del ucitrack.@percepxion[-1]
    add ucitrack percepxion
    add_list ucitrack.@percepxion[-1].affects="auditwatch"
    set ucitrack.@percepxion[-1].exec="/etc/init.d/percepxion reload"
    commit ucitrack
EOF

# remove LuCI cache
rm -rf /tmp/luci-indexcache /tmp/luci-modulecache

exit 0
