function __game_on_init_axis_connection() {
  local brew_prefix camera_ip config dev host_ip i iface info name port separator
  local -a devices services

  brew_prefix=$(brew --prefix)
  camera_ip=192.168.1.64
  config=$brew_prefix/etc/kea/kea-dhcp4.conf
  host_ip=192.168.1.1
  separator=${(l:82::=:)}

  [[ -n $GAME_ON_SCRIPT_INFO ]] && printf "%s\n\n%s\n\n" "$separator" "$GAME_ON_SCRIPT_INFO"

  [[ -x $brew_prefix/sbin/kea-dhcp4 ]] || brew install kea

  while IFS=$'\t' read -r dev name; do
    info=$(ifconfig $dev 2>/dev/null) || continue

    [[ $info == *'status: active'* && $info == *baseT* ]] || continue
    [[ -z "$1" || $dev == "$1" ]] || continue

    (( ${devices[(Ie)$dev]} )) && continue

    devices+=("$dev")
    services+=("$name")
  done <<< "$(networksetup -listnetworkserviceorder | awk -F'Device: ' '/^\(\*\)/{skip=1} /^\([0-9]+\)/{name=$0; sub(/^\([0-9]+\) /, "", name); skip=0} /Device: en/{if (!skip) {dev=$2; sub(/\)$/, "", dev); print dev "\t" name}}')"

  (( ${#devices} )) || {
    printf "\033[31mNo active wired interface found, plug in the camera cable first\033[0m\n"; return 1;
  }

  (( ${#devices} == 1 )) || {
    printf "\033[31mMultiple active wired interfaces, rerun with the camera one:\033[0m\n"

    for i in {1..${#devices}}; do
      printf "  __game_on_init_axis_connection %s  (%s)\n" "$devices[$i]" "$services[$i]"
    done

    return 1
  }

  iface=$devices[1]
  port=$services[1]

  printf "%s\n\n" "$separator"
  printf "Using interface: \033[31m%s (%s)\033[0m\n" "$port" "$iface"
  printf "Camera will be reachable at \033[31mhttp://%s\033[0m - watch for DHCPDISCOVER > DHCPACK\n" "$camera_ip"
  printf "Stop with \033[31mCtrl-C\033[0m, DHCP on %s is restored automatically\n\n" "$port"
  printf "%s\n\n" "$separator"

  {
    sudo networksetup -setmanual "$port" $host_ip 255.255.255.0

    i=0

    until ifconfig $iface | grep -q "inet $host_ip "; do
      (( ++i > 10 )) && {
        printf "\033[31mFailed to set %s on %s, enable the service in System Settings > Network first\033[0m\n" "$host_ip" "$port"; return 1;
      }

      sleep 0.5
    done

    ping -c 1 -t 1 $camera_ip &> /dev/null && {
      printf "\033[31mWarning: something already answers at %s, the camera will not get it\033[0m\n\n" "$camera_ip"
    }

    mkdir -p ${config:h}

    cat > $config <<EOF
{"Dhcp4":{"interfaces-config":{"interfaces":["$iface"]},"lease-database":{"type":"memfile","persist":true,"name":"dhcp.leases"},"valid-lifetime":4000,"renew-timer":1000,"rebind-timer":2000,"subnet4":[{"id":1,"subnet":"192.168.1.0/24","interface":"$iface","pools":[{"pool":"$camera_ip - $camera_ip"}]}],"loggers":[{"name":"kea-dhcp4","output-options":[{"output":"stdout"}],"severity":"INFO"}]}}
EOF

    sudo pkill -x kea-dhcp4 2>/dev/null && sleep 1

    sudo rm -f /tmp/dhcp.leases*(N) $brew_prefix/var/run/kea/kea-dhcp4*.pid(N)
    sudo env KEA_DHCP_DATA_DIR=/tmp $brew_prefix/sbin/kea-dhcp4 -c $config 2>&1 | awk '/DHCPDISCOVER|DHCPACK/ { $0 = "\033[32m" $0 "\033[0m" } { print; fflush() }'
  } always {
    sudo networksetup -setdhcp "$port"
    printf "\n\n%s\n\nDHCP restored on %s\n\n%s\n" "$separator" "$port" "$separator"
  }
}

trap : INT
__game_on_init_axis_connection "$@"
