#!/bin/bash

set -euo pipefail

for interface in eth0 eth1 eth2 eth3
do
  logger -t "robotnik watchdog" "Link check $interface"
  # If link is down, log debug info and reset link.
  if ip link show "$interface" | grep -q 'NO-CARRIER'
  then
    # Log debug info to syslog
    (
      echo "Robotnik link $interface is down. Attempting to reset"
      echo "ethtool $interface"
      ethtool "$interface"
      echo "ethtool -i $interface"
      ethtool -i "$interface"
      echo "ethtool -S $interface"
      ethtool -S "$interface"
    ) | logger -t "robotnik watchdog"
    # Try to reset interface
    logger -t "robotnik watchdog" "Stopping $interface"
    ifdown "$interface"
    logger -t "robotnik watchdog" "Restarting $interface"
    ifup "$interface"
  fi
done
