#!/bin/sh
#
# Copyright (c) 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: ix1a,v 1.3.2.1 2005/05/27 16:29:31 thomas Exp $
# Creation date        23.03.2005
# Project              CAPI for BSD
#
# /etc/rc.d/ix1a - Startup script for the IX1 active ISDN controllers
#

# PROVIDE: capictlr capi ix1a
# REQUIRE: capibase
# KEYWORD: FreeBSD CAPI IX1 ITK ix1a shutdown



. /etc/rc.subr

name="ix1a"
rcvar=`set_rcvar`
start_cmd="ix1a_start"
stop_cmd="ix1a_stop"



# load a specific CAPI controller driver module
load_controller_driver ()
{
	if ! sysctl capi.${1}.version > /dev/null 2>&1; then
	        echo -n "${1} "
		if kldload "${1}"; then
			debug "CAPI controller driver module ${1} loaded."
		else
			warn "CAPI controller driver module ${1} failed to load."
		fi
	fi
}



# unload a specific CAPI controller driver module
unload_controller_driver ()
{
	if kldstat -n ${1} > /dev/null 2>&1; then
		if kldunload "${1}"; then
			debug "CAPI controller driver module ${1} unloaded."
		else
			warn "CAPI controller driver module ${1} failed to unload."
		fi
	fi
}



# perform a specific controller download command
perform_download_command ()
{
	eval load_cmd=\$${1}_load_cmd
	if [ -z "${load_cmd}" ]; then
		load_cmd=${1}ctl
	fi

	eval load_flags=\$${1}_load_flags
	if [ -z "${load_flags}" ]; then
		load_flags="-q"
	fi
	
	eval load_conf=\$${1}_load_conf
	if [ -z "${load_conf}" ]; then
		load_conf=/etc/${1}.cfg
	fi

	if [ ! -r "${load_conf}" ]; then
		info "Configuration file \"${load_conf}\" does not exist, create default."
                
                if "${load_cmd}" -i ${load_flags} -f "${load_conf}"; then
                        debug "Initial default configuration file created successfully."
                else
                        warn "Error creating initial default configuration file."
                fi
        fi
         
        if [ -r "${load_conf}" ]; then
		info "\"${load_cmd}\" ${load_flags} -f \"${load_conf}\""
		if "${load_cmd}" ${load_flags} -f "${load_conf}"; then
			debug "Download command for ${1} successful."
		else
			warn "Download command for ${1} failed."
		fi
	else
                warn "Configuration file \"${load_conf}\" does not exist."
	fi
}



ix1a_start ()
{
	load_controller_driver "${name}"
	
	perform_download_command "${name}"
}



ix1a_stop ()
{
	ix1a_load_flags="-r"
	perform_download_command "${name}"

	unload_controller_driver "${name}"
}



load_rc_config $name
run_rc_command "$1"
