1#!/bin/sh
2
3# This is a shell script that starts and stops the recollindex daemon
4# depending on whether or not the power supply is plugged in.  It should be
5# called from the file ~/.config/autostart/recollindex.desktop.
6#
7# That is: make the script executable (chmod +x) and replace in
8# recollindex.desk the line:
9#   Exec=recollindex -w 60 -m
10# With
11#   Exec=/path/to/recoll_index_on_ac.sh
12#
13#
14# By: The Doctor (drwho at virtadpt dot net)
15# License: GPLv3
16#
17# Modifications by J.F Dockes
18#  - replaced "acpi" usage with "on_ac_power" which seems to be both
19#    more common and more universal.
20#  - Changed the default to be that we run recollindex if we can't determine
21#    power status (ie: on_ac_power not installed or not working: we're most
22#    probably not running on a laptop).
23
24INDEXER="recollindex -w 60 -m"
25ACPI=`which on_ac_power`
26
27# If the on_ac_power script isn't installed, warn, but run anyway. Maybe
28# this is not a laptop or not linux.
29if test "x$ACPI" = "x" ; then
30    echo "on_ac_power utility not found. Starting recollindex anyway."
31fi
32
33while true; do
34    # Determine whether or not the power supply is plugged in.
35    if test "x$ACPI" != "x" ; then
36        on_ac_power
37        STATUS=$?
38    else
39        STATUS=0
40    fi
41
42    # Get the PID of the indexing daemon.
43    if test -f ~/.recoll/index.pid ; then
44       PID=`cat ~/.recoll/index.pid`
45       # Make sure that this is recollindex running. pid could have
46       # been reallocated
47       ps ax | egrep "^[ \t]*$PID " | grep -q recollindex || PID=""
48    fi
49#    echo "Recollindex pid is $PID"
50
51    if test $STATUS -eq 1 ; then
52	# The power supply is not plugged in.  See if the indexing daemon is
53	# running, and if it is, kill it.  The indexing daemon will not be
54	# started.
55        if test x"$PID" != x; then
56	    kill $PID
57	fi
58    else
59	# The power supply is plugged in or we just don't know.
60        # See if the indexing daemon is running, and if it's not start it.
61        if test -z "$PID" ; then
62	    $INDEXER
63	fi
64    fi
65
66    # Go to sleep for a while.
67    sleep 120
68    continue
69done
70