#!/bin/sh

[ -f /etc/openwrt_release ] || exit 1

log_console() {
    if [ ! -t 0 ] ; then
        echo "$@"  > /dev/console
    fi
}

echo "Configurator started!"


if [ -z "$UID" ] ; then
    UID=$(dd if=/dev/mtd2 ibs=1 skip=768 count=10 | tr -d "\377" | tr -d "\000")
fi

if [ -z "$UID" ] ; then
        echo "UID not found."
else
    echo "Current UID: $UID"
    log_console "Current UID: $UID"
fi


#uci set openvpn.vendotek.enabled='0'
#uci commit
#/etc/init.d/openvpn stop
#/etc/init.d/openvpn disable
#log_console "VPN Disabled."

uci del_list network.lan.dns='8.8.8.8'
uci commit network

uci set dhcp.@domain[-1].name="flaps.vendotek.eu"
uci show|grep domain
uci set dhcp.@domain[-1].ip="78.47.130.116"
uci show|grep domain
uci commit dhcp


sed -i -e s:"PING_INTERVAL=600":"PING_INTERVAL=900":g /etc/init.d/heartbeat2

[ -f "/etc/init.d/heartbeat2" ] && exit 0

echo "NEW CHANGES"
wget -4 -O /tmp/heartbeat2 '--timeout=10' --quiet --no-check-certificate https://openwrt.vendotek.eu/files/heartbeat2

if [ -f "/tmp/heartbeat2" -a -s "/tmp/heartbeat2" ] ; then
    mv /tmp/heartbeat2 /etc/init.d/heartbeat2
    chmod 755 /etc/init.d/heartbeat2
    chown root:root /etc/init.d/heartbeat2
    /etc/init.d/heartbeat stop
    /etc/init.d/heartbeat disable
    /etc/init.d/heartbeat2 enable
    /etc/init.d/heartbeat2 start
fi



echo "Configurator finished!"
echo "********** CONFIGURATOR FINISHED  **********" > /dev/console
exit 0
