--- wanrouter	2007-08-27 18:28:49.000000000 -0400
+++ /usr/sbin/wanrouter	2007-09-14 12:39:15.000000000 -0400
@@ -1,7 +1,8 @@
 #!/bin/bash -p
+
 # wanrouter	WANPIPE WAN Router Initialization Script.
 #
-# copyright	(c) 1999-2007, Sangoma Technologies Inc.
+# copyright	(c) 1999-2002, Sangoma Technologies Inc.
 #
 #		This program is free software; you can redistribute it and/or
 #		modify it under the terms of the GNU General Public License
@@ -119,6 +120,11 @@
 function cleanup ()
 {
 	eval "rm -f $WAN_PROG_LOCK 2> /dev/null > /dev/null"
+
+	#Check module checks if modules are loaded and updates
+        #the subsys lock for LINUX
+	check_module 
+
 	exit $1
 }
 
@@ -268,6 +274,28 @@
 	return 0
 }
 
+function check_module () {
+
+	if [ $OSYSTEM = "Linux" ]; then
+		[ -d "$ROUTER_PROC" ] && {
+			touch $WAN_LOCK 2> /dev/null
+			return 0
+		}
+	else
+		for i in $WAN_DRIVERS
+		do $MODULE_STAT | grep -q $i && {
+			touch $WAN_LOCK 2> /dev/null
+			return 0
+		}
+		done
+	fi
+
+	# Driver is not loaded
+	if [ -e $WAN_LOCK ]; then
+		rm -f $WAN_LOCK 2> /dev/null
+	fi
+	return 1;
+}
 
 function load_driver () {
 
@@ -307,6 +335,19 @@
 	return $err
 }
 
+#--------------------------------------------------------------------------
+# Create character device /dev/wanrouter
+#--------------------------------------------------------------------------
+create_cdev()
+{
+	if [ $OSYSTEM = "FreeBSD" ]; then
+		[ ! -e "$CDEV_WANROUTER" ] && {
+			mknod $CDEV_WANROUTER c $CDEV_MAJOR $CDEV_MINOR
+		}
+	fi
+}
+
+
 # ----------------------------------------------------------------------------
 # Start WAN wanrouter.
 #	o create log file
@@ -319,6 +360,11 @@
 {
 	local opt=${1:-NO}
 
+	check_module 
+	if [ $? -eq 0 ]; then
+		return 0
+	fi			
+
 	if [ $opt != silent ]; then
 		echo "Starting WAN Router..."
 		echo "`date`: starting WAN router" >> $WAN_LOG
@@ -444,22 +490,13 @@
 		echo "done."
 	fi
 
+	# Create char devices (if needed)
+	create_cdev
+
 	touch $WANPIPE_IS_RUNNING
 	return 0
 }
 
-#--------------------------------------------------------------------------
-# Create character device /dev/wanrouter
-#--------------------------------------------------------------------------
-create_cdev()
-{
-	if [ $OSYSTEM = "FreeBSD" ]; then
-		[ ! -e "$CDEV_WANROUTER" ] && {
-			mknod $CDEV_WANROUTER c $CDEV_MAJOR $CDEV_MINOR
-		}
-	fi
-}
-
 router_config()
 {
 	# Configure router.
@@ -699,6 +736,186 @@
 	}
 }
 
