#!/bin/sh

##uncomment to debug
#set -x
#exec 2>>/home/root/dbg/firmware.out   
#exec 1>&2
#echo $0 $* >&2

if [ -r ./shared ] ; then . ./shared ; else . /etc/pcmcia/shared ; fi

# Get device attributes
get_info $DEVICE
HWADDR=`/sbin/ifconfig $DEVICE | sed -n -e 's/.*addr \([^ ]*\) */\1/p'`

# Load site-specific settings
ADDRESS="$SCHEME,$SOCKET,$INSTANCE,$HWADDR"
start_fn () { return; }
stop_fn () { return; }

# Find the path where wireless tools are installed
for IWPATH in /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin /sbin ; do
    if [ -x $IWPATH/iwconfig ] ; then break ; fi
done
    
case "$ACTION" in
'resume')
    # Sandisk card hangs on resume, so reset it
    # fork in to new shell to avoid deadlock
    ( sleep 1 ; cardctl eject $SOCKET ; cardctl insert $SOCKET ) &
    exit 0
    ;;
'start'|'reset')

#    cardctl resume
#    $IWPATH/iwpriv $DEVICE reset 1

$IWPATH/iwconfig $DEVICE | grep -q "Power Management"
if [ "$?" = "1" ] ; then
    # load firmware to the card
    echo "Loading firmware..."
    FW1=/etc/hotplug/firmware/pm010102.hex
    FW2=/etc/hotplug/firmware/rf010804.hex
    prism2_srec -gs $DEVICE $FW1
    prism2_srec -gp $DEVICE $FW1
    $IWPATH/iwpriv $DEVICE reset 1
    prism2_srec -rp $DEVICE $FW2
    $IWPATH/iwconfig $DEVICE mode 2

    #cardctl scheme CardResumed
fi

    ;;
esac

export WIRELESS_USE=$0
./network $*
exit $?

