~ubuntu-branches/ubuntu/raring/lirc/raring

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#!/bin/sh
set -e

DebconfLoad ()
{
	#Read remote information
	#that was populated in debconf
	db_get lirc/remote
	REMOTE="$RET"
	db_get lirc/remote_driver
	REMOTE_DRIVER="$RET"
	db_get lirc/remote_modules
	REMOTE_MODULES="$RET"
	db_get lirc/remote_device
	REMOTE_DEVICE="$RET"
	db_get lirc/remote_lircd_conf
	REMOTE_LIRCD_CONF="$RET"

	#Read transmitter information
	#that was populated in debconf
	db_get lirc/transmitter
	TRANSMITTER="$RET"
	db_get lirc/transmitter_driver
	TRANSMITTER_DRIVER="$RET"
	db_get lirc/transmitter_modules
	TRANSMITTER_MODULES="$RET"
	db_get lirc/transmitter_device
	TRANSMITTER_DEVICE="$RET"
	db_get lirc/transmitter_lircd_conf
	TRANSMITTER_LIRCD_CONF="$RET"

	#Whether we will be starting lircd
	db_get lirc/start_lircd
	START_LIRCD="$RET"

	db_get lirc/lircmd_conf
	LIRCMD_CONF="$RET"

	#force noninteractive reconfiguration
	db_get lirc/reconfigure
	FORCE_NONINTERACTIVE_RECONFIGURATION="$RET"

	#Common kernel module configuration
	db_get lirc/port
	LIRC_PORT=$RET
	db_get lirc/irq
	LIRC_IRQ=$RET
	db_get lirc/cflags
	LIRC_CFLAGS=$RET

	#lirc_parallel
	db_get lirc/timer
	LIRC_PARALLEL_TIMER=$RET
}

check_hardware_conf ()
{
	#Make sure that all variables are there, and also create the file if
	#it is not there
	#local VAR
	for VAR in REMOTE REMOTE_MODULES REMOTE_DRIVER REMOTE_DEVICE REMOTE_SOCKET REMOTE_LIRCD_CONF REMOTE_LIRCD_ARGS TRANSMITTER TRANSMITTER_MODULES TRANSMITTER_DRIVER TRANSMITTER_DEVICE TRANSMITTER_SOCKET TRANSMITTER_LIRCD_CONF TRANSMITTER_LIRCD_ARGS START_LIRCD START_LIRCMD LOAD_MODULES LIRCMD_CONF FORCE_NONINTERACTIVE_RECONFIGURATION
	do
		# Patch provided by Robert Bihlmeyer <robbe@orcus.priv.at>
		# See http://bugs.debian.org/303078
		if ! grep -q "^[ 	]*$VAR\>" /etc/lirc/hardware.conf; then
			echo "$VAR=\"\"" >> /etc/lirc/hardware.conf
		fi
	done
}

Save ()
{
	check_hardware_conf

        if [ -f $1 ]; then
                cp $1 $1.old
        fi

	sed -i -e "
		s|^\(REMOTE\)=.*|\1=\"$REMOTE\"|;
		s|^\(REMOTE_DRIVER\)=.*|\1=\"$REMOTE_DRIVER\"|;
		s|^\(REMOTE_MODULES\)=.*|\1=\"$REMOTE_MODULES\"|;
		s|^\(REMOTE_DEVICE\)=.*|\1=\"$REMOTE_DEVICE\"|;
		s|^\(REMOTE_LIRCD_CONF\)=.*|\1=\"$REMOTE_LIRCD_CONF\"|;
		s|^\(TRANSMITTER\)=.*|\1=\"$TRANSMITTER\"|;
		s|^\(TRANSMITTER_DRIVER\)=.*|\1=\"$TRANSMITTER_DRIVER\"|;
		s|^\(TRANSMITTER_MODULES\)=.*|\1=\"$TRANSMITTER_MODULES\"|;
		s|^\(TRANSMITTER_DEVICE\)=.*|\1=\"$TRANSMITTER_DEVICE\"|;
		s|^\(TRANSMITTER_LIRCD_CONF\)=.*|\1=\"$TRANSMITTER_LIRCD_CONF\"|;
		s|^\(START_LIRCD\)=.*|\1=\"$START_LIRCD\"|;
		s|^\(LIRCMD_CONF\)=.*|\1=\"$LIRCMD_CONF\"|;
		s|^\(FORCE_NONINTERACTIVE_RECONFIGURATION\)=.*|\1=\"$FORCE_NONINTERACTIVE_RECONFIGURATION\"|;
		" $1
}

