# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler NetworkConfigOptions
AddConfigHelp "DownInterfaces auto|<ifname> [...]" "The names of network interfaces to bring down before suspending. If the parameter \"auto\" is given, all interfaces which are not lo are brought down."
AddConfigHelp "UpInterfaces auto|<ifname> [...]" "The names of network interfaces to bring up after suspending. If the parameter \"auto\" is given, the interfaces stopped before suspending will be started in reverse order."

NetworkStop() {
    # If the script stops networking altogether, there's not much we can do.
    if [ -n "$NETWORK_CALL_ONCE" ] ; then
	network_ifdown
	[ $? -eq 0 ] && return 0
	return 1
    fi

    local ret
    local int
    local do_auto
    NETWORK_DOWNEDIFS="" # for saving the downed interfaces, for "auto"
    ret=0
    do_auto=
    case $NETWORK_DOWNIFS in *\ auto\ *) do_auto=yes ;; esac
    for int in `sed -rne 's,[[:space:]]*([[:alnum:]]+):.*,\1,p' /proc/net/dev`; do
	if [ -z "$do_auto" ] ; then
	    # Only proceed if we were given "auto" or this interface was meant
	    # to be brought down.
	    case "$NETWORK_DOWNIFS" in *\ $int\ *) ;; *) continue ;; esac
	else
	    # Don't bring down lo in auto mode.
	    [ "$int" = "lo" ] && continue
	fi
	vecho 2 "Bringing down interface $int"
	network_ifdown $int
	[ $? -ne 0 ] && ret=1
	NETWORK_DOWNEDIFS="$int $NETWORK_DOWNEDIFS"
    done
    return $ret
}

NetworkStart() {
    # If the script starts networking altogether, there's not much we can do.
    if [ -n "$NETWORK_CALL_ONCE" ] ; then
	network_ifup
	[ $? -eq 0 ] && return 0
	return 1
    fi

    local ret
    local int
    ret=0
    for int in $NETWORK_UPIFS ; do
	if [ "$int" = "auto" ] ; then
	    for int in $NETWORK_DOWNEDIFS ; do
		vecho 2 "Bringing up interface $int (from auto)"
		network_ifup $int
		[ $? -ne 0 ] && ret=1
	    done
	    continue
	fi
	vecho 2 "Bringing up interface $int"
	network_ifup $int
	[ $? -ne 0 ] && ret=1
    done
    return 0
}

NetworkConfigOptions() {
    local opt
    opt="$1"
    shift
    case $opt in
	downinterfaces)
	    NETWORK_DOWNIFS="$NETWORK_DOWNIFS $* "
	    ;;
	upinterfaces)
	    NETWORK_UPIFS="$NETWORK_UPIFS $* "
	    ;;
	*)
	    return 1
    esac
    if [ -z "$NETWORK_HOOKED" ] ; then
	AddSuspendHook 60 NetworkStop
	AddResumeHook 60 NetworkStart
	NetworkDetectDistro
	NETWORK_HOOKED=1
    fi
    return 0
}

NetworkDetectDistro() {
    # Use either a given $DISTRIBUTION or autodetect one.
    case "$DISTRIBUTION" in
	gentoo)
	    network_ifup() {
	    	[ -x "/etc/init.d/net.$1" ] && /etc/init.d/net.$1 start
	    }
	    network_ifdown() {
	    	[ -x "/etc/init.d/net.$1" ] && /etc/init.d/net.$1 stop
	    }
	    ;;
	suse)
	    network_ifup() {
		/etc/init.d/network start
	    }
	    network_ifdown() {
		/etc/init.d/network stop
	    }
	    NETWORK_CALL_ONCE=1
	    ;;
	debian|ubuntu)
	    network_ifup() {
		start-stop-daemon --start --background \
		    --startas /sbin/ifup --name "hibernate_ifup_$1" -- $*
	    }
	    network_ifdown() {
		/sbin/ifdown $*
	    }
	    ;;
	mandrake|fedora|redhat)
	    network_ifup() {
		/sbin/ifup $*
	    }
	    network_ifdown() {
		/sbin/ifdown $*
	    }
	    ;;
	slackware)
	    network_ifup() {
		/etc/rc.d/rc.inet1 start
	    }
	    network_ifdown() {
		/etc/rc.d/rc.inet1 stop
	    }
	    NETWORK_CALL_ONCE=1
	    ;;
    	*)
	    # Auto-detect
	    if [ -x "/sbin/ifup" ] ; then
		network_ifup() {
		    /sbin/ifup $*
		}
		network_ifdown() {
		    /sbin/ifdown $*
		}
	    elif [ -x "/etc/init.d/ifup" ] ; then
		network_ifup() {
		    /etc/init.d/ifup $*
		}
		network_ifdown() {
		    /etc/init.d/ifdown $*
		}
	    elif [ -x "/etc/sysconfig/network-scripts/ifup" ] ; then
		network_ifup() {
		    /etc/sysconfig/network-scripts/ifup $*
		}
		network_ifdown() {
		    /etc/sysconfig/network-scripts/ifdown $*
		}
	    elif [ -x "/etc/init.d/networking" ] ; then
		network_ifup() {
		    /etc/init.d/networking start
		}
		network_ifdown() {
		    /etc/init.d/networking stop
		}
		NETWORK_CALL_ONCE=1
	    elif [ -x "/etc/init.d/network" ] ; then
		network_ifup() {
		    /etc/init.d/network start
		}
		network_ifdown() {
		    /etc/init.d/network stop
		}
		NETWORK_CALL_ONCE=1
	    else
	    	network_ifup() {
		    /sbin/ifconfig $1 up
		}
		network_ifdown() {
		    /sbin/ifconfig $1 down
		}
	    fi
    esac
    return 0
}

# $Id: network 1196 2008-05-01 11:58:52Z nigel $
