File: hotplug.sh

package info (click to toggle)
gnomad2 2.5.0-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,384 kB
  • ctags: 650
  • sloc: ansic: 8,784; sh: 3,240; makefile: 112
file content (128 lines) | stat: -rwxr-xr-x 3,297 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh

INSTALL="/usr/bin/install -c"
HOTPLUGPATH=/etc/hotplug
SCRIPTNAME=nomadjukebox
USERMAP=nomad.usermap

# See if the parameter script ($2), device ($3) and productid ($4)
# are already defined in the usermap ($1)
function inmap {
    while read LINE; do
	if [ "x${LINE}" != "x" ]; then
	    firstword=`echo ${LINE} | awk '{ print $1 }'`
	    if [ ${firstword} != "#" ]; then
	        # This is a device line entry
		script=${firstword}
		manid=`echo ${LINE} | awk '{ print $3 }'`
		productid=`echo ${LINE} | awk '{ print $4 }'`
		# Skip blank products...
		if [ "x${script}" = "x$2" ]; then 
		    if [ "x${manid}" = "x$3" ]; then 
			if [ "x${productid}" = "x$4" ]; then
			    echo "yes"
			    return 0
			fi
		    fi
		fi
	    fi
	fi
    done < $1
    echo "no"
    return 0
}

# Scan the usermap $2 for all devices in $1 to see if they are already
# there, else patch the usermap.
function patchusermap {
    # Nullify comment
    comment=""
    while read LINE; do
	if [ "x$LINE" != "x" ]; then
	    firstword=`echo ${LINE} | awk '{ print $1 }'`
	    if [ ${firstword} = "#" ]; then
	    # This is a comment line, save it.
		comment=${LINE}
	    else
	        # This is a device line entry
		script=${firstword}
		manid=`echo ${LINE} | awk '{ print $3 }'`
		productid=`echo ${LINE} | awk '{ print $4 }'`
		# Skip blank products...
		if [ "x${manid}" != "x" ]; then
	            # See if this product is already in the usermap
		    echo "Checking for product ${productid} in $2..."
		    if [ `inmap $2 ${script} ${manid} ${productid}` = "no" ]; then
			echo "Not present, adding to hotplug map."
			echo ${comment} >> $2
			echo ${LINE} >> $2
			comment=""
		    else
			echo "Already installed."
		    fi
		fi
	    fi
	fi
    done < $1
}

# Check prerequisites
COMMAND=`which diff 2> /dev/null`
if [ "x${COMMAND}" = "x" ];
then
    echo "Missing diff program. Fatal error."
    exit 1
fi
COMMAND=`which patch 2> /dev/null`
if [ "x${COMMAND}" = "x" ];
then
    echo "Missing patch program. Fatal error."
    exit 1
fi
COMMAND=`which grep 2> /dev/null`
if [ "x${COMMAND}" = "x" ];
then
    echo "Missing grep program. Fatal error."
    exit 1
fi

# This script locates the hotplug distribution on a certain host
# and sets up userland hotplugging scripts according to rules. 
# The in-parameters are the hotplug directory and the name of a
# file of hotplug device entries and a script to be executed for 
# these deviced.

if test -d ${HOTPLUGPATH}
then
    echo "Hotplug in ${HOTPLUGPATH}"
else
    echo "Hotplug missing on this system. Cannot install."
    exit 1
fi

if test -d ${HOTPLUGPATH}/usb 
then
    echo "Has usb subdirectory."
else
    mkdir ${HOTPLUGPATH}/usb
fi

echo "Installing script."
${INSTALL} nomadjukebox ${HOTPLUGPATH}/usb
echo "Installing usermap."
${INSTALL} -m 644 ${USERMAP} ${HOTPLUGPATH}/usb
# If we find a usb.usermap file, and we see that this distribution
# of hotplug does not support private usermaps, then we need to
# patch the usb.usermap file.
#
# Create a merged file, diff the files to each other, and if there
# were mismatches, then patch the installed file.
if test -f ${HOTPLUGPATH}/usb.usermap
then
    echo "Has a usb.usermap"
    patchusermap ${USERMAP} /etc/hotplug/usb.usermap
fi
echo "Hotplugging successfully installed."

exit 0