118 lines
2.4 KiB
Bash
Executable file
118 lines
2.4 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# tux-pack — pseudo-OOP Bash archiver
|
|
# -----------------------------------
|
|
# Usage:
|
|
# tux-pack <file|dir> <tar.gz|tar.xz|zip>
|
|
# tux-pack -e <archive>
|
|
#
|
|
# Examples:
|
|
# tux-pack myfolder tar.gz
|
|
# tux-pack -e myarchive.tar.xz
|
|
#
|
|
|
|
set -euo pipefail
|
|
|
|
declare -A TuxPack=(
|
|
[input]=""
|
|
[format]=""
|
|
[output]=""
|
|
)
|
|
|
|
TuxPack.new() {
|
|
local input="$1"
|
|
local format="${2:-}"
|
|
TuxPack[input]="$input"
|
|
TuxPack[format]="$format"
|
|
}
|
|
|
|
TuxPack.validate_input() {
|
|
[[ -e "${TuxPack[input]}" ]] || {
|
|
echo "Error: Input not found: ${TuxPack[input]}" >&2
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
TuxPack.pack() {
|
|
local input="${TuxPack[input]}"
|
|
local format="${TuxPack[format]}"
|
|
local base name
|
|
|
|
base="$(basename "$input")"
|
|
name="${base%/}"
|
|
|
|
case "$format" in
|
|
tar.gz)
|
|
TuxPack[output]="${name}.tar.gz"
|
|
echo "Creating ${TuxPack[output]}..."
|
|
tar -czf "${TuxPack[output]}" "$input"
|
|
;;
|
|
tar.xz)
|
|
TuxPack[output]="${name}.tar.xz"
|
|
echo "Creating ${TuxPack[output]}..."
|
|
tar -cJf "${TuxPack[output]}" "$input"
|
|
;;
|
|
zip)
|
|
TuxPack[output]="${name}.zip"
|
|
echo "Creating ${TuxPack[output]}..."
|
|
zip -qr "${TuxPack[output]}" "$input"
|
|
;;
|
|
*)
|
|
echo "Error: Unsupported format '$format'" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
echo "Done: ${TuxPack[output]}"
|
|
}
|
|
|
|
TuxPack.extract() {
|
|
local archive="$1"
|
|
[[ -f "$archive" ]] || {
|
|
echo "Error: File not found: $archive" >&2
|
|
exit 1
|
|
}
|
|
|
|
case "$archive" in
|
|
*.tar.gz)
|
|
echo "Extracting $archive..."
|
|
tar -xzf "$archive"
|
|
;;
|
|
*.tar.xz)
|
|
echo "Extracting $archive..."
|
|
tar -xJf "$archive"
|
|
;;
|
|
*.zip)
|
|
echo "Extracting $archive..."
|
|
unzip -q "$archive"
|
|
;;
|
|
*)
|
|
echo "Error: Unsupported archive format." >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
echo "Extraction complete."
|
|
}
|
|
|
|
usage() {
|
|
echo "Usage:"
|
|
echo " tux-pack <file|dir> <tar.gz|tar.xz|zip>"
|
|
echo " tux-pack -e <archive>"
|
|
exit 1
|
|
}
|
|
|
|
# -------------------------
|
|
# Runtime logic
|
|
# -------------------------
|
|
if [[ $# -lt 2 ]]; then
|
|
usage
|
|
fi
|
|
|
|
if [[ "$1" == "-e" ]]; then
|
|
TuxPack.extract "$2"
|
|
else
|
|
TuxPack.new "$1" "$2"
|
|
TuxPack.validate_input
|
|
TuxPack.pack
|
|
fi
|
|
|