#
#
#
test_startmode() {
    test "$STARTMODE" = "off" && return 6
    test "$MODE" = "all" -a "$STARTMODE" = "hotplug" && return 99
    test "$MODE" = "all" && return 0
    test "$STARTMODE" = "$MODE" && return 0
    test "$MODE" = "manual" && return 0 
    return 5
}
#
# checks if an driver is allready loaded
#
test_driver_loaded() {
    local module rest ret
    ret=0
    while read module rest ; do
        # 10 for busy 
	test "$1" == "${module}" && ret=10
    done < <(cat /proc/modules)
    return $ret
}

#
# get the first free idx for registering
# ISDN cards
# result: free_idx
#
get_first_free_idx() {
    local -a _idxa
    local _lin _it
    free_idx=-1
    read _it _lin < <(head -n 1 /dev/isdninfo)
    test "$?" = "0" || return
    read -a _idxa < <(echo $_lin)
    while [ $free_idx -lt 65 ]; do
	free_idx=$(($free_idx + 1))
	if [ "${_idxa[$free_idx]}" = "-" ]; then
	    free_idx=$(($free_idx / 2))
	    return
	fi
    done
    free_idx=-1
}

#
# get the card id for index $1
# result: card_id
#
get_i4l_id() {
    local -a _idxa
    local _lin _it _id
    card_id="-"
    read _it _lin < <(head -n 1 /dev/isdninfo)
    test "$?" = "0" || return
    read -a _idxa < <(echo $_lin)
    _id=$1
    _id=${_id:=0}
    _id=$(($_id * 2))
    card_id=${_idxa[$_id]}
}

#
# start_isdnlog
# $1  index of card
# $2  name of controller (contrX)
#
start_isdnlog() {
    test "$ISDNLOG_START" = "no" && return
    optionfile=/etc/isdn/isdnlog.options.$2
    test -r $optionfile || return
    devidx=$(($1 * 2))
    ilogdev=isdnctrl${devidx}
    /usr/sbin/isdnlog -f $optionfile /dev/$ilogdev
    echo "$ilogdev" > /var/run/isdnlog.$2
}

#
# kill_isdnlog
# $1  name of controller (contrX)
#
kill_isdnlog() {
    devfile=/var/run/isdnlog.$1
    test -f $devfile || return
    dev=`cat $devfile`
    pidfile=/var/run/isdnlog.${dev}.pid 
    test -r $pidfile || return
    /bin/kill `cat $pidfile` >& /dev/null
    rm -f $devfile
    rm -f $pidfile
}