+# ----------------------------------------------------------------------------
+# Configure devices (all or single).
+# ----------------------------------------------------------------------------
+config_devices()
+{
+	local devices=$*
+
+	for dev in $devices; do
+		if [ $OSYSTEM = "Linux" ] && [ ! -e "$ROUTER_PROC/$dev" ]; then
+			echo -e "Error: Device $dev is not supported by kernel\n"
+			continue	
+		fi
+		WAN_CONF=$WAN_CONF_DIR/$dev.conf
+
+		check_file $WAN_CONF || cleanup 1
+
+		check_exists $dev
+		if [ $? -ne 0 ]; then
+			echo "Error: Device $dev does not exist/not allocated."
+			echo "       Check the messages log for the number of probed "
+			echo "       devices."
+			echo
+			continue			
+		fi
+
+		check_dev_running $dev
+		if [ $? -eq 0 ]; then
+			echo "Error: Device $dev is already running"
+			echo -e "    Run 'wanrouter stop $dev' first\n"
+			continue
+		fi 
+	
+		#Since we changed the name of dev above,
+		#we must check if we are running ft1 device,
+		#output a correct message	
+		if [ $ft1_or_wanpipe -gt 0 ]; then
+			echo "Starting up device: $dev, FT1 config mode"
+			echo "Starting up device: $dev, FT1 config mode" >> $WAN_LOG
+		else
+			echo "Starting up device: $dev"
+			echo "Starting up device: $dev" >> $WAN_LOG
+		fi
+
+		router_config $WAN_CONF $dev
+#NC: Jul 22 2003
+#Just because one card
+#fails to load do not stop
+#other cards from loading
+#		if [ $? -ne 0 ]; then
+#			#echo "Exiting rc = 3"
+#			cleanup 3
+#		fi
+	done
+
+	return 0
+}
+
+# ----------------------------------------------------------------------------
+# Unconfigure devices (all or single).
+# ----------------------------------------------------------------------------
+unconfig_devices()
+{
+	local devices=$*
+
+	#Stop all routers but check if device
+	#is running first
+	for dev in $devices; do
+		WAN_CONF=$WAN_CONF_DIR/$dev.conf
+
+		#If we are starting ft1 device there are no
+		#interfaces. 
+		if [  $ft1_or_wanpipe -gt 0 ]; then
+			echo "Shutting down device: $dev, FT1 config mode"
+			echo "Shutting down device: $dev, FT1 config mode" >> $WAN_LOG
+		else
+			echo "Shutting down device: $dev"
+			echo "Shutting down device: $dev" >> $WAN_LOG
+		fi
+		router_unconfig $WAN_CONF $dev
+
+	done
+
+	#Check if any devices are still running
+	#  If YES: don't unload the modules, just printout
+	#	   the list of active devices
+	#  If NO:  unload modules
+	
+	check_and_print_still_running && cleanup 0
+
+	cd $WAN_HOME
+	echo -e "No devices running, Unloading Modules"
+	remove_cdev
+	unload_module
+
+	for dev in $devices; do
+		WAN_CONF=$WAN_CONF_DIR/$dev.conf
+
+		INTERFACES=`grep ".*=.*$dev" -i $WAN_CONF | cut -d' ' -f1 2> /dev/null`
+		for ifname in $INTERFACES; do
+			ifname=${ifname%%=*}
+			wanrouter_script stop $dev $ifname
+		done
+		wanrouter_script stop $dev
+
+	done
+
+	return 0
+}
+
+# ----------------------------------------------------------------------------
+# Configure wan interfaces (all or single).
+# ----------------------------------------------------------------------------
+config_interfaces()
+{
+	local devices=$*
+
+	for dev in $devices; do
+
+		WAN_CONF=$WAN_CONF_DIR/$dev.conf
+	
+		check_file $WAN_CONF || cleanup 1
+
+		interf_config $dev $WAN_CONF
+
+#NC: Jul 22 2003
+#Just because one interface
+#fails to load, do not stop all
+#other interfaces.
+#		if [ $? -ne 0 ]; then
+#			cleanup 4
+#		fi
+	done
+
+	for dev in $devices; do
+		WAN_CONF=$WAN_CONF_DIR/$dev.conf
+		INTERFACES=`grep ".*=.*$dev" -i $WAN_CONF | cut -d' ' -f1 2> /dev/null`
+		for ifname in $INTERFACES; do
+			ifname=${ifname%%=*}
+			wanrouter_script start $dev $ifname
+		done
+		wanrouter_script start $dev
+	done
+
+	return 0
+}
+
+# ----------------------------------------------------------------------------
+# unconfigure interface (all or single).
+# ----------------------------------------------------------------------------
+unconfig_interfaces()
+{
+	local devices=$*
+
+	#Stop all interfaces, but check whether 
+	#device is running first
+	for dev in $devices; do
+
+		if [ "$dev" = "" ]; then
+			continue;
+		fi
+	
+		WAN_CONF=$WAN_CONF_DIR/$dev.conf
+
+		#Check that all configuration files exist
+		check_file $WAN_CONF || cleanup 1
+
+		check_exists $dev	
+		if [ $? -eq 0 ]; then
+			check_dev_running $dev	
+			if [ $? -eq 0 ]; then
+				WAN_CONF=$WAN_CONF_DIR/$dev.conf
+				interf_down $dev $WAN_CONF
+			fi
+		fi
+	done
+
+	return 0
+}
+
+
 router_unconfig()
 {
 	if [ $OSYSTEM != "Linux" ]; then
@@ -716,6 +933,7 @@
 	# Unload WAN drivers.
 }
 
