]> git.donarmstrong.com Git - bin.git/blob - getmail
add reset usb bus command
[bin.git] / getmail
1 #!/bin/sh
2
3 if [ -n "$VERBOSE" ]; then
4     set -x
5 fi;
6
7 cmd_exists () {
8     if which "$1" >/dev/null 2>&1; then
9         return 0;
10     else
11         return 1;
12     fi;
13 }
14
15 # Configure the two things below for your MUA and the host being run.
16 IMAPHOST=$( awk -F= '/remotehost/{print $2}' ~/.offlineimaprc | head -n 1 )
17 MUAS="neomutt mutt mutt-ng"
18
19 # Don't check mail if ~/.nocheck exists and we're not running
20 # interactively.
21 if [ -z "$CRON" ] && [ -e ~/.nocheck ]; then
22     exit 0;
23 fi;
24
25 if [ -z "$HOSTNAME" ]; then
26     HOSTNAME="$(hostname)"
27 fi;
28
29 IONICE=""
30 if cmd_exists "ionice"; then
31     IONICE="ionice -c 3"
32 fi;
33
34 # if we are running under cron and there is no running MUA, exit
35 if [ -n "$CRON" ]; then
36     RUNNING_MUA=0
37     for MUA in $MUAS;  do 
38         if kill -0 `pgrep -U $(id -u) -x $MUA` 2>/dev/null; then
39             RUNNING_MUA=1
40             break;
41         fi;
42     done;
43     if [ "x$RUNNING_MUA" = "x0" ]; then
44         exit 0;
45     fi;
46 fi;
47
48 # source the ssh info if it exists
49 if [ -e ~/.ssh/"ssh_agent_info_$HOSTNAME" ]; then
50         . ~/.ssh/"ssh_agent_info_$HOSTNAME" > /dev/null;
51 fi;
52
53 PID="$(pgrep -U $(id -u) offlineimap)";
54 if [ -n "$PID" ]; then
55         # if offlineimap has been running for more than 20 minutes
56         if [ $(( $(date +'%s') - $(stat -c '%Y' /proc/$PID/cmdline) )) -gt 1200 ]; then
57                 kill "$PID" >/dev/null 2>&1 || true;
58                 sleep 5
59                 kill -0 "$PID" >/dev/null 2>&1 && kill -9 "$PID" >/dev/null 2>&1;
60                 PID="$(pgrep -U $(id -u) offlineimap)";
61         fi;
62 fi;
63
64 if [ -z "$PID" ]; then
65         nice -n 19 ${IONICE} offlineimap -o -u basic 2>&1 |grep validity| \
66             perl -ne 'my ($account,$list) = /UID validity problem for folder ([\w-]+)\.([\w-]+)/;
67                   next unless defined $account and length $account;
68                   qx(/home/don/bin/deletemailbox -a Inbox $account.$list > /dev/null);';
69     if cmd_exists "notmuch"; then
70         nice -n 19 ${IONICE} notmuch new >/dev/null 2>&1;
71     fi;
72 fi;