#!/bin/bash -x

if [ ! -f /home/disk0/frontend.env ]; then
        echo /home/disk0/frontend.env not found
        exit 1
fi
. /home/disk0/frontend.env

kernel_rel=$(uname -r)

nvidia_drivers="/usr/local/nvidia/$NVIDIA_VER/$kernel_rel/kernel/drivers/video"
ssound_drivers="/usr/local/ssound/$SSOUND_VER/$kernel_rel/kernel/drivers/ssound"

basebd_drivers="/usr/local/basebd/$BASEBD_VER/$kernel_rel/kernel/drivers/bb"
ssound_libs="/usr/local/ssound/$SSOUND_VER/lib"

cf_drv_path="/lib/modules/$kernel_rel/kernel/drivers"
cf_nvidia_drivers="$cf_drv_path/video"
cf_ssound_drivers="$cf_drv_path/ssound"
cf_basebd_drivers="$cf_drv_path/bb"

driver_path="/home/%DRV_MOUNT%"

module_load_from(){
        ret=$(lsmod | grep $1)
        if [ -n "$ret" ]; then
                ret=$(ksyms -m | awk '/__insmod_'$1'_O/ {print $2}')
                ret=${ret#__insmod_$1_O}
                ret=${ret%/*}
        fi
}
cmp_module(){
        cmp -s $2/$1.o $3/$1.o
        if [ $? = 0 ]; then
                return 0
        fi

        return 1
}
cmp_loaded_module(){
        module_load_from $1
        if [ -z "$ret" ]; then
                return 1
        fi

        if ! cmp_module $1 $ret $2
        then
                return 1
        fi

        return 0
}

app_path=${0%/*}
ferr=0

if [ -z "$LAST_LDLIBPATH" ]; then
        export LAST_LDLIBPATH=$LD_LIBRARY_PATH
fi
export LD_LIBRARY_PATH=$ssound_libs:$LAST_LDLIBPATH

if ! cmp_loaded_module basebd $basebd_drivers
then
        rmmod basebd

        insmod $basebd_drivers/basebd.o
fi
