#!/bin/bash

# Network interface configuration
#
# Copyright (c) 2002-2003 SuSE Linux AG Nuernberg, Germany.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
# Authors: Michal Svec <msvec@suse.cz>
#          Mads Martin Joergensen <mmj@suse.de>
#
# $Id: functions.common 1102 2004-09-28 17:37:01Z zoz $
#

#####################################################################
# Returnvalues
#
# I don't know currently, if all this returnvalues are necessary. Probably some
# of them will be removed. The values 0-7 should be very similar to the LSB
# return values for init scripts
R_SUCCESS=0        # interface has been set up
R_ERROR=1          # any unspecified error
R_USAGE=2          # wrong usage
R_NOTIMPL=3        # feature not implemented
R_NOTALLOWED=4     # insufficient privilege

R_NODEV=7          # the given interface does not exist
R_NOTCONFIGURED=5  # the bootmode does not match the current mode
R_INACTIVE=5       # the interface is not up and it should not
R_NOTRUNNING=7     # the interface is not up but it should be up

R_NOCONFIG=9       # we could not find a matching configuration file
R_BUSY=10          # the interface is busy and should not be shut down.
                   # It will be used for 'ifstatus ... -o stop' or ifdown ... -o
                   # check' or whatever we use for the check if a interface can
                   # be shut down savely.
R_LOCKED=11        # ifup/down is currently running on this hwdesc
R_DHCP_BG=12       # dhcp client is running in bg, but no address up to now
                   # or setting up firewall is still in progress
R_INTERNAL=$R_ERROR      # internal error, e.g. no config or missing scripts
R_NOT_UP2DATE=13   # returned from ifprobe if configuration has changed

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/etc/sysconfig/network/scripts

LOGGER=/bin/logger

logerror () {
	local LINE PREFX="ERROR: "
	test "$DEBUG" = "yes" && PREFX="`printf "%-20s ERROR: " $SCRIPTNAME`"
	test "$RUN_FROM_RC" = "yes" && PREFX="`printf "    %-9s " $INTERFACE`"

	while read LINE; do
		test "$USE_SYSLOG" = "yes" -a -x "$LOGGER" -a "$DEBUG" != "yes" &&
			$LOGGER -p user.err -t "$SCRIPTNAME" "$LINE"
		test "$BE_QUIET" != "yes" -o "$DEBUG" = "yes" &&
			echo -e "$PREFX$LINE"
		PREFX="       "
		test "$DEBUG" = "yes" && PREFX="                            "
		test "$RUN_FROM_RC" = "yes" && PREFX="              "
	done < <(echo -e "$*")
}

message () {
	test "$DEBUG" = "yes" && echo -e "$SCRIPTNAME $*" && return 0
	test "$BE_QUIET" = "yes" && return 1
	test -z "$*" && return
	echo -e "$*"
}

message_n () {
	test "$DEBUG" = "yes" && echo -en "$SCRIPTNAME $*" && return 0
	test "$BE_QUIET" = "yes" && test "$DEBUG" != "yes" && return 1
	echo -en "$*"
}

debug () {
	test "$DEBUG" = "yes" || return 1
	test -z "$*" && return
	echo -e "$SCRIPTNAME [DEBUG] $*"
}

message_if_not_run_from_rc () {
	if [ "$RUN_FROM_RC" = "yes" ]; then
		debug "$*"
	else
		message "$*"
	fi
	return 0
}

reverse () {
	local X=$1
	test -z "$X" && return
	shift
	reverse $*
	echo $X
}
