~dricottone/my-utils

1f09f68b7797319eddf938fd66de11d685e3c421 — Dominic Ricottone 2 years ago 330d007
Changes to rebom and library

Rebom now acts like cat, just including a BOM in front.

The library has been updated to dump usage, version, and help messages
regardless of the quiet option (-q, --quiet). Previously, the observed
behavior depended on the order of options. Dumped messages were
processed at the time they were seen, so they would ignore a quiet
option that followed. It is better to behave consistently than to obey
the user inconsistently.
3 files changed, 82 insertions(+), 29 deletions(-)

M core/README.md
M core/mylib.bash
M core/rebom
M core/README.md => core/README.md +1 -1
@@ 13,7 13,7 @@ gitstat         |Fetch updates on git repositories                             |
enumerate       |Rename files in current directory into sequential numbers     |`bash`
mkbak           |Create a backup of a target file                              |`bash`
rand            |Get a random number within an inclusive range                 |`bash`, `shuf`
rebom           |Add BOM to a target file
rebom           |Add BOM to a target file                                      |`bash`
rmold           |Delete old files
start-at        |After a pattern is matched, re-print                          |GNU or New (AT&T) `awk`
stop-at         |Re-print until a pattern is matched                           |GNU or New (AT&T) `awk`

M core/mylib.bash => core/mylib.bash +23 -5
@@ 70,12 70,10 @@ base_msg() {

# Internal API
# Normal -> <MESSAGE>
# Quiet  ->
# Note: ignore Verbose
# Quiet  -> <MESSAGE>
# Note: ignore Verbose AND Quiet
dump_msg() {
  if [[ "${quiet:=0}" -eq 0 ]]; then
    /usr/bin/printf "%s\n" "$1"
  fi
  /usr/bin/printf "%s\n" "$1"
}




@@ 346,6 344,26 @@ archive_extension() {
    ext="tar.bz2.gpg"
    ;;

  *.zip|*.cbz|*.docx|*.pptx|*.xlsx)
    debug_msg "Detected zip file"
    ext="zip"
    ;;

  *.rar)
    debug_msg "Detected rar file"
    ext="rar"
    ;;

  *.rpa)
    debug_msg "Detected rpa file"
    ext="rpa"
    ;;

  *.7z)
    debug_msg "Detected 7z file"
    ext="7z"
    ;;

  *)
    debug_msg "Could not parse filename"
    ;;

M core/rebom => core/rebom +58 -23
@@ 1,41 1,76 @@
#!/bin/sh
#!/bin/bash

name="rebom"
version="1.0"
help_message=$(/usr/bin/cat <<-EOF
version="1.1"
read -r -d '' help_message <<-EOF
	Add BOM to a target file
	Usage: rebom TARGET [OPTIONS]
	 -h, --help            print this message
	 -v, --version         print version number and exit
	 -v, --verbose         show additional messages
	 -V, --version         print version number and exit
EOF
)

. /usr/local/lib/myminiparse.sh
source /usr/local/lib/mylib.bash

positional=()
quiet=0
verbose=0
while [[ $# -gt 0 ]]; do
  case "$1" in

  -h|--help)
    help_msg
    shift
    ;;

# re-print first non-option argument, then exit
for arg; do
  case "$arg" in
  -q|--quiet)
    #ignore these
    debug_msg "Setting quiet option to 1 (was ${quiet})"
    quiet=1
    shift
    ;;

  -v|--verbose)
    debug_msg "Setting verbose option to 1 (was ${verbose})"
    verbose=1
    shift
    ;;

  -V|--version)
    version_msg
    shift
    ;;

  *)
    # main routine
    if [ ! -f "$arg" ]; then
      (>&2 /usr/bin/printf "%s: No such file '%s'\n" "$name" "$arg")
      exit 1
    else
      /usr/bin/printf "\xEF\xBB\xBF"
      /usr/bin/cat "$1"
      exit $?
    fi
    debug_msg "Argument '${1}' added to positional array"
    positional+=("$1")
    shift
    ;;
  esac
done

# if have not exited, no filename was specified
if [ "$quiet" -eq 0 ]; then
  (>&2 /usr/bin/printf "Usage: rebom TARGET [OPTIONS]\n")
# error if no filenames given
if [[ ${#positional[@]} -eq 0 ]]; then
  usage_msg
fi
exit 1

code=0
files=()
for filename in "${positional[@]}"; do
  if [ ! -f "$filename" ]; then
    nonfatal_error_msg "No such file '$filename'"
    code=1
  else
    files+=("$filename")
  fi
done

# error if no valid filenames given
if [[ ${#files[@]} -eq 0 ]]; then
  exit 1
fi

/usr/bin/printf "\xEF\xBB\xBF"
/usr/bin/cat ${files[@]}

exit "$code"