#!/bin/bash

if [ $UID != 0 ]; then
	echo "Please run this script with sudo:"
	echo "sudo $0 $*"
	exit 1
fi

RULES_FILE=/etc/udev/rules.d/95-rutokenweb_hid.rules

if [ -f "$RULES_FILE" ]; then
	cp "$RULES_FILE" "$RULES_FILE".orig
	echo "Save a copy of the original 95-rutokenweb_hid.rules with the suffix .orig"
fi

echo "Copying 95-rutokenweb_hid.rules to /etc/udev/rules.d/..."
cat > "$RULES_FILE" <<EOM_____________20140404
SUBSYSTEM=="usb", GOTO="rutokenweb_hid_rules_end_check_subsystem"
SUBSYSTEM!="usbmisc" GOTO="rutokenweb_hid_rules_end"

LABEL="rutokenweb_hid_rules_end_check_subsystem"

ACTION!="add", GOTO="rutokenweb_hid_rules_end"
KERNEL!="hiddev*", GOTO="rutokenweb_hid_rules_end"

ATTRS{idVendor}=="0a89", ATTRS{idProduct}=="0040", MODE="0666"

LABEL="rutokenweb_hid_rules_end"
EOM_____________20140404

echo "Reloading udev rules..."
udevadm control --reload-rules

echo "Please, remove RutokenWeb and insert it back to begin working with"

