#!/bin/bash

# Set sensible path
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Get device and device type
DEVICE=$1
DEVICETYPE=`echo ${DEVICE} | sed "s/[0-9]*$//"`
if echo $DEVICE | grep -q ':' ; then
    ISALIAS=yes
else
    ISALIAS=no
fi
# We only do stuff for real devices, not aliases
[ "${ISALIAS}" = "yes" ] && exit 0
 
[ "${DEVICETYPE}" = "" ] && exit 1

# For Xircom CEM56 PCMCIA ethernet driver, we have to cycle up/down the device
# before it is configured in order for it to work correctly (don't ask why!)
# THIS SHOULD BEFORE ANY OTHER CONFIGURATION !!!
# Unfortunately, /var/lib/pcmcia/stab might not yet have been updated if
# we are called through hotplug, thus we cannot rely on it.
# We take care not to cycle up/down an interface which is already up.
if [ "${DEVICETYPE}" = "eth" ] && \
	! ip -o link show "${DEVICE}" | grep UP > /dev/null 2>&1 && \
	/sbin/cardctl ident | grep CEM56 > /dev/null 2>&1 ; then
    # CEM56 plugged and ethernet interface is down => cycle up/down device
    if ! (ip link set "${DEVICE}" up && ip link set "${DEVICE}" down) ; then
        echo "failed to cycle up/down Xircom ${DEVICE} for bug workaround" >&2
        exit 1
    fi
fi

# Done
exit 0
