modules=( "dhcpcd" )
config_eth0=( "dhcp" )
dhcpcd_eth0="-t 10"

PIDFILE='/var/run/wpa_supplicant.pid'
WPA_SUPPLICANT_IFACE='eth0'

preup()
{
  if [[ "${IFACE}" == "$WPA_SUPPLICANT_IFACE" ]]; then
    start_wpa_supplicant
  fi
}

postdown()
{
  if [[ "${IFACE}" == "$WPA_SUPPLICANT_IFACE" ]]; then
    stop_wpa_supplicant
  fi
}

start_wpa_supplicant()
{
  # For some reason, start-stop-daemon in stop_wpa_supplicant doesn't always work.
  # Even if I put a killall command there, wpa_supplicant is still running sometimes when I call start_wpa_supplicant.
  # Perhaps stop_wpa_supplicant is never run for some reason?
  # Regardless of the reason, we need to kill wpa_supplicant here, or else it won't start because it's already running.
  ebegin "Stopping wpa_supplicant (if it is running)"
  killall -q -w wpa_supplicant
  ! pgrep -x wpa_supplicant
  eend $?

  ebegin "Starting wpa_supplicant"
  start-stop-daemon --start --background --quiet --make-pidfile --pidfile "$PIDFILE" --exec \
    /sbin/wpa_supplicant -- "-i${IFACE}" -c/etc/wpa_supplicant/wpa_supplicant.conf -Dwired
  eend $?
}

stop_wpa_supplicant()
{
  ebegin "Trying to stop wpa_supplicant"
  start-stop-daemon --stop --pidfile "$PIDFILE" --oknodo
  eend $?
}
