~dricottone/my-utils

ref: bec43bccba699cbbd287bdeee5a2bcdd4dd19c85 my-utils/documents/hugo-extract-timestamp -rwxr-xr-x 1.1 KiB
bec43bccDominic Ricottone Adding documents scripts 4 days ago
                                                                                
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
#!/bin/bash

name="hugo-extract-timestamp"
version="1.0"
read -r -d '' help_message <<-EOF
	Extract a date/timestamp from a Hugo content files
	Usage: hugo-extract-timestamp [OPTIONS] [TARGETS ..]
	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

# if no filenames given
if [[ ${#positional[@]} -eq 0 ]]; then
  usage_msg
fi

code=0
newdate="$(hugo-date)"
for filename in "${positional[@]}"; do

  # if filename does not exist
  if [[ ! -f "$filename" ]]; then
    nonfatal_error_msg "No such file '$filename'"
    code=1

  # if filename does not contain timestamp
  elif ! /usr/bin/grep --quiet "$filename" --regexp="^date:"; then
    if [[ "$verbose" -eq 1 ]]; then
      debug_error_msg "No timestamp in '$filename'"
    fi
    /usr/bin/echo
    code=1

  # main routine
  else
    /usr/bin/grep "$filename" --regexp="^date:" | /usr/bin/sed --expression="s/^date: *//"
  fi
done

# exit with accumulated error code
exit "$code"