ChooseRemote()
{
	#Read our old remote configuration
	OLDREMOTE=`grep "^REMOTE=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`
	REMOTE_DEVICE=`grep "^REMOTE_DEVICE=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`
	REMOTE_DRIVER=`grep "^REMOTE_DRIVER=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`
	REMOTE_MODULES=`grep "^REMOTE_MODULES=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`
	REMOTE_LIRCD_CONF=`grep "^REMOTE_LIRCD_CONF=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`

	#read our old transmitter configuration
	OLDTRANSMITTER=`grep "^TRANSMITTER=" /etc/lirc/hardware.conf | sed s/\"//g| awk -F "=" '{print $2}'`
	TRANSMITTER_DEVICE=`grep "^TRANSMITTER_DEVICE=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`
	TRANSMITTER_DRIVER=`grep "^TRANSMITTER_DRIVER=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`
	TRANSMITTER_MODULES=`grep "^TRANSMITTER_MODULES=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`
	TRANSMITTER_LIRCD_CONF=`grep "^TRANSMITTER_LIRCD_CONF=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`

	START_LIRCD=`grep "^START_LIRCD=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`

	#Avoid corner cases that can prevent proper reconfiguration
	if [ -z "$OLDREMOTE" ]; then
		OLDREMOTE="None"
	fi
	if [ -z "$OLDTRANSMITTER" ]; then
		OLDTRANSMITTER="None"
	fi

	#force reconfiguration.  can be set by external applications
	#to true if they want to bypass the normal methods presented
	#to users and instead just parse the file.
	FORCE_NONINTERACTIVE_RECONFIGURATION=`grep "^FORCE_NONINTERACTIVE_RECONFIGURATION=" /etc/lirc/hardware.conf | sed s/\"//g | awk -F "=" '{print $2}'`

	#Present the user with some choices
		#To later explain what this is actually doing without man pages:
		#First, only grab lines with a ;
		#Now, remove all commas from streams
		#Now, Remove all remaining comments or ['s
		#Now, Print the remote name
		#Next, sort the list and remove duplicates
		#Now, Affix a comma and space to all starts of lines
		#Lastly, change the newline to a null character
	REMOTES=`cat /usr/share/lirc/lirc.hwdb /usr/share/lirc/extras/more_remotes.hwdb 2>/dev/null | grep ";" | sed 's/,//g' | awk -F ";" '$0!~/^#|^\[/{print $1}' | sort -u | sed 's/^/,\ /' | tr '\n' '\0'`
	db_subst lirc/remote REMOTES $REMOTES

	if [ -f /usr/share/lirc/extras/transmitter.hwdb ]; then
		TRANSMITTERS=`grep ";" /usr/share/lirc/extras/transmitter.hwdb | sort | sed 's/,//g' | awk -F ";" '$0!~/^#|^\[/{print $1}' | sed 's/^/,\ /' | tr '\n' '\0'`
		db_subst lirc/transmitter TRANSMITTERS $TRANSMITTERS
	fi

	#If we are forcing a reconfigure, then our old remote and new ones are the same
	if [ "$FORCE_NONINTERACTIVE_RECONFIGURATION" = "true" ]; then
		CHOSEN_REMOTE="${OLDREMOTE}"
		OLDREMOTE=""
		db_set lirc/remote $CHOSEN_REMOTE
	else
		db_input high lirc/remote || true
		if [ -f /usr/share/lirc/extras/transmitter.hwdb ]; then
			db_input high lirc/transmitter || true
		fi
		db_go

		#Populate remote information (remote is our primary device at all times)
		db_get lirc/remote
		CHOSEN_REMOTE="${RET}"
	fi

	if [ "$CHOSEN_REMOTE" != "None" -a "$CHOSEN_REMOTE" != "Custom" ] && [ "$OLDREMOTE" != "$CHOSEN_REMOTE" -o "$FORCE_NONINTERACTIVE_RECONFIGURATION" = "true" ]; then
		#To later explain what this is actually doing without man pages:
		#First, only grab lines with a ;
		#Now, remove all commas from streams
		#Lastly, take the last matching entry found (so remotes.hwdb overrides lirc.hwdb)
		REMOTE_LINE=`cat /usr/share/lirc/lirc.hwdb /usr/share/lirc/extras/more_remotes.hwdb 2>/dev/null | grep ";" | sed 's/,//g' | grep "${CHOSEN_REMOTE}" | tail -1`
		#Remove all remaining comments or ['s
		#then print the remote name
		REMOTE_MODULES=`echo $REMOTE_LINE | awk -F ";" '$0!~/^#|^\[/{print $3}'`
		REMOTE_DRIVER=`echo $REMOTE_LINE | awk -F ";" '$0!~/^#|^\[/{print $2}'`
		REMOTE_LIRCD_CONF=`echo $REMOTE_LINE | awk -F ";" '$0!~/^#|^\[/{print $5}'`
		SUPPORTED_DRIVERS=`lircd -H help 2>&1 | awk '$0!~/^S|^D/{print $1}' | tr '\n' '\ '`

		#/dev/input/eventX is a weird case: filter it
		if [ "$REMOTE_LIRCD_CONF" = "<A HREF=\"http://linux.bytesex.org/v4l2/faq.html#lircd\">generic config file for Linux input layer</A>" ]; then
			REMOTE_LIRCD_CONF=""
		fi

		#For atilibusb, we need to blacklist the kernel module
		if [ "$REMOTE_DRIVER" = "atilibusb" ]; then
			BLACKLIST="lirc_atiusb ati_remote"
		elif [ "$REMOTE_DRIVER" = "atiusb" ]; then
			BLACKLIST="ati_remote"
		else
			BLACKLIST=""
		fi

		#lirc.hwdb puts none rather than ;;
		if [ "$REMOTE_MODULES" != "none" ]; then
			#if we have a module in use, don't use a driver too
			REMOTE_DRIVER=""
		else
			REMOTE_MODULES=""
		fi

		#lirc.hwdb puts none rather than ;;
		if [ "$REMOTE_DRIVER" = "none" ]; then
			REMOTE_DRIVER=""
		fi

		#only need to put driver for non kernel module devices
		if ! echo "$SUPPORTED_DRIVERS" | grep "$REMOTE_DRIVER" > /dev/null; then
			REMOTE_DRIVER=""
		fi

		#reset our REMOTE_DEVICE whenever remotes
		#are changed.  Calculated later.
		REMOTE_DEVICE=""

		#Save any detected options
		db_fset lirc/reconfigure config_changed true

	elif [ "$OLDREMOTE" != "$CHOSEN_REMOTE" ] && [ "$CHOSEN_REMOTE" = "None" ]; then
		db_fset lirc/reconfigure config_changed true
		REMOTE_DEVICE=""
		REMOTE_MODULES=""
		REMOTE_DRIVER=""
		REMOTE_LIRCD_CONF=""
	fi

	#If we have a devinput driver, we need
	#to present the user with a way to figure out
	#what device is it
	#This should be ran even if they haven't changed remotes
	if [ "$REMOTE_DRIVER" = "devinput" ]; then
		#present question
		EVENTS=`find /dev/input/* -type d -exec find {} -not -type d -printf ", %p" \;`
		db_subst lirc/dev_input_device EVENTS $EVENTS
		db_input high lirc/dev_input_device || true
		db_go

		#get our answer
		db_get lirc/dev_input_device
		if [ "${RET}" != "${REMOTE_DEVICE}" ]; then
			REMOTE_DEVICE="${RET}"
			db_fset lirc/reconfigure config_changed true
		fi
	elif [ "$REMOTE_DRIVER" = "dvico" ];then
		#present question
		EVENTS=`ls /dev/usb | sed 's/^/,\ \/dev\/usb\//' | tr '\n' '\0'`
		db_subst lirc/dev_input_device EVENTS $EVENTS
		db_input high lirc/dev_input_device || true
		db_go

		#get our answer
		db_get lirc/dev_input_device
		if [ "${RET}" != "${REMOTE_DEVICE}" ]; then
			REMOTE_DEVICE="${RET}"
			db_fset lirc/reconfigure config_changed true
		fi
	#otherwise, set our old device in debconf again
	else
		db_set lirc/dev_input_device ""
	fi


	#If we are forcing a reconfigure, then our old remote and new ones are the same
	if [ "$FORCE_NONINTERACTIVE_RECONFIGURATION" = "true" ]; then
		CHOSEN_TRANSMITTER="${OLDTRANSMITTER}"
		OLDTRANSMITTER=""
		db_set lirc/transmitter $CHOSEN_TRANSMITTER
	else
	#populate transmitter information (transmitter is secondary device)
		db_get lirc/transmitter
		CHOSEN_TRANSMITTER="${RET}"
	fi

	if [ -f /usr/share/lirc/extras/transmitter.hwdb ] && [ "$CHOSEN_TRANSMITTER" != "None" -a "$CHOSEN_TRANSMITTER" != "Custom" ] && [ "$OLDTRANSMITTER" != "$CHOSEN_TRANSMITTER" -o "$FORCE_NONINTERACTIVE_RECONFIGURATION" = "true" ]; then
		TRANSMITTER_LINE=`grep ";" /usr/share/lirc/extras/transmitter.hwdb | sed 's/,//g' |  grep "${CHOSEN_TRANSMITTER}" | tail -1`
		TRANSMITTER_MODULES=`echo $TRANSMITTER_LINE | awk -F ";" '$0!~/^#|^\[/{print $3}'`
		TRANSMITTER_DRIVER=`echo $TRANSMITTER_LINE | awk -F ";" '$0!~/^#|^\[/{print $2}'`
		TRANSMITTER_LIRCD_CONF=`echo $TRANSMITTER_LINE | awk -F ";" '$0!~/^#|^\[/{print $5}'`
		#transmitter.hwdb puts none rather than ;;
		if [ "$TRANSMITTER_MODULES" != "none" ]; then
			#if we have a module in use, don't use a driver too
			TRANSMITTER_DRIVER=""
		else
			TRANSMITTER_MODULES=""
		fi

		#transmitter.hwdb puts none rather than ;;
		if [ "$TRANSMITTER_DRIVER" = "none" ]; then
			TRANSMITTER_DRIVER=""
		fi

		#only need to put driver for non kernel module devices
		if ! echo "$SUPPORTED_DRIVERS" | grep "$TRANSMITTER_DRIVER" > /dev/null; then
			TRANSMITTER_DRIVER=""
		fi

		db_fset lirc/reconfigure config_changed true

	elif [ "$OLDTRANSMITTER" != "$CHOSEN_TRANSMITTER" ] && [ "$CHOSEN_TRANSMITTER" = "None" ]; then
		db_fset lirc/reconfigure config_changed true
		TRANSMITTER_DEVICE=""
		TRANSMITTER_DRIVER=""
		TRANSMITTER_MODULES=""
		TRANSMITTER_LIRCD_CONF=""
	fi

	#Choose a serial port if we are using serial
	if [ "$REMOTE_MODULES" = "lirc_dev lirc_serial" ] || [ "$TRANSMITTER_MODULES" = "lirc_dev lirc_serial" ]; then
		db_input high lirc/serialport || true
		db_go
	#if we aren't using serial, then go back to default
	else
		db_set lirc/serialport "/dev/ttyS0"
	fi

	#Plan to have lircd enabled
	START_LIRCD=true

	#If we have a remote and a transmitter, figure out devices
	if [ "$CHOSEN_TRANSMITTER" != "None" ] && [ "$CHOSEN_REMOTE" != "None" ]; then
		#Make sure that at least one of these has changed before we make changes
		if [ "$OLDREMOTE" != "$CHOSEN_REMOTE" ] || [ "$OLDTRANSMITTER" != "$CHOSEN_TRANSMITTER" ]; then
			#if no remote device is defined already
			#and we do use a kernel module
			if [ -z "$REMOTE_DEVICE" ] && [ ! -z "$REMOTE_MODULES" ] ; then
				REMOTE_DEVICE="/dev/lirc0"
			fi

			#set up our transmitter.
			#if we use the same kernel modules, we use the same device
			#and we should disable transmitter stuff further
			if [ ! -z "TRANSMITTER_MODULES" -a "$TRANSMITTER_MODULES" = "$REMOTE_MODULES" ] || [ ! -z "$TRANSMITTER_DRIVER" -a "$TRANSMITTER_DRIVER" = "$REMOTE_DRIVER" ]; then
				TRANSMITTER_DEVICE=""
				TRANSMITTER_DRIVER=""
			#if they aren't the same driver/device, we needed a remote module and we need a transmitter kernel module
			elif [ "$REMOTE_DEVICE" = "/dev/lirc0" ] && [ ! -z "$TRANSMITTER_MODULES" ]; then
				TRANSMITTER_DEVICE="/dev/lirc1"
			#if they aren't the same  driver/device, but we didn't need a remote module, but need a transmitter kernel module
			elif [ ! -z "$TRANSMITTER_MODULES" ]; then
				TRANSMITTER_DEVICE="/dev/lirc0"
			fi
		fi
	#if we have just a transmitter, figure out device
	elif [ "$CHOSEN_TRANSMITTER" != "None" ] && [ "$CHOSEN_REMOTE" = "None" ] ; then
		#if we need kernel transmitter modules
		if [ ! -z "$TRANSMITTER_MODULES" ]; then
			TRANSMITTER_DEVICE="/dev/lirc0"
		fi
	#we have just chosen a remote
	elif [ "$CHOSEN_REMOTE" != "None" ] && [ "$CHOSEN_TRANSMITTER" = "None" ]; then
		#no remote device is defined already
		#and we need kernel remote modules
		if [ -z "$REMOTE_DEVICE" ] && [ ! -z "$REMOTE_MODULES" ] ; then
			REMOTE_DEVICE="/dev/lirc0"
		fi
	#if both remote and transmitter are set to none
	else
		#disable lircd
		START_LIRCD=false
	fi

	#Store the results of our device determination
	db_set lirc/remote_device "${REMOTE_DEVICE}"
	db_set lirc/remote_lircd_conf "$REMOTE_LIRCD_CONF"
	db_set lirc/blacklist "$BLACKLIST"
	db_set lirc/remote_modules "$REMOTE_MODULES"
	db_set lirc/remote_driver "$REMOTE_DRIVER"
	db_set lirc/transmitter_modules "$TRANSMITTER_MODULES"
	db_set lirc/transmitter_device "${TRANSMITTER_DEVICE}"
	db_set lirc/transmitter_driver "${TRANSMITTER_DRIVER}"
	db_set lirc/transmitter_lircd_conf "$TRANSMITTER_LIRCD_CONF"
	db_set lirc/start_lircd "${START_LIRCD}"
	db_set lirc/reconfigure "false"
	db_go
}

