~dricottone/my-utils

bec43bccba699cbbd287bdeee5a2bcdd4dd19c85 — Dominic Ricottone 4 days ago a5400e3
Adding documents scripts
M Makefile => Makefile +4 -0
@@ 14,6 14,7 @@ install:
	$(call subdir_make,archives,install)
	$(call subdir_make,containers,install)
	$(call subdir_make,display,install)
	$(call subdir_make,documents,install)
	$(call subdir_make,emulation,install)
	$(call subdir_make,games,install)
	$(call subdir_make,network,install)


@@ 24,6 25,7 @@ uninstall:
	$(call subdir_make,archives,uninstall)
	$(call subdir_make,containers,uninstall)
	$(call subdir_make,display,uninstall)
	$(call subdir_make,documents,uninstall)
	$(call subdir_make,emulation,uninstall)
	$(call subdir_make,games,uninstall)
	$(call subdir_make,network,uninstall)


@@ 34,6 36,7 @@ test: clean
	$(call subdir_make,archives,test)
	$(call subdir_make,containers,test)
	$(call subdir_make,display,test)
	$(call subdir_make,documents,test)
	$(call subdir_make,emulation,test)
	$(call subdir_make,games,test)
	$(call subdir_make,network,test)


@@ 44,6 47,7 @@ clean:
	$(call subdir_make,archives,clean)
	$(call subdir_make,containers,clean)
	$(call subdir_make,display,clean)
	$(call subdir_make,documents,clean)
	$(call subdir_make,emulation,clean)
	$(call subdir_make,games,clean)
	$(call subdir_make,network,clean)

M documents/Makefile => documents/Makefile +4 -0
@@ 6,13 6,17 @@ COMP_DIR?=/usr/local/etc/bash_completion.d

install:
	install -m755 hugo-bump $(BIN_DIR)/hugo-bump
	install -m755 hugo-extract-timestamp $(BIN_DIR)/hugo-extract-timestamp
	install -m755 hugo-date $(BIN_DIR)/hugo-date
	install -m755 hugo-post $(BIN_DIR)/hugo-post
	install -m755 hugo-timestamp $(BIN_DIR)/hugo-timestamp

uninstall:
	rm $(BIN_DIR)/hugo-bump
	rm $(BIN_DIR)/hugo-date
	rm $(BIN_DIR)/hugo-extract-timestamp
	rm $(BIN_DIR)/hugo-post
	rm $(BIN_DIR)/hugo-timestamp

test:
	bats test/

M documents/hugo-bump => documents/hugo-bump +1 -0
@@ 45,5 45,6 @@ for filename in "${positional[@]}"; do
  fi
done

# exit with accumulated error code
exit "$code"


A documents/hugo-extract-timestamp => documents/hugo-extract-timestamp +48 -0
@@ 0,0 1,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"


A documents/hugo-timestamp => documents/hugo-timestamp +75 -0
@@ 0,0 1,75 @@
#!/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