#!/bin/bash

#set -x

#
# This script is essentially a wrapper for the real snazzy_can_tx executable. 
#

# The real snazzy_can_tx executables
SCT_SNAZZY_CAN_MESSAGE_EXE="/usr/bin/snazzy_can_tx_72"
SCT_OLD_SNAZZY_CAN_MESSAGE_EXE="/usr/bin/snazzy_can_tx_55"
SCT_CAN_MESSAGE_EXE=""

SNAZZY_VERSION=$(eDriveXLoadTool --get_firmware_version 1)

case $(( 10#$SNAZZY_VERSION )) in
    72) # eDX 2.0 & 2.1  
        echo "Newer firmware detected. Using ${SCT_SNAZZY_CAN_MESSAGE_EXE}."
        SCT_CAN_MESSAGE_EXE=${SCT_SNAZZY_CAN_MESSAGE_EXE}
        ;;

    *) # "54" | "55")
        echo "Older firmware detected. Using ${SCT_OLD_SNAZZY_CAN_MESSAGE_EXE}."
        SCT_CAN_MESSAGE_EXE=${SCT_OLD_SNAZZY_CAN_MESSAGE_EXE}
        ;; 
esac

# Safety check to make sure the correct snazzy exe exists
which $SCT_CAN_MESSAGE_EXE > /dev/null 2>&1
result=$?
if [ $result -eq 0 ]; then
   echo "Found $SCT_CAN_MESSAGE_EXE"
else
   echo "Could not find executable $SCT_CAN_MESSAGE_EXE - exiting!"
   exit $result
fi

# Call the real snazzy_can_tx, passing on the arguments
$SCT_CAN_MESSAGE_EXE $@

