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
#!/bin/bash
name="hugo-timestamp"
version="1.0"
read -r -d '' help_message <<-EOF
Set a date/timestamp into a Hugo content files
Usage: hugo-timestamp [OPTIONS] TARGET DATETIME
Options:
-h, --help print this message and exit
-q, --quiet suppress error messages
-v, --verbose show additional messages
-V, --version print version number and exit
EOF
source /usr/local/lib/mylib.bash
. /usr/local/lib/myparse.bash
# error if no filenames given
if [[ ${#positional[@]} -lt 2 ]]; then
usage_msg
fi
_ed_append_date() {
ed -s "$1" <<EOF
/^title:/a
date: "$2"
.
w
q
EOF
}
_ed_overwrite_date() {
ed -s "$1" <<EOF
g/^date:/c
i
date: "$2"
.
w
q
EOF
}
filename="${positional[0]}"
newdate="${positional[1]}"
# if filename does not exist
if [[ ! -f "$filename" ]]; then
error_msg "No such file '$filename'"
# main routine
else
# try read timestamp from file
olddate="$(hugo-extract-timestamp "$filename")"
# branch 1: has no timestamp so append one
if [[ -z "$olddate" ]]; then
if [[ "$verbose" -eq 1 ]]; then
debug_msg "No date found, creating one"
fi
_ed_append_date "$filename" "$newdate"
# branch 2: has a timestamp so replace it
else
if [[ "$verbose" -eq 1 ]]; then
debug_msg "replacing ${olddate}"
fi
_ed_overwrite_date "$filename" "$newdate"
fi
fi