PDA

View Full Version : Bash script - Add device to xorg.conf



daller
June 5th, 2007, 03:20 PM
Hi there,

I'm trying to make a script to ease the setup of wizardpen tablets...

The guide is here:

https://help.ubuntu.com/community/TabletSetupWizardpen

I would like to have the script add the Device-section to xorg.conf (and running the script twice, shouldn't add 2 devices!!!)

This is also the case with adding udev-rules and scripts to the rc.local file...

What's the approach? - I can't seem to figure it out!

kidders
June 6th, 2007, 04:46 PM
Hi there,

I can think of two possible approaches...

- I -
Check for the presence of some of the lines you want to add. For instance...

grep -iq '^\s*driver\s*"wizardpen"' /etc/X11/xorg.conf
Just for simplicity's sake, that example looks for a Driver "wizardpen" declaration. The command returns 0 if a match is found, and 1 otherwise (eg it's been commented out).

- II -
Alternatively, you could create a file to "tag" a system that's already been tweaked, and refuse to repeat the modifications (unless a user executed your script with a '--force' option, perhaps). If you decided you were going to store files on a person's system (rather than doing something quick & clean with grep, etc.), you might want to go the whole hog, and store enough information to reverse any of the changes you make.

I'm not sure where would be an appropriate place to store such files, but you could do something like...

DIR=/var/cache/wizardmods
mkdir -p $DIR
touch $DIR/tag
cp /etc/X11/xorg.conf $DIR
...
...
md5sum /etc/X11/xorg.conf > $DIR/xorg.conf.md5


Your script would refuse to modify a system if /var/cache/wizardmods/tag existed, unless the user forced it.
It could undo changes that went pear-shaped or broke someone's system, using the backed up xorg.conf.
The script would refuse to uninstall automatically unless xorg.conf's MD5 sum matched the expected value (ie the file hadn't been modified since it was tweaked) ... again, unless it was '--forced'-ed.


Now that I've written them both down, one idea seems a whole lot more complicated than the other! Do you like either of them?

daller
June 6th, 2007, 11:08 PM
They are both nice solutions,

but here are 2 questions:

Is it possible to place the device-section next to the other devices, instead of just adding it in the bottom?

And if the script is ran again, I would like the first appended section to be deleted, and replaced with the new one...

How do I insert this line in the ServerLayout section: ?

InputDevice "WizardPen Tablet" "AlwaysCore"

I was thinking of using the same tools that applications use to edit xorg.conf... (I asume such tools exist! - I mean somehow the xorg.conf file is created, right?)

kidders
June 7th, 2007, 02:21 AM
Hey again,


I was thinking of using the same tools that applications use to edit xorg.conf.I'm not sure such tools exist. Unless they're awkwardly formatted (eg XML), config files are generally trivial to alter with standard text processing utilities (eg sed, awk, etc). For instance ...


How do I insert this line in the ServerLayout section: ?

InputDevice "WizardPen Tablet" "AlwaysCore"Something like the following should do this for you...

sed 's/\(\s*section\s*"ServerLayout"\)\s*$/\1\n\tInputDevice "WizardPen Tablet" "AlwaysCore"/i' /etc/X11/xorg.confAssuming my regular expression is general enough to work on other peoples' xorg.conf files (which is not a given at 2am hehe), that command should spit out a new one with the added line.


Is it possible to place the device-section next to the other devices, instead of just adding it in the bottom?

And if the script is ran again, I would like the first appended section to be deleted, and replaced with the new one.I doubt where device sections are added makes enough of a difference to justify the added hassle, but you can, if you really want to.

To replace a section (rather than blindly adding a new one), I would suggest...

- I -
Being smart, and trying to "intelligently" identify things that might be replaceable. For instance, you might decide that X servers equipped with the device you're configuring won't need a keyboard. Failing that, you could search for pre-existing WizardPen devices to supplant, and failing that, simply append the new device section. I'm not sure how straightforward this would be though.

- II -
Being dumb, and marking the device section your script adds like this...

### Automatically generated by daller - Do not remove this line!
### Changes you make to this device will not be preserved
Section "InputDevice"
Identifier "WizardPen Tablet"
...
...
EndSection
### Automatically generated by daller - Do not remove this line either!In this case, I think it might be smarter to be dumb! Identifying & stripping out bits of a file that you've specially marked would be much easier. You could then proceed to blindly append new device sections to the xorg.conf.

How best to do things like this is often about finding a balance between what's elegant and what's straightforward & reliable. It's important to leave the xorg.conf looking reasonably pretty, but above all, you have to be absolutely certain there is no way your script could break it.

UAResolved (http://ubuntuforums.org/showthread.php?t=377083)