]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - shell/crontab.d/setup.sh
Use command grouping to reduce number of redirects
[config/dotfiles.git] / shell / crontab.d / setup.sh
1 #!/bin/sh
2
3 # Combine all crontab.* files in ~/.crontab.d/ into a single crontab file and
4 # load it with `crontab`.
5 #
6 # An existing crontab entry not generated with this script is not overwritten.
7
8 # Copyright (C) 2012-2014  Simon Ruderich
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23
24 set -eu
25
26
27 HEADER_WARNING='# WARNING! DO NOT EDIT THIS FILE! #'
28
29 # Don't overwrite existing crontab entries. Not race condition free but that
30 # can't be helped.
31 if crontab -l >/dev/null 2>&1; then
32     if crontab -l | head -n3 | grep -F -x "$HEADER_WARNING" >/dev/null; then
33         :
34     else
35         echo 'Existing crontab entry found, please remove it manually.'
36         exit 2
37     fi
38 fi
39
40
41 DIRECTORY="$HOME/.crontab.d"
42 if test ! -d "$DIRECTORY" || test ! -O "$DIRECTORY"; then
43     exit 1
44 fi
45
46 # `set -e` aborts when `mktemp` fails.
47 CRONTAB=`mktemp --tmpdir="$DIRECTORY" update-crontab.XXXXXXXXXXXX`
48
49 {
50     echo '###################################'
51     echo "$HEADER_WARNING"
52     echo '###################################'
53     echo
54     printf '# It was generated from "%s/*" on %s.' \
55         "$DIRECTORY" "`LANG=C date '+%a, %d %b %Y %H:%M:%S %z'`"
56 } > "$CRONTAB"
57
58 # Enforce C sort order.
59 LC_ALL=C
60
61 NO_MATCHES=
62 for file in "$DIRECTORY"/crontab.*; do
63     # No crontab.* files exist, abort.
64     if test ! -e "$file"; then
65         NO_MATCHES=y
66         break
67     fi
68
69     printf 'Found "%s".\n' "$file"
70
71     printf '\n\n## %s:\n' "$file" >>"$CRONTAB"
72
73     # Strip licenses, multiple empty lines and fix $HOME variables in PATH
74     # (cron doesn't expand variables).
75     START_REGEX='^# This [a-zA-Z]* is free software: you can redistribute it'
76     END_REGEX='^# along with this [a-zA-Z]*.  If not, see <[^>]*>\.'
77     <"$file" sed -e "/$START_REGEX/,/$END_REGEX/ d" \
78                  -e "/^#[ ]*$/ d" \
79                  -e "/^# Copyright (C) [0-9][0-9]* / d" \
80         | cat --squeeze-blank \
81         | sed "/^PATH/ s:\$HOME:$HOME:g" \
82         >>"$CRONTAB"
83 done
84
85 # Update crontab with the crontab.* files.
86 if test -z "$NO_MATCHES"; then
87     echo
88     echo 'Updating crontab.'
89     crontab "$CRONTAB"
90
91 # No files found, remove the existing crontab entry. Ignore errors in case no
92 # crontab entry existed in the first place.
93 else
94     echo 'No files found, removing old crontab (if it exists).'
95     crontab -r || true
96 fi
97
98 rm "$CRONTAB"