summaryrefslogtreecommitdiff
path: root/archive/bin/loadcron
blob: a8ee614b56e9b91852a3a2da5eee8c9017d2eaf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/sh

# Load a crontab from ~/.config/cron.  Replace "$HOME" with actual
# homedir because cron can't deal with $HOME in crontabs

# This script was originally written by Joey Hess:
# http://git.kitenet.net/?p=joey/home.git;a=blob;f=bin/loadcron

set -e

WARNING="# Automatically generated by loadcron; edit ~/.config/cron/ files instead."

if [ ! -z "$(crontab -l 2>/dev/null)" ] && ! crontab -l | grep -q "$WARNING"; then
    if [ "$1" != "-f" ]; then
	echo "loadcron: Current crontab was not generated by loadcron; not changing." >&2
	echo "loadcron: Use loadcron -f to override"
	exit 1
    else
	crontab -l > $HOME/tmp/oldcrontab
	echo "loadcron: Old crontab is backed up to $HOME/tmp/oldcrontab"
    fi
fi

if [ -z "$(crontab -l 2>/dev/null)" ] && [ "$1" != "-f" ]; then
    echo >&2 "$(basename $0): no crontab; not loading without -f"
    exit 1
fi

tab=$HOME/.config/cron/$(whoami)_$(hostname -f)
if [ -e "$tab" ]; then
    (
	echo "$WARNING"
        echo
	echo "# From $tab:"
	sed "s!\$HOME!$HOME!g" < "$tab"
        # anacron: disabled because I don't have any useful anacron
        # jobs on any hosts at present
        # hostanacrontab=$HOME/.config/anacron/$(whoami)_$(hostname -f)
        # mkdir -p $HOME/local/anacron/spool
        # if [ -e "$hostanacrontab" ]; then
        #     echo "# Personal anacron:"
        #     echo "0 8 * * * anacron -s -t $hostanacrontab -S $HOME/local/anacron/spool"
        #     echo "@reboot   anacron -s -t $hostanacrontab -S $HOME/local/anacron/spool"
        #     echo
        # fi
    ) | crontab -
else
    echo "loadcron: $tab does not exist, not loading" >&2
fi

# units=$HOME/.config/host-user-units/`hostname -f`
# if [ -e "$units" ]; then
#     cat "$units" | while read unit; do
#         systemctl --user enable "$unit"
#     done
# fi