#!/bin/sh
##############################################################################
#
# sgtlpatch - determine version of SigmaTel Infrared device
#             and apply appropriate patch
#
#   Copyright (C) 2004, SigmaTel, Inc. <irquality@sigmatel.com>
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place Ste 330, Boston MA 02111, USA
#
##############################################################################
#
# NOTES:
#   1) The device must already be enumerated
#   2) This mounts usbdevfs if not already mounted.
#   3) This only locates and patches the first device.
#   4) The patch files and the stir42xx application are expected
#      to be in the same directory as this script.
#
##############################################################################

DEVICE_VID=066f
DEVICE_PID=4210
PROCBUSUSB=/proc/bus/usb
USBDEVICES=${PROCBUSUSB}/devices
PATCHDIR=`dirname $0`

# mount usbdevfs if not already
if [ \! -r ${USBDEVICES} ] ; then
    echo $0: mounting usbdevfs
    mount -t usbdevfs none ${PROCBUSUSB}
    if [ \! -r ${USBDEVICES} ] ; then
        echo unable to mount usbdevfs 1>&2
        exit 1
    fi
fi

VIDPIDREV=`grep "^P:.*Vendor=${DEVICE_VID} ProdID=${DEVICE_PID}" ${USBDEVICES} | head -1`
if [ "${VIDPIDREV}" = "" ] ; then
    echo cannot find device 1>&2
    exit 1
fi

DEVICE_REV=`echo ${VIDPIDREV} | sed -n -e 's/.* Rev=\([0-9]*\)\.\([0-9]*\).*$/\1\2/p' ${USBDEVICES}`

PATCHFILE=${PATCHDIR}/${DEVICE_PID}${DEVICE_REV}.sb
${PATCHDIR}/stir42xx irda0 $* -p${PATCHFILE}

