college/Summer-2024/CS-3424/Assignments/Assignment-2/src/assign2.bash

117 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eEuo pipefail
SCRIPT_PATH="${BASH_SOURCE[0]}"
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
#######################################
# Print the given message to STDERR
#######################################
err() {
printf "%s\n" "${*}" >&2
}
#######################################
# End the script with a non-zero exit code and the given message
# Arguments:
# msg: The message to print to STDERR before exiting
#######################################
die() {
local msg="${*}"
err "$msg"
exit 1
}
#######################################
# Print a usage synopsis for the script to stdout
#######################################
usage() {
cat <<-__EOS__
Usage:
${SCRIPT_PATH} <input-files...>
Args:
<input-files...>: One or more paths to files on the system
Example:
${SCRIPT_PATH} ./some/file/to/handle.txt ./another/file/to/handle.txt
__EOS__
}
#######################################
# Replace all instances of <dstamp> or <date> with todays date in day/month/year
# format in place for the given file
# Arguments:
# file: The file to template in the date for
#######################################
replace_date_placeholders_in_file() {
local file="${1}"
local current_date
current_date="$(date +"%d\/%m\/%Y")"
sed -ri "s/(<dstamp>|<date>)/$current_date/g" "$file"
}
#######################################
# Redact the given file with the included sed script in place for the given file
# Arguments:
# file: The file to redact
#######################################
redact_file() {
local file="${1}"
sed -ri -f "${SCRIPT_DIR}/assign2.sed" "$file"
}
#######################################
# Check that the given file can be used within the script. For example, check
# that the current script/user has read & write permissions for the given file.
# Arguments:
# file: The file to check
#######################################
check_file() {
local file="${1}"
if ! [[ -r "$file" ]]; then
err "Unable to read '${file}', the current user lacks read permissions or the file does not exist!"
return 1
fi
if ! [[ -w "$file" ]]; then
err "Unable to modify '${file}', the current user lacks write permissions!"
return 1
fi
}
#######################################
# Check all given files and return a single error code if any given check fails.
# This is done so all errors for the given files are output to the user prior to
# exiting.
# Arguments:
# files: an array of files to loop through and validate
#######################################
validate_files() {
local ret_code=0
local files=("${@}")
for file in "${files[@]}"; do
if ! check_file "$file"; then
ret_code=1
fi
done
return "$ret_code"
}
main() {
local files=("${@}")
if (("${#files[@]}" == 0)); then
usage
die "No arguments provided!"
fi
validate_files "${files[@]}" || die "File checks failed, refusing to run script!"
for file in "${files[@]}"; do
redact_file "$file"
replace_date_placeholders_in_file "$file"
done
}
main "${@}"