#! /bin/sh

if [ -z $LVS_ROOT ]; then
    basedir="/opt/synview"
else
    basedir="${LVS_ROOT}"
fi
drvdir="${basedir}/drivers/lvsm"

initscript="sv.synview.sm"

ret_val=0

case "$1" in
    compile)
    # cd to driver directory and freshly rebuild
    cd $drvdir
    touch lvsm.core.c
    make
    if [ -e lvsm.ko ]; then
    ret_val=0 # make was successfull
    else
    echo "Error: no kernel module build! Check if kernel headers are installed."
    exit 1
    fi
    exit $ret_val
    ;;

    install)
    # cd to driver directory and freshly rebuild
    cd $drvdir
    touch lvsm.core.c
    make
    if [ -e lvsm.ko ]; then
    ret_val=0 # make was successfull
    else
    echo "Error: no kernel module build! Check if kernel headers are installed."
    exit 1
    fi
    # install the driver init script
    cd /etc/init.d
    if [ -x /usr/lib/lsb/install_initd ]; then
        /usr/lib/lsb/install_initd ${initscript}
    elif [ -x /sbin/chkconfig ]; then
        /sbin/chkconfig --add ${initscript}
    elif [ -x /usr/sbin/update-rc.d ]; then
        /usr/sbin/update-rc.d ${initscript} defaults 90 10
    else # last resort
        for i in 2 3 4 5; do
            ln -sf ${initscript} /etc/rc${i}.d/S90${initscript}
        done
        for i in 0 1 6; do
            ln -sf ${initscript} /etc/rc${i}.d/K10${initscript}
        done
    fi
    # and finally start it
    ./${initscript} start
    exit $ret_val
    ;;

    try-install)
    # only if the device is found
    nr_devs=$(lspci -d 1124:* | wc -l)
    if [ $nr_devs -gt 0 ]; then
        $0 install
        exit $? # exit with status of last command
    fi
    exit $ret_val
    ;;
    
    uninstall)
    # try to stop the driver (silently if not started)
    cd /etc/init.d
    ./${initscript} try-stop
    # and uninstall the init script
    if [ -x /usr/lib/lsb/remove_initd ]; then
        /usr/lib/lsb/remove_initd ${initscript}
    elif [ -x /sbin/chkconfig ]; then
        /sbin/chkconfig --del ${initscript}
    elif [ -x /usr/sbin/update-rc.d ]; then
        /usr/sbin/update-rc.d -f ${initscript} remove
    else # last resort
        rm -f /etc/rc?.d/???${initscript}
    fi
    exit $ret_val
    ;;

   reset)
    # rescan pci bus after CorSight device reset

    # check CorSight devices
    CorSight1=$(lspci -d 1124:0064 | wc -l)
    CorSight2=$(lspci -d 1124:0065 | wc -l)
    CorSight3=$(lspci -d 1124:0066 | wc -l)

    if   [ $CorSight1 -gt 0 ]; then
	dev=0000:03:00.0
    elif [ $CorSight2 -gt 0 ]; then
	dev=0000:01:00.0
    elif [ $CorSight3 -gt 0 ]; then
 	dev=0000:01:00.0
    else
	exit 1
    fi

    cd /etc/init.d
    ./${initscript} try-stop
    echo "Stop CorSight streaming device driver."

    port=$(basename $(dirname $(readlink "/sys/bus/pci/devices/$dev")))

    echo "Removing $dev..."
    echo 1 > "/sys/bus/pci/devices/$dev/remove"

    echo "Performing hot reset of port $port..."

    bc=$(setpci -s $port BRIDGE_CONTROL)

    setpci -s $port BRIDGE_CONTROL=$(printf "%04x" $((0x$bc | 0x40)))
    sleep 0.01
    setpci -s $port BRIDGE_CONTROL=$bc
    sleep 0.5

    echo "Rescanning bus..."

    echo 1 > "/sys/bus/pci/devices/$port/rescan"

    ./${initscript} start
    echo "Start CorSight streaming device driver."
    exit $ret_val
    ;;

    *)
    echo "Usage: $0 {compile|install|uninstall|try-install|reset}"
    exit 1
    ;;
esac

# If we got here somehow without exiting, return the ret_val
exit $ret_val

