#!/bin/sh

MODEL=$(cat /barix/apps/epic-sip/MODEL)

# step 1:
# initial batch of units shipped without epic.io section
# we need to make sure it exists for proper operation
if [ -z "$(uci -q get epic.io)" ]; then
    uci set epic.io='section'
    uci set epic.io.notify_relay1='false'
    uci set epic.io.notify_relay2='true'
    uci commit
fi


if [ -z "$(uci -q get epic.sip.input_gain)" ]; then
    uci set epic.sip.input_gain='100'
    uci commit
fi


if [ -z "$(uci -q get epic.sip.hdx_level)" ]; then
    uci set epic.sip.hdx_level='0.05'
    uci set epic.sip.hdx_timeout='600'
    uci commit
fi


if [ -z "$(uci -q get epic.main.device_alias)" ]; then
    uci set epic.main.device_alias="$MODEL"
    uci commit
fi


if [ -z "$(uci -q get epic.main.description)" ]; then
    uci set epic.main.description="System Description: $MODEL"
    uci commit
fi


if [ -z "$(uci -q get epic.snmp)" ]; then
    uci set epic.snmp='snmp'
    uci set epic.snmp.enable='false'
    uci commit
fi


# Create the snmpd.conf
echo "sysObjectID .1.3.6.1.4.1.17491.1.161" > /etc/snmp/snmpd.conf

FW_VER=$(cat /barix/info/VERSION)
echo "sysDescr \"$MODEL $FW_VER\"" >> /etc/snmp/snmpd.conf

SYS_NAME=$(uci get epic.main.device_alias)
echo "sysName \"$SYS_NAME\"" >> /etc/snmp/snmpd.conf

echo "view customView included .1.3.6.1.2.1.1.1" >> /etc/snmp/snmpd.conf
echo "view customView included .1.3.6.1.2.1.1.2" >> /etc/snmp/snmpd.conf
echo "view customView included .1.3.6.1.2.1.1.3" >> /etc/snmp/snmpd.conf
echo "view customView included .1.3.6.1.2.1.1.5" >> /etc/snmp/snmpd.conf
echo "view customView included .1.3.6.1.4.1.17491.1.161" >> /etc/snmp/snmpd.conf

echo "rocommunity AESNMP1978 default -V customView" >> /etc/snmp/snmpd.conf
echo "pass_persist .1.3.6.1.4.1.17491.1.161 /barix/apps/epic-sip/SNMP/snmp_controller.py" >> /etc/snmp/snmpd.conf

/etc/init.d/snmpd restart

# UCI_MODEL=$(uci -q get "epic.main.model")

#if [ "$MODEL" != "$UCI_MODEL" ]; then
#     cp /barix/config/defaults/epic /barix/config/current/epic
#fi

if [ "$MODEL" != "" ]; then
    uci -q set "epic.sip.model=$MODEL"
    uci commit
    sed -i "s/##PRODUCT##/$MODEL/g" /etc/lldpd.conf
fi

true
