You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.7 KiB
Bash

#!/bin/bash
optstring="o:h"
declare -i abort=0
# TODO: Consider a switch for per-file progress bars rather than a whole-operation one.
if [ ! -e "$(which pv)" ]; then
echo "Error: pv does not seem to be installed. Exiting."
exit 1
fi
function simplify_size_number {
local declare -i bytes=$1
local declare -i kilobytes=$((bytes/1024))
local size_str=""
if [ $kilobytes -lt 1024 ]; then
size_str="${kilobytes}kb"
else
local declare -i megabytes=$((kilobytes/1024))
if [ $megabytes -lt 1024 ]; then
size_str="${megabytes}mb"
else
local declare -i gigabytes=$((megabytes/1024))
size_str="${gigabytes}gb"
fi
fi
echo "$size_str"
}
while getopts ${optstring} args; do
case $args in
o) output_file=${OPTARG} ;;
:) echo "Output file required." ;;
# TODO: Expand this into something nicer looking.
h) echo "Usage: nyan -o [OUTPUT FILE] [INPUT FILE]..." && exit 0 ;;
esac
done
shift $((OPTIND-1))
if [ -z "$output_file" ]; then
echo "Error: no output file specified."
exit 1
fi
# TODO: Add overwrite switch.
if [ -e "$output_file" ]; then
echo "Error: Output file exists."
exit 1
fi
echo "Preparing to concatenate files."
declare -i total_size=0
for fname in "$@"; do
if [ ! -e "$fname" ]; then
abort=1
echo "${fname} does not exist."
elif [ -d "$fname" ]; then
abort=1
echo "${fname} is a directory."
else
current_file_size=$(stat -c%s "$fname")
total_size=$((total_size+current_file_size))
echo "Adding ${fname} at $(simplify_size_number $total_size)."
fi
done
echo "Concatenating files..."
if [ $abort -eq 0 ]; then
cat "$@" | pv -epts "$total_size" > "$output_file"
echo "Done!"
else
echo "Errors occurred, concatenation aborted."
fi