4f1e045656d118911904106681862dd3facb2ae6
[uzbl-mobile] / examples / data / uzbl / scripts / session.sh
1 #!/bin/sh
2
3 # Very simple session manager for uzbl.  When called with "endsession" as the
4 # argument, it'll backup $sessionfile, look for fifos in $fifodir and
5 # instruct each of them to store their current url in $sessionfile and
6 # terminate themselves.  Run with "launch" as the argument and an instance of
7 # uzbl will be launched for each stored url.  "endinstance" is used internally
8 # and doesn't need to be called manually at any point.
9 # Add a line like 'bind quit = /path/to/session.sh endsession' to your config
10
11 [ -d ${XDG_DATA_HOME:-$HOME/.local/share}/uzbl ] || exit 1
12 scriptfile=$0                           # this script
13 sessionfile=${XDG_DATA_HOME:-$HOME/.local/share}/uzbl/session # the file in which the "session" (i.e. urls) are stored
14 configfile=${XDG_DATA_HOME:-$HOME/.local/share}/uzbl/config   # uzbl configuration file
15 UZBL="uzbl -c $configfile"           # add custom flags and whatever here.
16
17 fifodir=/tmp # remember to change this if you instructed uzbl to put its fifos elsewhere
18 thisfifo="$4"
19 act="$8"
20 url="$6"
21
22 if [ "$act." = "." ]; then
23    act="$1"
24 fi
25
26
27 case $act in
28   "launch" )
29     urls=`cat $sessionfile`
30     if [ "$urls." = "." ]; then
31       $UZBL
32     else
33       for url in $urls; do
34         $UZBL --uri "$url" &
35       done
36     fi
37     exit 0
38     ;;
39
40   "endinstance" )
41     if [ "$url" != "(null)" ]; then
42       echo "$url" >> $sessionfile;
43     fi
44     echo "exit" > "$thisfifo"
45     ;;
46
47   "endsession" )
48     mv "$sessionfile" "$sessionfile~"
49     for fifo in $fifodir/uzbl_fifo_*; do
50       if [ "$fifo" != "$thisfifo" ]; then
51         echo "spawn $scriptfile endinstance" > "$fifo"
52       fi
53     done
54     echo "spawn $scriptfile endinstance" > "$thisfifo"
55     ;;
56
57   * ) echo "session manager: bad action"
58       echo "Usage: $scriptfile [COMMAND] where commands are:"
59       echo " launch     - Restore a saved session or start a new one"
60       echo " endsession - Quit the running session. Must be called from uzbl"
61       ;;
62 esac
63