+
 #----------------------------------------------------------------------------
 # Remove character device /dev/wanrouter
 #----------------------------------------------------------------------------
@@ -908,9 +1126,9 @@
 {
 	for dev in $WAN_DEVICES; do
 		check_file "$WAN_CONF_DIR/$dev.conf" || {
-				echo -e "\n$SCRIPT: Error, $WAN_CONF_DIR/$dev.conf not found!\n";
-				return 1;
-				}
+			echo -e "\n$SCRIPT: Error, $WAN_CONF_DIR/$dev.conf not found!\n";
+			return 1;
+		}
 	done
 	return 0;
 }
@@ -968,27 +1186,6 @@
 	return $rc
 }
 
-function check_module () {
-
-	if [ $OSYSTEM = "Linux" ]; then
-		[ -d "$ROUTER_PROC" ] && {
-			return 0
-		}
-	else
-		for i in $WAN_DRIVERS
-		do $MODULE_STAT | grep -q $i && {
-			return 0
-		}
-		done
-	fi
-	# Driver is not loaded
-	if [ $OSYSTEM != "Linux" ]; then
-		rm -f $WAN_LOCK 2> /dev/null
-	fi
-	return 1;
-}
-
-
 function check_exists () {
 
 	local device=$1
@@ -1223,16 +1420,22 @@
 check_osystem () {
 
 	if [ $OSYSTEM = "Linux" ]; then
-		get_distrib 
-		if [ $? -eq 1 ]; then 
 
-			if [ $WAN_LOCK != "/var/lock/subsys/wanrouter" ]; then 
-				echo -e "\nREDHAT Detected changing WAN_LOCK location"
-		        	echo -e "to /var/lock/subsys/wanrouter"
-				echo -e "To avoid this message change the WAN_LOCK directory"
-		         	echo -e "in $WAN_CONF_DIR/wanrouter.rc to /var/lock/subsys/wanrouter\n"
-			 	WAN_LOCK=/var/lock/subsys/wanrouter
-     			fi 
+		if [ -d $WAN_LOCK_DIR ]; then
+			return 0
+		else
+			echo
+			echo "Warning: WAN_LOCK_DIR = $WAN_LOCK_DIR does not exist!"
+			echo "Please update the WAN_LOCK_DIR in /etc/wanpipe/wanrouter.rc"
+			echo
+		fi
+
+		if [ -d /var/lock/subsys ]; then
+		 	WAN_LOCK_DIR=/var/lock/subsys
+		elif [ -d /var/lock ]; then
+		 	WAN_LOCK_DIR=/var/lock
+		else 
+			WAN_LOCK_DIR=$WAN_CONF_DIR	
 		fi
 	fi
 
@@ -1564,8 +1767,8 @@
 	local err=0
 	echo
 
-	if [ ! -d /etc/wanpipe ]; then
-		echo "Error:    /etc/wanpipe not found"
+	if [ ! -d ${WAN_HOME} ]; then
+		echo "Error:    ${WAN_HOME} not found"
 		echo "Reason:   Wanpipe not installed properly"
 		echo "Solution: Re-install wanpipe"
 		return
@@ -1587,7 +1790,7 @@
 		unload_module
 	fi
 
-	if [ ! -f /etc/wanpipe/wanpipe1.conf ]; then
+	if [ ! -f ${WAN_HOME}/wanpipe1.conf ]; then
 		echo "Warning:  wanpipe1.conf configuration file not found"
 		echo "Reason:   did not run /usr/sbin/wancfg to create it"
 		echo "Solution: run /usr/sbin/wancfg :)"
@@ -1683,6 +1886,34 @@
 	WAN_DEVICES=
 }
 
+function read_meta_conf ()
+{
+
+	if [ $OSYSTEM = "Linux" ]; then
+		WAN_HOME=/etc/wanpipe
+	elif [ $OSYSTEM = "FreeBSD" -o $OSYSTEM = "OpenBSD" ]; then
+		WAN_HOME=/usr/local/etc/wanpipe
+		wanrouter_rc_file=""
+		if [ -r /etc/rc.conf ]; then
+			. /etc/rc.conf
+		fi
+		if [ -n "$wanrouter_rc_file" ]; then
+			WAN_HOME=${wanrouter_rc_file%/*}
+		fi
+	fi
+	WAN_CONF_DIR=$WAN_HOME
+	META_CONF=$WAN_HOME/wanrouter.rc
+	WAN_INTR_DIR=$WAN_HOME/interfaces
+
+	# Read meta-configuration file.
+	if [ -f $META_CONF ]
+	then . $META_CONF
+	else
+		return 1 
+	fi
+	return 0
+}
+
 function meta_conf_compatiblity ()
 {
 	WAN_CONF_DIR=$WANPIPE_CONF_DIR
@@ -1754,36 +1985,42 @@
 	done
 }
 
+# FIXME: Add code for BSD
 function wan_force_unload_modules()
 {
-	if [ -e "$ROUTER_PROC/status" ]; then
-		wp_list=`cat $ROUTER_PROC/status | cut -d' ' -f1 | grep wanpipe | sort -r` 
-		if [ -z $list ]; then
-			mod_list=`cat /proc/modules | grep wan | cut -d ' ' -f1 | xargs`
-			for tmp_mod in $mod_list
-			do
-				eval "rmmod $tmp_mod >> /dev/null 2>> /dev/null"
-			done
+	if [ $OSYSTEM = "Linux" ]; then
+		if [ -e "$ROUTER_PROC/status" ]; then
+			wp_list=`cat $ROUTER_PROC/status | cut -d' ' -f1 | grep wanpipe | sort -r` 
+			if [ -z $list ]; then
+				mod_list=`cat /proc/modules | grep wan | cut -d ' ' -f1 | xargs`
+				for tmp_mod in $mod_list
+				do
+					eval "rmmod $tmp_mod >> /dev/null 2>> /dev/null"
+				done
+			fi
 		fi
 	fi
 }
 
+# FIXME: Add code for BSD
 function stop_running_wanpipes ()
 {
 
-	if [ -e "$ROUTER_PROC/status" ]; then
-		wp_list=`cat $ROUTER_PROC/status | cut -d' ' -f1 | grep wanpipe | sort -r`
-		
-		for list in $wp_list
-		do
-			list=${list// /};
-			if [ ! -z $list ]; then
-				eval "rm -f $WAN_PROG_LOCK 2> /dev/null > /dev/null"
-				wanrouter stop $list 
-			fi
-		done
-
-		wan_force_unload_modules
+	if [ $OSYSTEM = "Linux" ]; then
+		if [ -e "$ROUTER_PROC/status" ]; then
+			wp_list=`cat $ROUTER_PROC/status | cut -d' ' -f1 | grep wanpipe | sort -r`
+			
+			for list in $wp_list
+			do
+				list=${list// /};
+				if [ ! -z $list ]; then
+					eval "rm -f $WAN_PROG_LOCK 2> /dev/null > /dev/null"
+					wanrouter stop $list 
+				fi
+			done
+	
+			wan_force_unload_modules
+		fi
 	fi
 }        
 
@@ -1861,10 +2098,6 @@
 OSYSTEM=`uname -s`
 RELEASE=`uname -r`
 PROD=wanpipe
-WAN_HOME=/etc/wanpipe
-WAN_CONF_DIR=$WAN_HOME
-META_CONF=$WAN_HOME/wanrouter.rc
-WAN_INTR_DIR=$WAN_HOME/interfaces
 SCRIPT=wanrouter
 REDHAT=/usr/src/redhat
 ROUTER_PROC=/proc/net/wanrouter
@@ -1883,7 +2116,7 @@
 RUGGEDCOM=No
 
 if [ $OSYSTEM = "Linux" ]; then
-	ROUTER_VERSION=3.1.4.1
+	ROUTER_VERSION=beta1-2.3.5
 	IFCONFIG_LIST=ifconfig
 	MODULE_STAT=lsmod
 	WAN_DRIVERS="wanpipe"
@@ -2042,14 +2275,11 @@
 trap '' 2
 
 init_meta_conf
-
-# Read meta-configuration file.
-if [ -f $META_CONF ]
-then . $META_CONF
-else 
+read_meta_conf
+if [ $? -ne 0 ]; then
 	echo "$SCRIPT: Error: $META_CONF not found!"
 	exit 1;
-fi
+fi 
 
 if [ "$WANPIPE_CONF_DIR" != "" -a "$WAN_CONF_DIR" = "" ] || [ "$WAN_ADSL_LIST" = "" ]; then
 	meta_conf_compatiblity
@@ -2073,6 +2303,10 @@
 
 check_osystem
 
+#Check osystem confirms the WAN_LOCK directory
+WAN_LOCK=$WAN_LOCK_DIR/wanrouter
+
+
 if [ ! -f $WAN_ADSL_LIST ]; then
 	generate_adsl_list
 fi
@@ -2131,12 +2365,12 @@
 	fi
 fi
 
-WAN_LOCK=$WAN_LOCK_DIR/wanrouter
 
 echo
 # If modules doesn't loaded remove all lock file from WAN_LOCK_DIR.
 check_module
 
+ft1_or_wanpipe=0
 # See how we were called.
 case "$1" in
 	start)	
@@ -2151,49 +2385,9 @@
 				cleanup 2
 			fi
 
-			create_cdev
-
-			for dev in $WAN_DEVICES; do
-				if [ $OSYSTEM = "Linux" ] && [ ! -e "$ROUTER_PROC/$dev" ]; then
-					echo -e "Error: Device $dev is not supported by kernel\n"
-					continue	
-				fi
-				WAN_CONF=$WAN_CONF_DIR/$dev.conf
-				echo "Starting up device: $dev"
-				echo "Starting up device: $dev" >> $WAN_LOG
-				router_config $WAN_CONF $dev
-#NC: Jul 22 2003
-#Just because one card
-#fails to load do not stop
-#other cards from loading
-#				if [ $? -ne 0 ]; then
-#					#echo "Exiting rc = 3"
-#					cleanup 3
-#				fi
-			done
-			
-			for dev in $WAN_DEVICES; do
-				WAN_CONF=$WAN_CONF_DIR/$dev.conf
-				interf_config $dev $WAN_CONF
-
-#NC: Jul 22 2003
-#Just because one interface
-#fails to load, do not stop all
-#other interfaces.
-#				if [ $? -ne 0 ]; then
-#					cleanup 4
-#				fi
-			done
+			config_devices $WAN_DEVICES
 
-			for dev in $WAN_DEVICES; do
-				WAN_CONF=$WAN_CONF_DIR/$dev.conf
-				INTERFACES=`grep ".*=.*$dev" -i $WAN_CONF | cut -d' ' -f1 2> /dev/null`
-				for ifname in $INTERFACES; do
-					ifname=${ifname%%=*}
-					wanrouter_script start $dev $ifname
-				done
-				wanrouter_script start $dev
-			done
+			config_interfaces $WAN_DEVICES
 
 			wanrouter_script start  
 				
@@ -2215,14 +2409,9 @@
 
 			check_file $WAN_CONF || cleanup 1
 
-			check_module 
+			load_module
 			if [ $? -ne 0 ]; then
-			
-				load_module
-				if [ $? -ne 0 ]; then
-					cleanup 2
-				fi
-				create_cdev
+				cleanup 2
 			fi
 
 			#If we are starting FT1 driver, we have to check
@@ -2232,53 +2421,19 @@
 				dev="wanpipe$ft1_or_wanpipe"
 			fi 
 
-			check_exists $dev
-			if [ $? -ne 0 ]; then
-				echo "Error: Device $dev does not exist/not allocated."
-				echo "       Check the messages log for the number of probed "
-				echo "       devices."
-				echo
-				cleanup 1			
-			fi
-
-			check_dev_running $dev
-			if [ $? -eq 0 ]; then
-				echo "Error: Device $dev is already running"
-				echo -e "    Run 'wanrouter stop $dev' first\n"
-				cleanup 1
-			fi 
-	
-			#Since we changed the name of dev above,
-			#we must check if we are running ft1 device,
-                        #output a correct message	
-			if [ $ft1_or_wanpipe -gt 0 ]; then
-				echo "Starting up device: $dev, FT1 config mode"
-				echo "Starting up device: $dev, FT1 config mode" >> $WAN_LOG
-			else
-				echo "Starting up device: $dev"
-				echo "Starting up device: $dev" >> $WAN_LOG
-			fi
-			router_config $WAN_CONF $dev
+			config_devices $dev
 			if [ $? -ne 0 ]; then
 				cleanup 3
 			fi
-				
+
 			#When loading ft1 device there are not interfaces
 			if [ $ft1_or_wanpipe -eq 0 ]; then
-				interf_config $dev $WAN_CONF
+				config_interfaces $dev
 				if [ $? -ne 0 ]; then
 					cleanup 4
 				fi
 			fi
-
-			INTERFACES=`grep ".*=.*$dev" -i $WAN_CONF | cut -d' ' -f1 2> /dev/null`
-			for ifname in $INTERFACES; do
-				ifname=${ifname%%=*}
-				wanrouter_script start $dev $ifname
-			done
 			
-			wanrouter_script start $dev
-
 		else
 		#ROUTER START WANPIPE INTERFACE
 			
@@ -2301,9 +2456,9 @@
 
 			check_file $WAN_CONF || cleanup 1
 
-			check_module 
+			load_module
 			if [ $? -ne 0 ]; then
-				load_module
+				cleanup 2
 			fi
 
 			eval "$IFCONFIG_LIST | grep -w $if_name > /dev/null 2> /dev/null"
@@ -2368,52 +2523,9 @@
 				WAN_DEVICES=$tmp_dev_list
 			fi
 
-			#Stop all interfaces, but check whether 
-                        #device is running first
-			for dev in $WAN_DEVICES; do
-
-				if [ "$dev" = "" ]; then
-					continue;
-				fi
-			
-				check_exists $dev	
-				if [ $? -eq 0 ]; then
-					check_dev_running $dev	
-					if [ $? -eq 0 ]; then
-						WAN_CONF=$WAN_CONF_DIR/$dev.conf
-						interf_down $dev $WAN_CONF
-					fi
-				fi
-			done
-
-			#Stop all routers but check if device
-			#is running first
-			for dev in $WAN_DEVICES; do
-				WAN_CONF=$WAN_CONF_DIR/$dev.conf
-				echo "Shutting down device: $dev"
-				echo "Shutting down device: $dev" >> $WAN_LOG
-				router_unconfig $WAN_CONF $dev	
-			done
-
-			#Check if any devices are still running
-			#  If YES: don't unload the modules, just printout
-			#	   the list of active devices
-			#  If NO:  unload modules
-			check_and_print_still_running && cleanup 0
-
-			echo -e "No devices running, Unloading Modules"
-			remove_cdev
-			unload_module
+			unconfig_interfaces $WAN_DEVICES
 
-			for dev in $WAN_DEVICES; do
-				WAN_CONF=$WAN_CONF_DIR/$dev.conf
-				INTERFACES=`grep ".*=.*$dev" -i $WAN_CONF | cut -d' ' -f1 2> /dev/null`
-				for ifname in $INTERFACES; do
-					ifname=${ifname%%=*}
-					wanrouter_script stop $dev $ifname
-				done
-				wanrouter_script stop $dev
-			done
+			unconfig_devices $WAN_DEVICES
 
 			wanrouter_script stop 
 
@@ -2432,11 +2544,6 @@
 			dev=$($GET_RC)
 			rm -f $RC
 
-			WAN_CONF=$WAN_CONF_DIR/$dev.conf
-
-			#Check that all configuration files exist
-			check_file $WAN_CONF || cleanup 1
-
 			#Check that modules are up and running
 			check_module
 			if [ $? -ne 0 ]; then
@@ -2452,39 +2559,12 @@
 				dev="wanpipe$ft1_or_wanpipe"
 			fi 
 
-			#If we are starting ft1 device there are no
-                        #interfaces. 
-			if [  $ft1_or_wanpipe -gt 0 ]; then
-				echo "Shutting down device: $dev, FT1 config mode"
-				echo "Shutting down device: $dev, FT1 config mode" >> $WAN_LOG
-			else
-				interf_down $dev $WAN_CONF
-				echo "Shutting down device: $dev"
-				echo "Shutting down device: $dev" >> $WAN_LOG
+			if [  $ft1_or_wanpipe -eq 0 ]; then
+				unconfig_interfaces $dev
 			fi
-			router_unconfig $WAN_CONF $dev
 
+			unconfig_devices $dev
 			
-			#Check if any devices are still running
-			#  If YES: don't unload the modules, just printout
-			#	   the list of active devices
-			#  If NO:  unload modules
-	
-			check_and_print_still_running && cleanup 0
-
-			cd $WAN_HOME
-			echo -e "No devices running, Unloading Modules"
-			remove_cdev
-			unload_module
-
-			INTERFACES=`grep ".*=.*$dev" -i $WAN_CONF | cut -d' ' -f1 2> /dev/null`
-			for ifname in $INTERFACES; do
-				ifname=${ifname%%=*}
-				wanrouter_script stop $dev $ifname
-			done
-
-			wanrouter_script stop $dev
-
 			[ -f $WAN_CONF_DIR/$FT1_CONF ] && rm -f $WAN_CONF_DIR/$FT1_CONF
 
 		else
@@ -2533,6 +2613,121 @@
 		fi			
 		;;
 
+	start_dev)	
+
+		if [ -z $2 ]; then	
+			# WANROUTER START_DEV
+
+			check_config || cleanup 1
+
+			load_module
+			if [ $? -ne 0 ]; then
+				cleanup 2
+			fi
+
+			config_devices $WAN_DEVICES
+				
+		elif [ -z $3 ]; then
+			#ROUTER START_DEV WANPIPE
+			
+			dev=$2
+
+			load_module
+			if [ $? -ne 0 ]; then
+				cleanup 2
+			fi
+
+			config_devices $dev
+			if [ $? -ne 0 ]; then
+				cleanup 3
+			fi
+		fi
+		;;
+
+	stop_dev)	
+		
+		if [ -z $2 ]; then
+			#WANROUTER STOP_DEV
+
+			check_module
+			if [ $? -ne 0 ]; then
+				unload_module
+				remove_cdev
+				echo -e "Router is already stopped !\n"
+				cleanup 1
+			fi
+
+			#Check that all wanpipe#.conf file defined in 
+			# WAN_DEVICES exist
+			check_config || {
+				unload_module
+				remove_cdev
+				echo -e "No devices running, Unloading Modules"
+				cleanup 1
+			}
+
+			if [ "$WAN_DEVICES_REV_STOP_ORDER" = "YES" ]; then
+				tmp_dev_list=
+				for dev in $WAN_DEVICES; do
+					tmp_dev_list="$dev ""$tmp_dev_list"
+				done
+				WAN_DEVICES=$tmp_dev_list
+			fi
+
+			unconfig_devices $WAN_DEVICES
+
+			wanrouter_script stop 
+
+		elif [ -z $3 ]; then
+			#WANROUTER STOP_DEV WANPIPE
+
+			dev=$2
+
+			if [ "$dev" = "all" ]; then
+                                stop_running_wanpipes
+				cleanup 0
+			fi
+
+			#Check that modules are up and running
+			check_module
+			if [ $? -ne 0 ]; then
+				unload_module
+				echo -e "Router is already stopped !\n";
+				cleanup 1;
+			fi
+
+			unconfig_devices $dev
+		fi
+		;;
+
+	start_ip)	
+
+		if [ -z $2 ]; then	
+			# WANROUTER START_IP
+			check_config || cleanup 1
+
+			config_interfaces $WAN_DEVICES
+
+			wanrouter_script start  
+				
+		elif [ -z $3 ]; then
+			#ROUTER START_IP WANPIPE
+			config_interfaces $2			
+		fi
+		;;
+
+	stop_ip)
+
+		if [ -z $2 ]; then
+			#WANROUTER STOP_IP
+			unconfig_interfaces $WAN_DEVICES
+
+		elif [ -z $3 ]; then
+			#WANROUTER STOP_IP WANPIPE
+			unconfig_interfaces $2
+		fi
+		;;
+
 	script)
 
 		#Debug Statement, used to test the script option
@@ -2576,6 +2771,31 @@
 		fi
 		cleanup 0;
 		;;
+
+	restart_dev)
+		eval "rm -f $WAN_PROG_LOCK 2> /dev/null > /dev/null"
+		if [ "$2" = "" ]; then
+			$0 stop_dev;
+			$0 start_dev;
+		elif [ "$3" = "" ]; then
+			$0 stop_dev $2;
+			$0 start_dev $2;
+		fi
+		cleanup 0;
+		;;
+
+	restart_ip)
+		eval "rm -f $WAN_PROG_LOCK 2> /dev/null > /dev/null"
+		if [ "$2" = "" ]; then
+			$0 stop_ip;
+			$0 start_ip;
+		elif [ "$3" = "" ]; then
+			$0 stop_ip $2;
+			$0 start_ip $2;
+		fi
+		cleanup 0;
+		;;
+
 	modules)
 
 		if [ $OSYSTEM = "Linux" ]; then
@@ -2618,14 +2838,12 @@
 			if [ $OSYSTEM = "Linux" ]; then
 				cat /proc/net/wanrouter/hwprobe_verbose
 			else
-				create_cdev
 				wanconfig hwprobe verbose
 			fi
 		else
 			if [ $OSYSTEM = "Linux" ]; then
 				cat /proc/net/wanrouter/hwprobe
 			else
-				create_cdev
 				wanconfig hwprobe
 			fi
 		fi
@@ -2708,21 +2926,22 @@
 		echo -e "	wanrouter start : Starts all devices specified in"
 		echo -e "                      $WAN_CONF_DIR/wanrouter.rc WAN_DEVICES\n"
 		echo -e "	wanrouter stop  : Stops all devices specified in"
-		echo -e "                      $WAN_CONF_DIR/wanrouter.rc WAN_DEVICES\n"
-		
+		echo -e "                      $WAN_CONF_DIR/wanrouter.rc WAN_DEVICES\n"	
 		echo -e "	wanrouter start wanpipe# : Start a single device\n"
 		echo -e "	wanrouter stop  wanpipe# : Stops a single device" 
 		echo -e "                               (# can range from 1 to 16)\n"
+		if [ $OSYSTEM = "Linux" ]; then
+			echo -e "	wanrouter start   wanpipe# if_name : Start a single interface on device\n"
+			echo -e "	wanrouter stop    wanpipe# if_name : Stops a single interface on device" 
+			echo -e "                                            (# can range from 1 to 16)\n"
+
+		fi
 		echo -e "	wanrouter restart	 : Restart all devices specified in"
 		echo -e "                      $WAN_CONF_DIR/wanrouter.rc WAN_DEVICES\n"
 		echo -e "	wanrouter restart wanpipe# : Restart a single device"
 		echo -e "                               (# can range from 1 to 16)\n"
 		echo
 		if [ $OSYSTEM = "Linux" ]; then
-			echo -e "	wanrouter start   wanpipe# if_name : Start a single interface on device\n"
-			echo -e "	wanrouter stop    wanpipe# if_name : Stops a single interface on device" 
-			echo -e "                                            (# can range from 1 to 16)\n"
-
 			echo -e "	wanrouter restart wanpipe# if_name : Restart a single interface on device"
 			echo -e "                                            (# can range from 1 to 16)\n"
 			echo
