]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - crontab.d/update.sh
crontab.d/update.sh: Expand $HOME when changing $PATH in crontab files.
[config/dotfiles.git] / crontab.d / update.sh
index cfdcbcc5a3364562ac3b2b0555c06d2e94fbf3f2..05519bb4a14b0b901bc58f585954673e670de73a 100755 (executable)
@@ -5,7 +5,7 @@
 #
 # An existing crontab entry not generated with this script is not overwritten.
 
-# Copyright (C) 2012  Simon Ruderich
+# Copyright (C) 2012-2013  Simon Ruderich
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -26,7 +26,8 @@ set -e
 
 HEADER_WARNING="# WARNING! DO NOT EDIT THIS FILE! #"
 
-# Don't overwrite existing crontab entries.
+# Don't overwrite existing crontab entries. Not race condition free but that
+# can't be helped.
 if crontab -l >/dev/null 2>&1; then
     if crontab -l | head -n3 | grep "^$HEADER_WARNING$" >/dev/null; then
         :
@@ -42,6 +43,7 @@ if test ! -d "$DIRECTORY" || test ! -O "$DIRECTORY"; then
     exit 1
 fi
 
+# `set -e` aborts when `mktemp` fails.
 CRONTAB=`mktemp --tmpdir="$DIRECTORY" update-crontab.XXXXXXXXXXXX`
 
 echo "###################################"  > "$CRONTAB"
@@ -50,6 +52,9 @@ echo "###################################" >> "$CRONTAB"
 echo >> "$CRONTAB"
 echo "# It was generated from '$DIRECTORY/*' on `date -R`." >> "$CRONTAB"
 
+# Enforce C sort order ($LANGUAGE is a GNU extension).
+LC_ALL=C
+LANGUAGE=C
 
 NO_MATCHES=
 for file in "$DIRECTORY"/crontab.*; do
@@ -65,12 +70,15 @@ for file in "$DIRECTORY"/crontab.*; do
     echo             >> "$CRONTAB"
     echo "## $file:" >> "$CRONTAB"
 
+    # Strip licenses, multiple empty lines and fix $HOME variables in PATH
+    # (cron doesn't expand variables).
     START_REGEX='^# This [a-zA-Z]* is free software: you can redistribute it'
     END_REGEX='^# along with this [a-zA-Z]*.  If not, see <[^>]*>\.'
     < "$file" sed -e "/$START_REGEX/,/$END_REGEX/ d" \
                   -e "/^#[ ]*$/ d" \
                   -e "/^# Copyright (C) [0-9][0-9]* / d" \
         | cat --squeeze-blank \
+        | sed "/^PATH/ s:\$HOME:$HOME:g" \
         >> "$CRONTAB"
 done