#!/bin/sh
#
# setup the network
#
# runlevels: geexbox, debug, configure

echo "### Setting up network ###"

WPA_CONFIG=/etc/wpa_supplicant.conf

wpa_config_gen() {
cat > $WPA_CONFIG <<EOF
ap_scan=$3
network={
ssid="$1"
psk="$2"
scan_ssid=$4
proto=WPA
key_mgmt=WPA-PSK
}
EOF
}

wifi_connection_up() {
  iwconfig $1 2>&1 | grep -q -e "Cell:" -e "Access Point: ..:"
}

# bring lo up, whether we have network card or not
ifconfig lo 127.0.0.1 up

# create /etc/hosts file, useful for gethostbyname(localhost)
echo -e "127.0.0.1\tlocalhost" > /etc/hosts

# get options
test -f /etc/network || exit 1
. /etc/network
test -z "$HOST" && HOST=0.0.0.0
if [ -x /usr/bin/iwconfig ]; then
  for i in `iwconfig 2>&1 | grep '^[^\ ]' | grep -v '^lo' | cut -f1 -d' '`; do
    ifconfig $i up >/dev/null 2>&1
  done
  WIFI=`iwconfig 2>&1 | grep '^[^\ ]' | grep ESSID | cut -f1 -d' ' | head -n 1`
  ETH=`iwconfig 2>&1 | grep '^[^\ ]'  | grep -v '^lo' | grep "no wireless extensions" | cut -f1 -d' ' | head -n 1`
else
  ETH=eth0
fi

(
  # select device
  if test $PHY_TYPE = wifi -o $PHY_TYPE = auto; then
    DEV=$WIFI
    if test -n "$DEV"; then
      test -n "$WIFI_MODE" && iwconfig "$DEV" mode "$WIFI_MODE"
      test -n "$WIFI_CHANNEL" && iwconfig "$DEV" channel "$WIFI_CHANNEL"
      test -n "$WIFI_ESSID" && iwconfig "$DEV" essid "$WIFI_ESSID"
      if test $WIFI_ENC = WEP; then
        test -n "$WIFI_KEY" && iwconfig "$DEV" key "$WIFI_KEY"
      elif test $WIFI_ENC = WPA; then
        if test -x /usr/bin/wpa_supplicant; then
          if test ! -f $WPA_CONFIG; then
            wpa_config_gen "$WIFI_ESSID" "$WIFI_KEY" $WPA_AP_SCAN $WPA_SCAN_SSID
            [ $WPA_CIPHER != "none" ] && sed -i "s/}/pairwise=$WPA_CIPHER\ngroup=$WPA_CIPHER\n}/" $WPA_CONFIG
          fi
          wpa_supplicant -B -i $DEV -c $WPA_CONFIG -D $WPA_DRV
        fi
        if test -x /usr/bin/iwpriv; then
          # WPA may not be up, try using iwpriv as well
          iwpriv $DEV set AuthMode=WPAPSK >/dev/null 2>&1
          iwpriv $DEV set EncrypType=TKIP >/dev/null 2>&1
          iwpriv $DEV set WPAPSK="$WIFI_KEY" >/dev/null 2>&1
        fi
      fi
    fi
    if [ $PHY_TYPE = auto ]; then
      COUNT=0
      while [ $COUNT -lt 10 ]; do
        wifi_connection_up $DEV && break
        sleep 1
        COUNT=$((COUNT + 1))
      done
      wifi_connection_up $DEV || unset DEV
    fi
  fi
  if test $PHY_TYPE = ethernet -o $PHY_TYPE = auto -a -z "$DEV"; then
    DEV=$ETH
  fi
  test -n "$DEV" || exit 1

  [ -n "$SUBNET" ] && NETMASK="netmask $SUBNET"

  # bring interface up
  if ifconfig $DEV $HOST $NETMASK >/dev/null 2>&1; then
    if test $HOST = 0.0.0.0; then
      [ $DHCP_TIMEOUT = "0" ] && DHCP_WAIT="-n" || DHCP_WAIT="-T $DHCP_TIMEOUT"
      udhcpc -H geexbox $DHCP_WAIT -i $DEV >/dev/null 2>&1 && NET=yes
      test "$NET" != yes && ifconfig $DEV 192.168.0.54 netmask 255.255.255.0 && NET=yes
    else
      metric=0
      for i in $GATEWAY; do
        route add default gw $i dev $DEV metric $((metric++))
      done
      NET=yes
    fi
    echo "" > /var/ifup
  fi

  if test "$UPNP" = "yes"; then
    # add UPnP multicast route
    route add -net 239.0.0.0 netmask 255.0.0.0 $DEV
  fi

  # adding DNS server
  if [ "$NET" = yes ]; then
    for i in $DNS_SERVER; do
      echo "nameserver $i" >> /etc/resolv.conf
    done
  fi
)&

exit 0
