#!/bin/bash
#kstrax_ctr load helper script
#COPYRIGHT (C) HITACHI,LTD. 2005,2006
#Created by S.Moriya<s-moriya@sdl.hitachi.co.jp>

function usage() {
    echo "Usage : kernel-rec <start/stop> <module parameter(number of buffer entry)>"
    echo "Example1) ./record.sh start 2000"
    echo "Example2) ./record.sh start"
}

function create_IF() {
    local TMP_MAJOR=`grep kstrax /proc/devices`
    local MAJOR="${TMP_MAJOR%% kstrax}"
    local MINOR="0"
    local DEV_NAME="/dev/kstrax"
    mknod $DEV_NAME c $MAJOR $MINOR
    chmod o-rw $DEV_NAME 
}

function load_module() {
    local VERSION=`uname -r`
    local TMP_SYSCALL_TABLE=`grep sys_call_table /boot/System.map-$VERSION`
    local SYSCALL_TABLE=`echo $TMP_SYSCALL_TABLE | cut -f 1 -d \ `
    local KSTRAX_CTR=`/sbin/lsmod | grep -c kstrax_ctr`
    local KSTRAX_BUF=`/sbin/lsmod | grep -c kstrax_buf`

    if [ $KSTRAX_CTR = "0" ]; then
	/sbin/modprobe kstrax_ctr addr=0x$SYSCALL_TABLE
    fi

    if [ $# = "0" ]; then
	/sbin/modprobe kstrax_buf
    elif [ $# = "1" ]; then
	/sbin/modprobe kstrax_buf kbuffer_entry=$1
    fi    
    create_IF
}

function unload_module() {
    rm -f /dev/kstrax
    /sbin/rmmod kstrax_buf
#    echo kstrax_ctr is not unloaded.
}

if [ $# = "0" ]; then
    usage
else
    if [ $1 = "start" ]; then
	load_module $2
    elif [ $1 = "stop" ]; then
	unload_module
    else
	usage
    fi
fi
