#!/bin/bash
#
# This script utilizes netcfg-daemon.

. /etc/rc.conf
. /etc/rc.d/functions
. /usr/lib/network/globals

case "$1" in
    start)
        if ! ck_daemon net-profiles; then
            exit_stderr "net-profiles has already been started.  Try '/etc/rc.d/net-profiles restart'"
        fi

        # Ensure any device renaming has occurred as intended
        for daemon in "${DAEMONS[@]}"; do
            if [[ "$daemon" = "net-rename" ]]; then
                if ck_daemon net-rename; then
                    /etc/rc.d/net-rename start
                fi
          fi
        done

        # $NET env var is passed from the kernel boot line
        if [[ "$NET" = "menu" ]]; then
            if /usr/bin/netcfg-menu; then
                mv "$STATE_DIR"/{menu,netcfg-daemon}        # JP: user may want to disconnect profile by calling net-profiles stop
                add_daemon net-profiles
                exit 0
            fi
        elif [[ -n "$NET" ]]; then
            if /usr/bin/netcfg check-iface "$NET"; then
                echo "$NET" > "$STATE_DIR/netcfg-daemon"        # JP: user may want to disconnect profile by calling net-profiles stop
                add_daemon net-profiles
                exit 0
            fi
        elif /usr/bin/netcfg-daemon start; then
            add_daemon net-profiles
        else
            exit_err "No profile started."
        fi
        ;;
    stop)
        if ck_daemon net-profiles; then
            exit_stderr "net-profiles is not running"
        fi
        /usr/bin/netcfg-daemon stop
        rm_daemon net-profiles
        ;;
    restart)
        "$0" stop
        sleep 1
        "$0" start
        ;;
    *)
        exit_stderr "Usage: $0 {start|stop|restart}"
esac

# vim: ft=sh ts=4 et sw=4:
