#! /bin/bash
#
# Copyright (c) 2002-2004 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: Christian Zoz <zoz@suse.de>
#
# $Id: ifup-services 1090 2004-09-17 07:44:56Z zoz $
#

usage () {
	echo $@
	echo "Usage: if{up,down}-skel <config> [<interface>] [-o <options>]"
	echo "  In most cases config==interface, for details see ifup(8)."
	echo
	echo "Options are: option    : explanation"
	echo "All other or wrong options are silently ignored."
	echo
	echo "      This is just a skeleton."
	echo "  !!! PLEASE MODIFY THESE LINES !!!"
	exit $R_USAGE
}

######################################################################
# change the working direcory and source some common files
#
R_INTERNAL=1      # internal error, e.g. no config or missing scripts
cd /etc/sysconfig/network || exit $R_INTERNAL
test -f scripts/functions && . scripts/functions || exit $R_INTERNAL
test -f ./config && . ./config

######################################################################
# check arguments and how we are called (in case of links)
#
SCRIPTNAME=${0##*/}
ACTION=${SCRIPTNAME#if}
ACTION=${ACTION%%-services}
debug $*
case "${SCRIPTNAME}" in
	ifup-*) true ;;
	ifdown-*) true ;;
	*) usage
esac
INTERFACE=$1
case "$INTERFACE" in ""|-h|*help*) usage; esac
shift
if [ -n "$1" -a "$1" != "-o" ] ; then
	CONFIG=$INTERFACE
	INTERFACE=$1
fi
shift
test "$1" = "-o" && shift
OPTIONS="$@"
MODE=manual
while [ $# -gt 0 ]; do
	case $1 in
		boot|onboot) MODE=auto ;;
		hotplug)     MODE=auto ;;
		auto)        MODE=auto ;;
		quiet)       BE_QUIET=yes ;;
		debug)       BE_QUIET=no
		             DEBUG=yes ;;
		*)           debug unknown option $1 ;;
	esac
	shift
done

######################################################################
# get the interface and check if it is available or up
#
# if ! is_iface_available  $INTERFACE ; then
# 	logerror "interface ${INTERFACE} is not available"
# 	exit $R_NODEV
# fi
# if ! is_iface_up $INTERFACE ; then
# 	logerror "interface ${INTERFACE} is not up"
# 	exit $R_NOTRUNNING
# fi

######################################################################
# check presence of configuration file and source it
#
test -f ./ifcfg-$CONFIG && . ./ifcfg-$CONFIG
if [ -d "ifservices-$CONFIG" ] ; then
	cd ifservices-$CONFIG
else
	debug "No services to handle"
	exit 0
fi

# expand file pattern below to null string if no file matches
shopt -s nullglob
case $ACTION in
	up)
		message "Starting services"
		for SERVICE in S*; do
			./$SERVICE start
		done
		;;
	status)
		message "Checking services"
		for SERVICE in S*; do
			./$SERVICE status
		done
		;;
	down)
		message "Stopping services"
		for SERVICE in K*; do
			./$SERVICE stop
		done
		;;
esac
