#!/bin/sh
# FILE:/etc/init.d/S01_network
# Copyright (C) 2012 GuoWenxue <guowenxue@gmail.com QQ:281143292>
# This file used to configure the network from the configure files 

if [ -z "$network_cfg_dir" ] ; then
  export network_cfg_dir=/apps/etc/network
fi
cd $network_cfg_dir

interfaces=$( ls ifcfg* | \
		sed -e '/ifcfg-[A-Za-z0-9\._-]\+$/ { s/^ifcfg-//g;s/[0-9]/ &/}' | \
		sort -k 1,1 -k 2n | \
		sed 's/ //')

ifconfig lo 127.0.0.1
# No interface configure, then use the default IP address
if [ -z "$interfaces" ] ; then
   ifconfig eth0 192.168.1.199 up
   exit
fi   

# Set up all the interface
for i in $interfaces; do
  unset DEVICE TYPE
  eval $(fgrep "DEVICE=" ifcfg-$i)
  eval $(fgrep "TYPE=" ifcfg-$i)
  
  if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi
  
  if ! egrep -L "^ONBOOT=['\"]?[Nn][Oo]['\"]?" ifcfg-$i > /dev/null ; then
  	if [ "$TYPE" = "Ethernet" ] ; then
  		echo "Bring up $TYPE interface $i"
  		/usr/sbin/ifup-eth $i
  	elif [ "$TYPE" = "PPP" ] ; then
  		echo "Bring up $TYPE interface $i"
  	elif [ "$TYPE" = "WLAN" ] ; then
        # Just bring up the wiFi module here. Configure it in the followed shell scripts
        if [ -d /sys/class/net/$DEVICE/ ] ; then
           ifconfig $DEVICE up
        fi
  	else 
  		echo "Bring up $TYPE interface $i"
  	fi
  fi
done 
