#! /bin/sh
### BEGIN INIT INFO
# Provides:          network-manager
# Required-Start:    $remote_fs dbus udev
# Required-Stop:     $remote_fs dbus udev
# Should-Start:      $syslog
# Should-Stop:       $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: network connection manager
# Description:       Daemon for automatically switching network 
#        connections to the best available connection.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="network connection manager"
NAME="NetworkManager"

DAEMON=/usr/sbin/$NAME

PIDFILE=/var/run/$NAME/$NAME.pid

SCRIPTNAME=/etc/init.d/network-manager

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

test -f /etc/default/NetworkManager && . /etc/default/NetworkManager

#
# Function that starts the daemon/service.
#
d_start() {
  if [ "$(awk -v RS=' ' -v FS='=' '$1 == "net_rst" { print $2 ; exit }' /proc/cmdline)" == "true" ] ; then
    echo "Restore network cfg to default"
    find /etc/NetworkManager/system-connections/ -maxdepth 1 -type f -exec rm {} \;
    cp /etc/NetworkManager/system-connections/default/* /etc/NetworkManager/system-connections/
    rm -rf /var/lib/NetworkManager/*
    [ -x "/usr/share/NetworkManager/reset_to_default" ] && /usr/share/NetworkManager/reset_to_default
  fi
  start-stop-daemon --start --quiet --pidfile $PIDFILE \
    --exec $DAEMON -- $DAEMON_OPTS
}

#
# Function that stops the daemon/service.
#
d_stop() {
  start-stop-daemon --stop --retry 5 --quiet --pidfile $PIDFILE \
    --exec $DAEMON
}

log_daemon_msg_pre () { :; }
log_daemon_msg_post () { :; }
log_end_msg_pre () { :; }
log_end_msg_post () { :; }

log_daemon_msg () {
    if [ -z "${1:-}" ]; then
        return 1
    fi
    log_daemon_msg_pre "$@"

    if [ -z "${2:-}" ]; then
        echo -n "$1:" || true
        return
    fi
    
    echo -n "$1: $2" || true
    log_daemon_msg_post "$@"
}

# int log_end_message (int exitstatus)
log_end_msg () {
    # If no arguments were passed, return
    if [ -z "${1:-}" ]; then
        return 1
    fi

    local retval
    retval=$1

    log_end_msg_pre "$@"

    RED=''
    YELLOW=''
    NORMAL=''

    if [ $1 -eq 0 ]; then
        echo "." || true
    elif [ $1 -eq 255 ]; then
        /bin/echo -e " ${YELLOW}(warning).${NORMAL}" || true
    else
        /bin/echo -e " ${RED}failed!${NORMAL}" || true
    fi
    log_end_msg_post "$@"
    return $retval
}

log_progress_msg () {
    if [ -z "${1:-}" ]; then
        return 1
    fi
    echo -n " $@" || true
}

log_begin_msg () {
    if [ -z "${1:-}" ]; then
        return 1
    fi
    echo -n "$@" || true
}

log_success_msg () {
    if [ -n "${1:-}" ]; then
        log_begin_msg $@
    fi
    log_end_msg 0
}

log_failure_msg () {
    if [ -n "${1:-}" ]; then
        log_begin_msg $@ "..."
    fi
    log_end_msg 1 || true
}

log_warning_msg () {
    if [ -n "${1:-}" ]; then
        log_begin_msg $@ "..."
    fi
    log_end_msg 255 || true
}

pidofproc () {
    local pidfile base status specified pid OPTIND
    pidfile=
    specified=
    
    OPTIND=1
    while getopts p: opt ; do
        case "$opt" in
            p)  pidfile="$OPTARG"
                specified="specified"
    ;;
        esac
    done
    shift $(($OPTIND - 1))
    if [ $# -ne 1 ]; then
        echo "$0: invalid arguments" >&2
        return 4
    fi

    base=${1##*/}
    if [ ! "$specified" ]; then
        pidfile="/var/run/$base.pid"
    fi

    if [ -n "${pidfile:-}" -a -r "$pidfile" ]; then
        read pid < "$pidfile"
        if [ -n "${pid:-}" ]; then
            if $(kill -0 "${pid:-}" 2> /dev/null); then
                echo "$pid" || true
                return 0
            elif ps "${pid:-}" >/dev/null 2>&1; then
                echo "$pid" || true
                return 0 # program is running, but not owned by this user
            else
                return 1 # program is dead and /var/run pid file exists
            fi
        fi
    fi
    if [ -n "$specified" ]; then
        if [ -e "$pidfile" -a ! -r "$pidfile" ]; then
            return 4 # pidfile exists, but unreadable, return unknown
        else
            return 3 # pidfile specified, but contains no PID to test
        fi
    fi
    if [ -x /bin/pidof ]; then
        status="0"
        /bin/pidof -o %PPID -x $1 || status="$?"
        if [ "$status" = 1 ]; then
            return 3 # program is not running
        fi
        return 0
    fi
    return 4 # Unable to determine status
}

# Return LSB status
status_of_proc () {
    local pidfile daemon name status OPTIND

    pidfile=
    OPTIND=1
    while getopts p: opt ; do
        case "$opt" in
            p)  pidfile="$OPTARG";;
        esac
    done
    shift $(($OPTIND - 1))

    if [ -n "$pidfile" ]; then
        pidfile="-p $pidfile"
    fi
    daemon="$1"
    name="$2"

    status="0"
    pidofproc $pidfile $daemon >/dev/null || status="$?"
    if [ "$status" = 0 ]; then
        log_success_msg "$name is running"
        return 0
    elif [ "$status" = 4 ]; then
        log_failure_msg "could not access PID file for $name"
        return $status
    else
        log_failure_msg "$name is not running"
        return $status
    fi
}


case "$1" in
  start)
  log_daemon_msg "Starting $DESC" "$NAME"
  d_start
  case "$?" in
    0) log_end_msg 0 ;;
    1) log_progress_msg "already started"
       log_end_msg 0 ;;
    *) log_end_msg 1 ;;
  esac
  ;;
  stop)
  log_daemon_msg "Stopping $DESC" "$NAME"
  d_stop
  case "$?" in
    0) log_end_msg 0 ;;
    1) log_progress_msg "already stopped"
       log_end_msg 0 ;;
    *) log_end_msg 1 ;;
  esac
  ;;
  restart|force-reload)
  $0 stop
  $0 start
  ;;
  status)
  status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
  ;;
  *)
  echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
  exit 1
  ;;
esac

exit 0