#!/bin/sh

. /etc/network

profile=/var/lib/connman/default.profile

mkdir -p /var/lib/connman

hostname="`cat /etc/hostname`"
echo "127.0.0.1 localhost $hostname" > /etc/hosts 

[ -z "$IFACE" ] && IFACE=eth0
[ -f /sys/class/net/$IFACE/address ] && \
  IFACE=`cat /sys/class/net/$IFACE/address`

if [ "$NETWORK" = "WLAN" ]; then
  SSID_HEX=`echo -n "$SSID" | od -tx1 | cut -c8-| tr -d ' \n'`
  if [ "$SECURITY" = "WEP" -a -n "$PASSPHRASE" ]; then
    MODE="managed_wep"
  elif [ "$SECURITY" = "PSK" -a -n "$PASSPHRASE" ]; then
    MODE="managed_psk"
  else
    MODE="managed_none"
  fi
  IFACE=`echo $IFACE | sed 's/://g'`
  if [ "$HIDDEN" = "true" ]; then
    echo "[wifi_${IFACE}_hidden_${MODE}]" > $profile
  else
    echo "[wifi_${IFACE}_${SSID_HEX}_${MODE}]" > $profile
  fi
   echo "Name=$SSID" >> $profile
   echo "SSID=$SSID_HEX" >> $profile
   [ -n "$PASSPHRASE" ] && echo "Passphrase=$PASSPHRASE" >> $profile
   echo "Favorite=true" >> $profile
   echo "AutoConnect=true" >> $profile
else 
  echo "[ethernet_`echo $IFACE | sed 's/://g'`_cable]" > $profile
fi

if [ -z "$ADDRESS" ]; then
  echo "IPv4.method=dhcp" >> $profile
  exit 0
fi

ip=`echo $ADDRESS | cut -f1 -d/`
prefix=`echo $ADDRESS | cut -f2 -d/`

echo "IPv4.method=manual" >> $profile
echo "IPv4.local_address=$ip" >> $profile
[ -n "$prefix" ] && echo "IPv4.netmask_prefixlen=$prefix" >> $profile
[ -n "$GATEWAY" ] && echo "IPv4.gateway=$GATEWAY" >> $profile
[ -n "$DNS_SERVER" ] && echo "Nameservers=$DNS_SERVER;" >> $profile

exit 0
