#!/bin/sh -e
#
# Name: ifupdown
# Date: 2006-07-24 2:40
# Author: MontaVista Software, Inc. <source@mvista.com>
# Copyright: Copyright 1999-2003 MontaVista Software, Inc.
# License: 2003 (c) MontaVista Software, Inc. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
  #
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: S
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start/stop ifupdown daemon
# Description: start and stop the Ifupdown daemon
### END INIT INFO
# chkconfig: S 39 0

[ -x /sbin/ifup ] || exit 0
[ -x /sbin/ifdown ] || exit 0

. /lib/lsb/init-functions

MYNAME="${0##*/}"
report() { echo "${MYNAME}: $*" ; }
report_err() { log_failure_msg "$*" ; }
[ -r /etc/default/ifupdown ] && . /etc/default/ifupdown

# Note: The state file location is hardcoded in ifup|ifdown
IFSTATE=/etc/network/run/ifstate

myreadlink () {
  dest="${1%/}"
  extras=""

  while [ "$dest" != "" ]; do
    if [ -d "$dest" ]; then
      cd "$dest"
      dest=$(/bin/pwd)
      break
    fi

    if [ -L "$dest" ]; then
      d2=$(readlink "$dest")
      if [ "${d2#/}" = "$d2" ]; then
        dest="${dest%/*}/$d2"
      else
        dest="$d2"
      fi
    fi

    while [ ! -e "$dest" ]; do
      extras="${dest##*/}/$extras"
      if [ "${extras%%/*}" = ".." ]; then return 1; fi
      destx="${dest%/*}"
      if [ "$destx" = "$dest" ]; then destx=""; fi
      dest="$destx"
    done
  done
  dest="$dest/$extras"
  echo "${dest%/}"
}

case "$1" in
  start|restart)
    if [ "$2" ]; then
      report_err "Arguments to '$1' command not accepted"
      exit 3
    fi

    log_begin_msg "Setting up networking ..."

    # Create /etc/network/run

    # Clean up existing /etc/network/run
    # -rf switch to rm command covers both the cases when
    # /etc/network/run is a directory and a symbolic link
    if [ -d /etc/network/run -o -L /etc/network/run ]; then
      rm -rf /etc/network/run
    fi

    # The best choice to use for /etc/network/run is /dev/shm/network
    # If we can't use that, we'll just use the /etc/network directory
    WHAT_TO_USE=devshm

    # Check if /dev/shm is available and writable

    if [ ! -d /dev/shm -o ! -w /dev/shm -o ! -r /proc/mounts ]; then
      WHAT_TO_USE=owndir
    elif ! grep -qs "^tmpfs[[:space:]]\+/dev/shm[[:space:]]\+tmpfs[[:space:]]\+\([^[:space:]]\+,\)\?rw" /proc/mounts
    then
      WHAT_TO_USE=owndir
    elif grep -qs '[[:space:]]/dev[[:space:]]devfs[[:space:]]' /proc/mounts; then
      WHAT_TO_USE=owndir
    fi

    # Check for available space if we are using devshm
    if [ "$WHAT_TO_USE" = devshm ]; then
      SPACE=`df -k /dev/shm | tail -n 1 | awk '{if ($4 ~ /%/) { print $3 } else { print $4 } }'`
      if [ "$SPACE" -le 0 ]; then
        WHAT_TO_USE=owndir
      fi
    fi

    # Create a symbolic link from /etc/network/run
    # If WHAT_TO_USE is owndir then fall back to /etc/network
    if [ "$WHAT_TO_USE" = devshm ]; then
      [ -d /dev/shm/network ] || mkdir /dev/shm/network
      ln -s /dev/shm/network /etc/network/run
    else
      ln -s /etc/network /etc/network/run
    fi

    # Create the state file
    # Doing this also signals that ifupdown is available for use
    if [ ! -r "$IFSTATE" ]; then
      if ! : > "$IFSTATE" ; then
        report_err "Failure initializing $IFSTATE"
        log_end_msg 1
        exit 1
      fi 
    fi

    log_end_msg 0
    exit 0
    ;;

  stop)
    if [ "$2" ]; then
      report_err "Arguments to '$1' command not accepted"
      exit 3
    fi
    if [ -x /etc/init.d/ifupdown-clean ]; then
      /etc/init.d/ifupdown-clean start
    fi
    ;;

  force-reload)
    ;;

  *)
    echo "Usage: $0 {start|stop|restart|force-reload}" >&2
    exit 3
    ;;
esac

exit 0
