#!/bin/sh /etc/rc.common
# Copyright (C) 2019 2agroCloud.com

USE_PROCD=1
START=99
PROG=/usr/bin/2agroCloud
# ra0 MAC is stored at offset 0x04 of the "factory" mtd partition,
# readable even when Wi-Fi is disabled
FACTORY=$(grep '"factory"' /proc/mtd | cut -d: -f1)
UDID=$(hexdump -v -n 6 -s 4 -e '6/1 "%02X"' /dev/$FACTORY 2>/dev/null)
case "$UDID" in
  ""|000000000000|FFFFFFFFFFFF)
    UDID=$(cat /sys/class/net/ra0/address | awk '{print toupper($0)}' | sed -e 's/://g')
    ;;
esac

wifi_enable_if_disabled() {
  # Wi-Fi already enabled — leave the config untouched
  if [ "$(uci -q get wireless.@wifi-device[0].disabled)" != "1" ] &&
     [ "$(uci -q get wireless.@wifi-iface[0].disabled)" != "1" ]; then
    return 0
  fi

  local hostname
  hostname=$(uci -q get system.@system[0].hostname)
  [ -n "$hostname" ] || hostname=$(cat /proc/sys/kernel/hostname)

  uci set wireless.@wifi-device[0].disabled=0
  uci -q set wireless.@wifi-iface[0].disabled=0
  uci set wireless.@wifi-iface[0].mode=ap
  uci set wireless.@wifi-iface[0].ssid="$hostname"
  uci commit wireless
  # uci commit only saves the file; netifd must re-apply the config
  wifi reload
}

# Route priority: Ethernet (metric 10) > Wi-Fi client (metric 20).
# When a link goes down its default route is removed and the other takes over.
network_set_priority() {
  local eth sta wifi changed=0

  # logical interface bound to eth0 (option ifname or option device)
  eth=$(uci -q show network | grep -E "\.(ifname|device)='eth0'$" | head -n1 | cut -d. -f2)
  # logical interface of the Wi-Fi client (STA) section
  sta=$(uci -q show wireless | grep -E "\.mode='sta'$" | head -n1 | cut -d. -f2)
  [ -n "$sta" ] && wifi=$(uci -q get wireless."$sta".network)

  set_opt() { # <iface> <option> <value>
    [ "$(uci -q get network."$1"."$2")" = "$3" ] && return
    uci set network."$1"."$2"="$3"
    changed=1
  }

  if [ -n "$eth" ]; then
    set_opt "$eth" metric 10
    # static proto defaults to force_link=1: route would stay without cable
    set_opt "$eth" force_link 0
  fi
  [ -n "$wifi" ] && set_opt "$wifi" metric 20

  # reload only on change: network reload drops active connections
  if [ "$changed" = 1 ]; then
    uci commit network
    /etc/init.d/network reload
  fi
}

start_service() {
  ifdown modem
  uci -q delete system.ntp.server
  uci -q add_list system.ntp.server=2aprm.agrom.local
  uci -q add_list system.ntp.server=10.44.2.250
  uci -q add_list system.ntp.server=10.44.2.251
#  uci -q add_list system.ntp.server=0.ru.pool.ntp.org
#  uci -q add_list system.ntp.server=1.ru.pool.ntp.org
#  uci -q add_list system.ntp.server=2.ru.pool.ntp.org
#  uci -q add_list system.ntp.server=3.ru.pool.ntp.org
  uci commit system
  /etc/init.d/sysntpd restart
  wifi_enable_if_disabled
  network_set_priority
  # see /etc/sysctl.d/99-2agrocloud.conf; applied here too right after opkg install
  sysctl -qw vm.overcommit_memory=1
  procd_open_instance 2agroCloud
  procd_set_param command $PROG $UDID /dev/ttyS1 /dev/ttyS0
  procd_set_param respawn 30 1 0
  procd_set_param stdout 1
  procd_set_param stderr 1
  procd_close_instance
}