Setup_Blacklist()
{
	db_get lirc/blacklist
	BLACKLIST=$RET
	if [ ! -z "$BLACKLIST" ]; then
		cat > /etc/modprobe.d/lirc <<EOF
#File automatically generated upon lirc install
#Because a module had to be blacklisted. Don't
#Modify by hand, but rather via
#dpkg-reconfigure lirc
EOF
		for item in $BLACKLIST
		do
			cat >> /etc/modprobe.d/lirc-blacklist <<EOF
blacklist $item
EOF
			if egrep -q '^'"$item"'\ ' /proc/modules; then
				rmmod $item
			fi
		done
	else
		rm -f /etc/modprobe.d/lirc

	fi
}

Setup_Serial()
{
	#Move out old config
	if [ -f /etc/serial.conf ]; then
		mv /etc/serial.conf /etc/serial.conf.old
	fi

	#figure out if we have serial modules at all
	db_get lirc/remote_modules
	REMOTE_MODULES=$RET
	db_get lirc/transmitter_modules
	TRANSMITTER_MODULES=$RET
	if [ "$REMOTE_MODULES" = "lirc_dev lirc_serial" ] || [ "$TRANSMITTER_MODULES" = "lirc_dev lirc_serial" ]; then

		#if we chose ttyS0 or ttyS1, add it to modprobe.d
		db_get lirc/serialport
		SERIAL=$RET
		if [ "$SERIAL" = "/dev/ttyS0" ]; then
			cat > /etc/modprobe.d/lirc-serial.conf <<EOF
#COM1 equivalent, /dev/ttyS0
options lirc_serial irq=4 io=0x3f8
#COM2 equivalent, /dev/ttyS1
#options lirc_serial irq=3 io=0x2f8
EOF

		elif [ "$SERIAL" = "/dev/ttyS1" ]; then
			cat > /etc/modprobe.d/lirc-serial.conf <<EOF
#COM1 equivalent, /dev/ttyS0
#options lirc_serial irq=4 io=0x3f8
#COM2 equivalent, /dev/ttyS1
options lirc_serial irq=3 io=0x2f8
EOF
		fi
		cat > /etc/serial.conf << EOF
$SERIAL uart none
EOF
		#reload setserial to turn off serial port
		invoke-rc.d etc-setserial restart
	else
		rm /etc/modprobe.d/lirc-serial -f
	fi
}

