summaryrefslogtreecommitdiff
path: root/hotplug.sh.in
blob: 14b156078331b87e5071542768a5ab26b6b054b8 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash

INSTALL="@INSTALL@"
HOTPLUGPATH=/etc/hotplug
SCRIPTNAME=libmtp.sh
USERMAP=libmtp.usermap
UDEVPATH=/etc/udev/rules.d
UDEVRULES=libmtp.rules
HALBASEPATH=/usr/share/hal/fdi/information
HALPATH=/usr/share/hal/fdi/information/20thirdparty
HALRULES=libmtp.fdi

# 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 for udev first
if test -d ${UDEVPATH} ; then
    echo "You seem to have udev on your system. Installing udev rules..."
    ${INSTALL} ${UDEVRULES} ${UDEVPATH}
    echo "You may need additional setup to get correct permissions on your device."
    echo "See the INSTALL file for information."
    echo "Do you also want to install HAL support or the old hotplug support (y/n)?"
    read IN
    if [ "$IN" = "y" ] || [ "$IN" = "Y" ]; then
	echo "Continuing..."
    else
	exit 0
    fi
fi

# Check for HAL next
if test -d ${HALBASEPATH} ; then
    echo "You seem to have HAL on your system. Installing HAL rules..."
    mkdir -p ${HALPATH}
    ${INSTALL} ${HALRULES} ${HALPATH}
    echo "Do you also want to install the old hotplug support (y/n)?"
    read IN
    if [ "$IN" = "y" ] || [ "$IN" = "Y" ]; then
	echo "Continuing..."
    else
	exit 0
    fi
fi


# Check prerequisites
COMMAND=`which grep 2> /dev/null`
if [ "x${COMMAND}" = "x" ];
then
    echo "Missing grep program. Fatal error."
    exit 1
fi
COMMAND=`which awk 2> /dev/null`
if [ "x${COMMAND}" = "x" ];
then
    echo "Missing awk program. Fatal error."
    exit 1
fi
if [ "x${USER}" != "xroot" ];
then
    echo "WARNING: this program should be run as root!"
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} libmtp.sh ${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.
echo "Checking hotplugging CVS version..."
echo "/etc/hotplug/usb/*.usermap support was added in august 2002"
EDITMAP="yes"
CVSTAG=`grep '\$Id:' /etc/hotplug/usb.agent`
if [ "x${CVSTAG}" != "x" ]; then
    DATE=`echo ${CVSTAG} | awk '{ print $5 }'`
    echo "Hotplug version seems to be from ${DATE}"
    YEAR=`echo ${DATE} | awk 'BEGIN { FS="/"} {print $1; }'`
    MONTH=`echo ${DATE} | awk 'BEGIN { FS="/"} {print $2; }'`
    DAY=`echo ${DATE} | awk 'BEGIN { FS="/"} {print $3; }'`
    if [ "${YEAR}" -gt "2002" ]; then
	EDITMAP="no"
    else
	if [ "${YEAR}" -eq "2002" ]; then
	    if [ "${MONTH}" -ge "08" ]; then
		EDITMAP="no"
	    fi
	fi
    fi
fi
if [ "x${EDITMAP}" == "xyes" ]; then
    echo "We need to edit the ${HOTPLUGPATH}/usb.usermap if it exists..."
    if test -f ${HOTPLUGPATH}/usb.usermap
    then
	echo "We have a usb.usermap..."
	patchusermap ${USERMAP} /etc/hotplug/usb.usermap
    fi
fi

echo "Hotplugging successfully installed."

exit 0