IfThenElse is a simple program used to glue small unix tools to getter with the ultimate goal to automate some tedious tasks.
The structure off an IfThenElse chain matches that off a flow chart: Trigger → Check → [Then] Action1 | [Else] Action2.
For example a chain could be:
Trigger: Every minute
Check: Is a Movie Playing
Then: Turn off the light
Else: Turn on the light
This should result the light turning off when starting the movie, and on when the movie is finished.
Each IfThenElse chain is an action in itself and can be chained up.
A chain is described in it own file using the KeyFile format. The parser used is the one provided with GLib, GKeyFile
The format used in the KeyFile is very similar to the used on windows, with the following differences:
.ini files use the ; character to begin comments, key files use the # character.
Key files do not allow for ungrouped keys meaning only comments can precede the first group.
Key files are always encoded in UTF-8.
Key and Group names are case-sensitive, for example a group called [GROUP] is a different group from [group].
.ini files don’t have a strongly typed boolean entry type, they only have GetProfileInt. In GKeyFile only true and false (in lower case) are allowed.
The above mentioned chain could be described with the following example:
[Trigger] type=TimerTrigger timeout=60 action=Check [Check] type=ExternalToolCheck cmd=check_movies.sh true_status=1 false_status=0 compare_old_state=true then_action=Then else_action=Else [Then] type=ExternalToolAction cmd=switch_off_lights.sh [Else] type=ExternalToolAction cmd=switch_on_lights.sh
As you can see in the previous example, IfThenElse ties external tools together. A IfThenElse chain can be seen as a flowchart. Extending the example
Each node in the chain can only have one input and one output connected to each port. So a trigger can only drive one next node, and visa versa.
If you want to use multiple triggers, or drive multiple actions you have to use one of the special nodes. The MultiCombine node combines the different inputs, the MultiAction drives multiple outputs.
An example that uses the MultiAction: Say that, in the previous example, we want to turn_off the lights and put gajim in offline mode:
[Trigger] type=TimerTrigger timeout=60 action=Check [Check] type=ExternalToolCheck cmd=check_movies.sh true_status=1 false_status=0 compare_old_state=true then_action=ThenMulti else_action=Else [ThenMulti] type=MultiAction action=Then1;Then2 [Then1] type=ExternalToolAction cmd=switch_off_lights.sh [Then2] type=ExternalToolAction cmd=gajim-remote change_status offline [Else] type=ExternalToolAction cmd=switch_on_lights.sh
This way, it is easy to make complex chains. Using the program
IfThenElse takes a keyfile describing the chain as input.
To run the program:
ifthenelse <list of input files>
If you want to background IfThenElse.
ifthenelse -b <list of intput files>
If you want to generate a flow chart from the chain:
ifthenelse -d output.dot <list of intput files>
The generated dot file can be converted into an actual chart by running do on it:
dot -Tpng -O output.dot
This will generate a output.dot.png
IfThenElse accepts both individual files as input as directories. If a directory is passed it will, recursively, scan that directory for .ife files.
If no input file is given, it will load the .ife files in the ~/.IfThenElse directory. Accepted Signals
IfThenElse accepts the following signals:
INT, HUP, TERMP: Exit the program. USR1: Reload the input files.
Error handling is currently not propperly handled in IfThenElse. In it current form it will exit when any error is encountered. Disable script
To disable a script add an empty category called disable. e.g.
More information on how to use it, and how to write IfThenElse scripts can be found on the API page: IfThenElse API
Currently IfThenElse is used for:
''Domotica'': If music is playing, either by MPD or pulseaudio, turn on the stereo, switch to right channel. Otherwise turn off stereo.
''Domotica'': Wakeup alarm, at 7:30 turn on lights, play music.
''Domotica'': Read RFID card X turn on lights, etc.
''Desktop'': Change desktop wallpaper every 5 minutes.
''Laptop'': If connected to work/home network, start dropbox. Otherwise stop it.
The program is licensed under the GPL-v2 license.
The source code can be found here.