-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmotd
executable file
·77 lines (67 loc) · 1.76 KB
/
motd
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
# motd -- display /etc/motd only when it is updated
. lib.bash || exit
usage() {
echo "Usage: $progname [-dq] [-o <cache-path>] [<motd-path>]"
echo ""
echo_opt "-d" "show diff if cached motd exists"
echo_opt "-o PATH" "use a different local cache path"
echo_opt "-q" "do not display modification time"
echo_opt "PATH" "use a different motd path"
echo ""
echo "'motd -dq' is suitable for login scripts."
}
mtime() { perl -e 'print ((stat $ARGV[0])[9])' "$1"; }
strftime() { perl -MPOSIX -e 'print strftime $ARGV[0], localtime $ARGV[1]' "$@"; }
when() { strftime "%F %H:%M" "$(mtime "$file")"; }
copy() { mkdir -p "${2%/*}" && cp "$1" "$2" && touch -r "$1" "$2"; }
file=/etc/motd
cached=${XDG_CACHE_HOME:-$HOME/.cache}/motd-$HOSTNAME
diff=false
quiet=false
while getopts :do:q OPT; do
case $OPT in
d) diff=true;;
o) cached=$OPTARG;;
q) quiet=true;;
*) lib:die_getopts;;
esac
done; shift $((OPTIND-1))
file=${1:-$file}
if [ ! -f "$cached" ]; then
# motd added (first run on this host)
if [ -s "$file" ]; then
cat "$file"
$quiet || vmsg "$file added (last update $(when))"
copy "$file" "$cached"
elif [ -f "$file" ]; then
$quiet || vmsg "$file is empty (last update $(when))"
copy "$file" "$cached"
else
$quiet || vmsg "$file is missing"
fi
elif [ ! -f "$file" ]; then
# motd removed
if $diff; then
diff -uN "$cached" "$file"
else
vmsg "$file is gone"
fi
rm "$cached"
elif ! cmp -s "$cached" "$file"; then
# motd updated
if $diff; then
diff -uN "$cached" "$file"
else
cat "$file"
fi
vmsg "$file updated (last update $(when))"
copy "$file" "$cached"
else
# no change
if ! $diff; then
$quiet || cat "$file"
fi
$quiet || vmsg "$file unchanged (last update $(when))"
copy "$file" "$cached" # in case local copy is newer
fi