setup_lircd_conf()
{
	#if we have an existing file, back it up
	if [ -f "/etc/lirc/lircd.conf" ]; then
		mv /etc/lirc/lircd.conf /etc/lirc/lircd.conf.dpkg-old
	fi

	#Add a header to the lircd.conf
	cat >> /etc/lirc/lircd.conf << EOF
#This configuration has been automatically generated via
#the Ubuntu LIRC package maintainer scripts.
#
#It includes the default configuration for the remote and/or
#transmitter that you have selected during package installation.
#
#Feel free to add any custom remotes to the configuration
#via additional include directives or below the existing
#Ubuntu include directives from your selected remote and/or
#transmitter.

EOF
	#if we have a remote, include it's config
	if [ -f "/usr/share/lirc/extras/more_remotes/$REMOTE_LIRCD_CONF" ];then
		cat >> /etc/lirc/lircd.conf <<EOF
#Configuration for the $REMOTE remote:
include "/usr/share/lirc/extras/more_remotes/$REMOTE_LIRCD_CONF"

EOF
	elif [ -f "/usr/share/lirc/remotes/$REMOTE_LIRCD_CONF" ];then
		cat >> /etc/lirc/lircd.conf <<EOF
#Configuration for the $REMOTE remote:
include "/usr/share/lirc/remotes/$REMOTE_LIRCD_CONF"

EOF
	elif [ -f "$REMOTE_LIRCD_CONF" ]; then
cat >> /etc/lirc/lircd.conf <<EOF
#Configuration for the $REMOTE remote:
include "$REMOTE_LIRCD_CONF"

EOF
	elif [ "$REMOTE" != "None" ]; then
		cat >> /etc/lirc/lircd.conf <<EOF
#No default remote configuration was included for $REMOTE
#You will need to include your own custom configuration for
#this remote, and file a bug at https://bugs.launchpad.net/ubuntu/+source/lirc/+filebug

EOF
	fi

	#if we have a transmitter, include it's config
	if [ -f "/usr/share/lirc/extras/transmitters/$TRANSMITTER_LIRCD_CONF" ];then
		cat >> /etc/lirc/lircd.conf <<EOF
#Configuration for the $TRANSMITTER transmitter:
include "/usr/share/lirc/extras/transmitters/$TRANSMITTER_LIRCD_CONF"
EOF
	elif [ -f "$TRANSMITTER_LIRCD_CONF" ]; then
cat >> /etc/lirc/lircd.conf <<EOF
#Configuration for the $TRANSMITTER transmitter:
include "$TRANSMITTER_LIRCD_CONF"

EOF
	elif [ "$TRANSMITTER" != "None" ]; then
		cat >> /etc/lirc/lircd.conf <<EOF
#No default remote configuration was included for $TRANSMITTER
#You will need to include your own custom configuration for
#this transmitter, and file a bug at https://bugs.launchpad.net/ubuntu/+source/lirc/+filebug

EOF
	fi
}

