#!/bin/sh

# handy functions for rmmod/insmod
function xrmmod () {
	grep -qe "^$1" /proc/modules || return
	echo rmmod $1
	rmmod $1 || exit 1
}
function xinsmod () {
	echo insmod $*
	insmod -f $* || exit 1
}

# Source function library.
. /etc/rc.d/init.d/functions

# Source configuration options
. /etc/sysconfig/bttv

# prepare for crashing the box -- flush dirty buffers
sync; sleep 1; sync

export MODPATH=/lib/modules/bttv

case "$1" in
  start)
    $0 stop
    echo "Initializing TV and/or radio modules..."
    xinsmod videodev
    xinsmod i2c verbose=$I2C_VERBOSE scan=$I2C_SCAN i2c_debug=$I2C_DEBUG
    test -f $MODPATH/i2c_chardev.o && xinsmod i2c_chardev
    xinsmod tuner debug=$TUNER_DEBUG type=$TUNER_TYPE
    xinsmod msp3400		
    xinsmod bttv radio=$BTTV_RADIO vidmem=$BTTV_VIDMEM card=$BTTV_CARD
    if [ ! -f $MODPATH/tvee.h ]; then $MODPATH/readee > $MODPATH/tvee.h; fi
    exit 0
    ;;

  stop)
    echo "Deactivating TV and/or radio modules..."
    xrmmod bttv
    xrmmod msp3400
    xrmmod tuner
    xrmmod i2c_chardev
    xrmmod i2c-dev
    xrmmod algo-bit
    xrmmod i2c
    xrmmod videodev
    exit 0
    ;;

  restart|reload)
    $0 stop
    $0 start
    exit 0
    ;;

  *)
    # do not advertise unreasonable commands that there is no reason
    # to use with this device
    echo "Usage: bttv {start|stop|status|restart|reload}"
    exit 1

esac

