#!/bin/sh

# use this script to compile the module with a 2.2.x version
# of the linux kernel 

RULES_LOC=sct_rules
LINKED_C_FILES="filter_eval filter_in_out filter_node_build encode_decode"
LINKED_C_FILES="$LINKED_C_FILES rule_engine filter_vars filter_vector"
LINKED_C_FILES="$LINKED_C_FILES filter_type_check membuf printer"

sct_make_c_symlinks() {
    for file in $LINKED_C_FILES; do
        ln -s $RULES_LOC/$file.c .;
    done;
}

del_c_symlinks() {
    for file in $LINKED_C_FILES; do
        rm -f $file.c;
    done;
}

sct_make_symlinks() {
    ln -s Makefile-2.2 Makefile
    sct_make_c_symlinks
}

del_symlinks() {
    rm Makefile
    del_c_symlinks
}

del_symlinks_error() {
    cd $MOD_DIR && del_symlinks
    del_c_symlinks
}


trap del_symlinks_error SIGINT

MOD_DIR=`pwd`
if [ x"$KERNEL_DIR" = "x" ];
then
    export KERNEL_DIR=/usr/src/linux
fi
if [ x"$CC" = "x" ];
then
    CC=gcc
fi

echo "MOD_DIR - $MOD_DIR"

sct_make_symlinks

cd $KERNEL_DIR && \
    make CC="$CC -D__KERNEL__ -I$KERNEL_DIR/include -I$RULES_LOC" SUBDIRS=$MOD_DIR modules

cd $MOD_DIR && del_symlinks
