#!/usr/bin/sh

export CMEM_PARAMS="gfpbpa_size=8192 gfpbpa_quantum=4"

case "$1" in
  start)
        echo "Starting cmem driver "
        # load the module
        modprobe cmem_rcc $CMEM_PARAMS
        # remove old device nodes
        rm -f /dev/cmem_rcc
        rm -f /dev/cmem_rcc_2
        # get major number
        major=`awk "\\$2==\"cmem_rcc\" {print \\$1}" /proc/devices`
        echo major number for cmem_rcc is $major
        # make device node
        mknod /dev/cmem_rcc c $major 0
        mknod /dev/cmem_rcc_2 c $major 1
        #give permissions
        chmod 666 /dev/cmem_rcc
        chmod 666 /dev/cmem_rcc_2
        echo

        echo "Starting flxnet driver "
        # load the module
        modprobe flxnet_dev
        modprobe flxnet_pcie
        interfaces=$(ls /sys/class/net)
        if [[ $interfaces == *"flxnet"* ]]; then
          echo "Restarting DHCPD server"
          systemctl --system daemon-reload
          systemctl restart dhcpd.service
        else
          echo "No flxnet interfaces found, not restarting DHCP server"
        fi
        
        export FELIXCARDS=`/sbin/lspci -n | grep -c -e "10ee:703" -e "10dc:042"`
        echo $FELIXCARDS flx PCIe endpoints found
        FIRSTCARD=0
        if [ $FELIXCARDS -gt 0 ]; then
          echo "Starting flx driver "
          # load the module
          modprobe flx
          # remove old device nodes
          rm -f /dev/flx*
          # get major number
          major=`awk "\\$2==\"flx\" {print \\$1}" /proc/devices`
          echo major number for flx is $major
          for (( c=$FIRSTCARD; c<$FELIXCARDS; c++ ))
          do
            echo "creating node /dev/flx$c"
            # make device node
            mknod /dev/flx$c c $major $c
            # give permissions
            chmod 666 /dev/flx$c
          done
          for filename in /dev/xil_xvc/cfg_ioc*; do
            if [ -f $filename ]; then
              echo "changing mode of $filename to 666"
              chmod 666 $filename
            else
              echo "No Xilinx virtual cable (XVC) found, skipping chmod"
            fi
          done
          #create a link from flx0 to flx for compatibility between new driver and old library
          ln -s /dev/flx0 /dev/flx
          echo
        fi
        ;;

  stop)
        echo "Shutting down cmem_rcc driver "
        /sbin/rmmod cmem_rcc
        # remove old device nodes
        rm -f /dev/cmem_rcc
        rm -f /dev/cmem_rcc_2

        /sbin/lsmod | grep flx
        if [ $? = 0 ]; then
          echo "Shutting down flx driver "
          /sbin/rmmod flx
          # remove old device nodes
          rm -f /dev/flx*
        fi
        
        /sbin/lsmod | grep flxnet
        if [ $? = 0 ]; then
          echo "Shutting down flxnet driver "
          # before we remove the flxnet drivers we first bring down the ethernet interfaces flxnet*
          interfaces=$(ls /sys/class/net)
          for if in $interfaces
          do
            if [[ $if == "flxnet*" ]]; then
              ifconfig $if down
            fi
          done
          /sbin/rmmod flxnet_pcie
          /sbin/rmmod flxnet_dev
          # remove old device nodes
        fi
        ;;

   status)
        /sbin/lsmod | grep cmem_rcc
        if [ $? = 0 ]; then
          echo ""
          echo ">>>>>> Status of the cmem_rcc driver"
          echo ""
          more /proc/cmem_rcc
        fi

        /sbin/lsmod | grep flx
        if [ $? = 0 ]; then
          echo ""
          echo ">>>>>> Status of the flx driver "
          echo ""
          more /proc/flx
        fi
        ;;

  *)
        echo "*** Usage: flx-drivers {start|stop|status}"
        exit 1

esac

exit 0
