#!/bin/bash
name="mktar"
version="1.4"
read -r -d '' help_message <<-EOF
Archive utility
Usage: mktar -n OUTFILE [OPTIONS] INFILES
Options:
--checksum=ALGO create checksum with [none|sha1|sha256]
-d DIR, --from-directory DIR archive from a directory
-h, --help print this message
-n FILE, --name FILE name of archive
-p PASS, --passphrase PASS passphrase for encryption
-q, --quiet suppress error messages and prompts
-v, --verbose show additional messages
-V, --version print version number and exit
EOF
source /usr/local/lib/mylib.bash
source /usr/local/lib/archive.bash
archive_fn=""
checksum=-1
from_directory=""
positional=()
quiet=0
verbose=0
while [[ $# -gt 0 ]]; do
case $1 in
--checksum=no|--checksum=none|--checksum=0)
debug_msg "Setting checksum option to 0 (=none) (was ${checksum})"
checksum=0
shift
;;
--checksum=sha1|--checksum=1)
debug_msg "Setting checksum option to 1 (=sha1) (was ${checksum})"
checksum=1
shift
;;
--checksum=sha256|--checksum=2)
debug_msg "Setting checksum option to 2 (=sha256) (was ${checksum})"
checksum=2
shift
;;
--checksum=*)
attempted_checksum="$(/usr/bin/printf "%s\n" "$1" | sed -e 's/^.*=//' )"
error_msg "Unknown checksum '${attempted_checksum}'"
;;
-d|--from-directory)
debug_msg "Setting from_directory option to '${2}' (was ${from_directory})"
from_directory="--directory $2"
shift; shift
;;
-h|--help)
help_msg
shift
;;
-n|--name)
debug_msg "Setting output filename to ${2} (was ${archive_fn})"
archive_fn="$2"
shift; shift
;;
-p|--passphrase)
debug_msg "Setting passphrase to ${2} (was ${passphrase})"
passphrase="--passphrase $2"
shift; shift
;;
-q|--quiet)
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
;;
*)
debug_msg "Argument '${1}' added to positional array"
positional+=("$1")
shift
;;
esac
done
# error if no archive file given
if [[ -z "$archive_fn" ]]; then
error_msg "no archive file given"
usage_msg
fi
# determine archive action
archive_action="$(archive_extension "$archive_fn" | tee >(>&2 /usr/bin/head -n -1) | /usr/bin/tail -n 1)"
# determine checksum action and filename
if [[ "$checksum" -eq 1 ]]; then
checksum_action="sha1"
elif [[ "$checksum" -eq 2 ]]; then
checksum_action="sha256"
fi
checksum_fn="$(fn_basename "$archive_fn").${checksum_action}"
# check checksum files
if [[ "$checksum" -ge 1 ]]; then
if contains "$checksum_fn" "${positional[@]}"; then
error_msg "checksum '${checksum_fn}' cannot be an input file"
elif ! prompt_overwrite "checksum_fn"; then
error_msg "checksum '${checksum_fn}' cannot be overwritten"
fi
fi
# check passphrase
if [[ "$encrypt" -ge 1 && -z "$passphrase" ]]; then
passphrase=
fi
if ! archive --archive=$archive_action --name "$archive_fn" $passphrase $from_directory "${positional[@]}"; then
exit 1
fi
code=0
if [[ "$checksum" -ge 1 ]]; then
case "$checksum_action" in
sha1)
if ! /usr/bin/sha1sum "$archive_fn" | /usr/bin/awk '{print $1}' > "$checksum_fn"; then
code=1
fi
;;
sha256)
if ! /usr/bin/sha256sum "$archive_fn" | /usr/bin/awk '{print $1}' > "$checksum_fn"; then
code=1
fi
;;
esac
fi
exit "$code"