unload_modules()
{
    KERNEL=`uname -r`
    #unload all modules (but lirc_dev last)
    for item in `ls /lib/modules/$KERNEL/kernel/drivers/staging/media/lirc` lirc_dev
    do
        if egrep -q '^'"$item"'\ ' /proc/modules; then
            rmmod $item 2>/dev/null || true
        fi
    done
}

migrate_include()
{
	if [ -f $1 ]; then
		sed -i 's/\(^include\ \/.*$\)/\1\"/; s/^include\ \//include\ \"\//' $1
	fi
}

. /usr/share/debconf/confmodule

if [ "$1" = "configure" ]; then

	#0.8.6 got rid of mceusb2 so users need to repick remotes
	if dpkg --compare-versions "$2" le-nl "0.8.6-0ubuntu1"; then
		db_fset lirc/remote seen false
		db_fset lirc/transmitter seen false
	fi

	if [ -e "/etc/modprobe.d/lirc-serial" ]; then
			echo "Preserving user changes to /etc/modprobe.d/lirc-serial.conf ..."
			if [ -e "/etc/modprobe.d/lirc-serial.conf" ]; then
					mv -f "/etc/modprobe.d/lirc-serial.conf" "/etc/modprobe.d/lirc-serial.conf.dpkg-new"
			fi
			mv -f "/etc/modprobe.d/lirc-serial" "/etc/modprobe.d/lirc-serial.conf"
	fi


	#migrate syntax from older lirc version that might have included
	#an older variant of the include patch in intrepid and earlier
        if [ -n "$2" ] && dpkg --compare-versions "$2" lt "0.8.4a-0ubuntu2"; then
		migrate_include /etc/lirc/lircd.conf
        fi


	[ -x /sbin/udevd ] && invoke-rc.d udev reload >&2

	#unload any old modules before starting
	unload_modules

	#parse the lirc.hwdb to find our remote
	ChooseRemote

	#Prepare the blacklisting for necessary modules
	Setup_Blacklist

	#Setup serial module
	Setup_Serial

	db_fget lirc/reconfigure config_changed
	CONFIG_CHANGED=$RET
	db_fset lirc/reconfigure config_changed false

	if $CONFIG_CHANGED || [ -f /etc/lirc/hardware.conf ];then
		check_hardware_conf
		. /etc/lirc/hardware.conf
	fi

	if $CONFIG_CHANGED || [ "$DRIVER" = "UNCONFIGURED" ]; then
		DebconfLoad
		Save /etc/lirc/hardware.conf
	fi

	# If we don't have a valid lircd.conf try to get one
	if [ ! -f /etc/lirc/lircd.conf ] \
		|| grep -q "^#UNCONFIGURED" /etc/lirc/lircd.conf \
		|| $CONFIG_CHANGED ;then
		DebconfLoad
		setup_lircd_conf

	fi
	# If we don't have a valid lircmd.conf try to get one
	if [ ! -f /etc/lirc/lircmd.conf ] \
		|| grep -q "^#UNCONFIGURED" /etc/lirc/lircmd.conf ;then
		db_get lirc/lircmd_conf

		# if there is a misplaced config file it is ok to move it to
		# the right place
		if [ -f /etc/lircmd.conf ];then
			echo moving /etc/lircmd.conf to /etc/lirc/
			mv /etc/lircmd.conf /etc/lirc/

		# we don't restore the file unless the user is (re)configuring
		elif $CONFIG_CHANGED \
			&& [ -f "/usr/share/lirc/extras/more_remotes/$RET" ];then
			echo trying default lircmd.conf
			cp /usr/share/lirc/extras/more_remotes/$RET \
				/etc/lirc/lircmd.conf
		elif $CONFIG_CHANGED \
			&& [ -f "/usr/share/lirc/remotes/$RET" ];then
			echo trying default lircmd.conf
			cp /usr/share/lirc/remotes/$RET \
				/etc/lirc/lircmd.conf
		fi
	fi
	if [ -f /var/log/lircd ]
	then
		db_get lirc/remove_var-log-lircd
		if $RET
		then
			rm /var/log/lircd
		fi
	fi
fi


db_stop

#DEBHELPER#