#! /bin/sh
#----------------------------------------------------------------------------
# /sbin/hotplug - uevent helper script
#
# Creation:     2011-08-22 hbfl
# Last update:  $Id$
#
# Copyright (c) 2001-2012 the eisfair team, team(at)eisfair(dot)org
#
# 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.
#----------------------------------------------------------------------------
# Both $DEVPATH and $FIRMWARE are already provided in the environment.

HOTPLUG_FW_DIR='/lib/firmware'
HOTPLUG_DEBUG='false'
HOTPLUG_DEBUG_FILE='/var/log/hotplug'

if [ "$HOTPLUG_DEBUG" = "true" ]
then
    echo "`/bin/date +%Y-%m-%d` `/bin/date +%H:%M:%S` ARGUMENT=$1 FIRMWARE=$FIRMWARE ACTION=$ACTION SUBSYSTEM=$SUBSYSTEM PATH=/sys${DEVPATH}" >>$HOTPLUG_DEBUG_FILE
fi

if [ -n "$FIRMWARE" -a "$1" = "firmware" -a "$ACTION" = "add" ]
then
    if [ -f "$HOTPLUG_FW_DIR/$FIRMWARE" ]
    then
        if [ "$HOTPLUG_DEBUG" = "true" ]
        then
            echo "Loading firmware $HOTPLUG_FW_DIR/$FIRMWARE to /sys${DEVPATH}/data ..." >>$HOTPLUG_DEBUG_FILE
        fi

        echo "1" > /sys${DEVPATH}/loading
        #cat $HOTPLUG_FW_DIR/$FIRMWARE > /sys${DEVPATH}/data
        cp "$HOTPLUG_FW_DIR/$FIRMWARE" "/sys${DEVPATH}/data"
        sleep 1
        echo "0" > /sys${DEVPATH}/loading
    else
        echo "-1" > /sys${DEVPATH}/loading

        if [ "$HOTPLUG_DEBUG" = "true" ]
        then
            echo "Firmware $HOTPLUG_FW_DIR/$FIRMWARE not found ..." >>$HOTPLUG_DEBUG_FILE
        fi
    fi
fi