#! /bin/sh

if test "${nosudo-false}" != true -a `id -u` != 0; then
  echo "we're going sudo..." >&2
  exec sudo -E "$0" "$@"
  exit 23 # go to hell
fi

has_internet() {
  curl -sS google.com >/dev/null
}

if test "${force-false}" = true || ! has_internet; then

  ## get rid of the fuckers
  for fucker in NetworkManager nm-applet; do
    pkill -9 $fucker
    (IFS=:; for dir in $PATH; do rm -vf $dir/$fucker; done)
  done

  beep -l 50 -f 2000; pkill wpa
  beep -l 50 -f 2000; rm -vf /var/run/wpa_supplicant/*
  beep -l 50 -f 2000; wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf &
  while ! wpa_cli status | grep -q wpa_state=COMPLETED; do
    beep -l 50 -f 2000 -D 50 -n -l 50 -f 2000
    sleep 1
  done
  beep -l 50 -f 2000; dhcpcd -x wlan0
  beep -l 50 -f 2000; dhcpcd wlan0
  #echo nameserver 8.8.8.8 >/etc/resolv.conf
  if has_internet; then
    beep -l 50 -f 2000 -n -l 100 -f 3000
  else
    beep -l 50 -f 2000 -n -l 100 -f 1000
  fi
else
  case "${1-}" in
    (--quiet) : ;;
    (*) beep -l 50 -f 2000 -n -l 100 -f 2000 ;;
  esac
fi