#!/bin/sh
#
# Copyright (c) 2003-2005 "Thomas Wintergerst" <twinterg@gmx.de>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
# $Id: capibase,v 1.1.2.1 2005/05/27 16:29:31 thomas Exp $
# Creation date        26.07.2003
# Project              CAPI for BSD
#
# /etc/rc.d/capibase - Startup script for the kernel CAPI subsystem
#

# PROVIDE: capibase capi
# REQUIRE: root
# BEFORE: netif
# KEYWORD: FreeBSD CAPI



. /etc/rc.subr

name="capi"
rcvar=`set_rcvar`
start_cmd="capi_start"
stop_cmd="capi_stop"



# Start the CAPI subsystem if enabled by system configuraion
capi_start ()
{
	echo -n 'CAPI subsystem startup: '

	# Start the kernel CAPI manager module if not already loaded
	if ! sysctl capi.kcapimgr.version > /dev/null 2>&1; then
	        echo -n 'kcapimgr '
		if kldload kcapimgr; then
			debug 'CAPI manager module loaded.'
		else
			warn 'CAPI manager module failed to load.'
		fi
	fi

	# Start the CAPI device driver as the link to user space
	if ! sysctl capi.capidev.version > /dev/null 2>&1; then
	        echo -n 'capidev '
		if kldload capidev; then
			debug 'CAPI device driver loaded.'
		else
			warn 'CAPI device driver failed to load.'
		fi
	fi
	
	echo
}



# Stop the CAPI subsystem
capi_stop ()
{
	if kldstat -n capidev > /dev/null 2>&1; then
		if kldunload capidev; then
			debug 'CAPI device driver unloaded.'
		else
			warn 'CAPI device driver failed to unload.'
		fi
	fi
	
	if kldstat -n kcapimgr > /dev/null 2>&1; then
		if kldunload kcapimgr; then
			debug 'CAPI manager module unloaded.'
		else
			warn 'CAPI manager module failed to unload.'
		fi
	fi
}



load_rc_config $name
run_rc_command "$1"
