#!/bin/bash

# 01/14/10 - added ubuntu vs. centos support
# 11/26/09 - first version

# written by dranch


#Set to 1 if you want verbose messages
VERBOSE=1

#Set to 1 if you want debugging messages printed
DEBUG=1

#Set any required PATHs to be distribution independent
LSMOD="`whereis lsmod | awk '{print $2}'`"
LSUSB="`whereis lsusb | awk '{print $2}'`"

if [ -f /etc/redhat-release ]; then
   DISTRO="Redhat"
   USBPATH="/sys/class/usb_device"
fi
if [ -n "`uname -n | grep buntu`" ]; then
   DISTRO="Ubuntu"
   USBPATH="/sys/class/usb_endpoint"
fi

echo -e "\nDistro: $DISTRO"
#echo -e "USBPATH: $USBPATH"

#These are the US Interface Navigators USB IDs
USBCATID="0403:b810"
USBWKEYID="0403:b811"
USBRS232ID="0403:b812"


#-------------------------------------------------------------------------
# Mainline code
#-------------------------------------------------------------------------

#Load the modified FTDI module if required
if [ -z "`$LSMOD | grep ^ftdi_sio`" ]; then
   echo -e "\n ftdi_sio module not loaded.  You will be prompted for sudo \
if you're not already root"
   sudo /sbin/modprobe ftdi_sio
fi

#Find the installed USB ID handles
##CAT and 2nd PTT
USBCATMAJ=`$LSUSB | grep $USBCATID | awk '{print $2}' | sed 's/0*//'`
USBCATMIN=`$LSUSB | grep $USBCATID | awk '{print $4}' | sed 's/0*//' | sed 's/://'`

##WKey & FSK
USBWKEYMAJ=`$LSUSB | grep $USBWKEYID | awk '{print $2}' | sed 's/0*//'`
USBWKEYMIN=`$LSUSB | grep $USBWKEYID | awk '{print $4}' | sed 's/0*//' | sed 's/://'`

##RS232 & Config
USBRS232MAJ=`$LSUSB | grep $USBRS232ID | awk '{print $2}' | sed 's/0*//'`
USBRS232MIN=`$LSUSB | grep $USBRS232ID | awk '{print $4}' | sed 's/0*//' | sed 's/://'`

if [ $DEBUG -eq 1 ]; then
   echo -e "\nDEBUG: USBCATMAJ   : $USBCATMAJ"
   echo -e "DEBUG: USBCATMIN   : $USBCATMIN"
   echo -e "DEBUG: USBWKEYMAJ  : $USBWKEYMAJ"
   echo -e "DEBUG: USBRS232MIN : $USBRS232MIN"
   echo -e "DEBUG: USBRS232MAJ : $USBRS232MAJ"
   echo -e "DEBUG: USBRS232MIN : $USBRS232MIN"
   echo -e "\n"
fi


echo -e "\nUS Interface Navigator:\n"
if [ $VERBOSE -eq 1 ]; then
   #interface IDs - CAT and 2nd PTT

   if [ $DEBUG -eq 1 ]; then
      echo -e "\nDEBUG: initial USB path"
      ls $USBPATH/usbdev$USBCATMAJ.$USBCATMIN\_ep00/device
      echo -e "\n"
   fi

   for ID in `ls $USBPATH/usbdev$USBCATMAJ.$USBCATMIN\_ep00/device | grep ^[0-9]`; do
     echo "Device ID: $USBCATID: bound TTY: `ls $USBPATH/usbdev$USBCATMAJ.$USBCATMIN\_ep00/device/$ID | grep ttyUSB` : \
description: `cat $USBPATH/usbdev$USBCATMAJ.$USBCATMIN\_ep00/device/$ID/interface`"
   done

   #interface IDs - WKey & FSK
   for ID in `ls $USBPATH/usbdev$USBWKEYMAJ.$USBWKEYMIN\_ep00/device | grep ^[0-9]`; do
     echo "Device ID: $USBWKEYID: bound TTY: `ls $USBPATH/usbdev$USBWKEYMAJ.$USBWKEYMIN\_ep00/device/$ID | grep ttyUSB` : \
description: `cat $USBPATH/usbdev$USBWKEYMAJ.$USBWKEYMIN\_ep00/device/$ID/interface`"
   done

   #interface IDs - RS232 & Config
   for ID in `ls $USBPATH/usbdev$USBRS232MAJ.$USBRS232MIN\_ep00/device | grep ^[0-9]`; do
     echo "Device ID: $USBRS232ID: bound TTY: `ls $USBPATH/usbdev$USBRS232MAJ.$USBRS232MIN\_ep00/device/$ID | grep ttyUSB` : \
description: `cat $USBPATH/usbdev$USBRS232MAJ.$USBRS232MIN\_ep00/device/$ID/interface`"
   done
fi

echo -e "\ndone.\n\n"

