notes/scripts/xpress
2022-09-05 02:41:04 -07:00

141 lines
3.5 KiB
Bash
Executable file

#!/bin/bash
# Error handling
set -e
# For styling/colorizing output
txtbld=$(tput bold)
txtblue=${txtbld}$(tput setaf 4)
txtred=${txtbld}$(tput setaf 1)
txtgreen=${txtbld}$(tput setaf 2)
txtyellow=${txtbld}$(tput setaf 3)
txtwhite=${txtbld}$(tput setaf 7)
# Dependency check
function dependencycheck()
{
numdependencies="$#"
dependencies=("$@")
missingdependencies=0
for ((i = 0; i < numdependencies; i++)) ; do
if ! command -v "${dependencies[$i]}" &> /dev/null ; then
echo "${txtred}  dependency not met: ${dependencies[$i]}${txtwhite}"
missingdependencies=$((missingdependencies+1))
fi
done
if [ $missingdependencies -gt 0 ] ; then
exit 1
fi
}
# Main Dependency check
dependencycheck echo sed
# Intro Prompt
echo "${txtgreen}xpress: a simple shell script for compressing common file formats"
# Reset output style
tput bold & tput setaf 7
if [ "${1}" == "-tar" ] ; then
dependencycheck tar
echo "${txtblue}compressing .tar...${txtyellow}"
# main compression method
tar cf "${2}.tar" "${2}"
echo ".tar compression finished!${txtwhite}"
exit 0
fi
if [ "${1}" == "-tgz" ] ; then
dependencycheck tar gzip
echo "${txtblue}compressing .tgz...${txtyellow}"
# main compression method
tar -czf "${2}.tgz" "${2}"
echo ".tgz compression finished!${txtwhite}"
exit 0
fi
if [ "${1}" == "-gz" ] ; then
dependencycheck gzip
echo "${txtblue}compressing .gz...${txtyellow}"
# main compression method
gzip --keep "${2}"
echo ".gz compression finished!${txtwhite}"
exit 0
fi
if [ "${1}" == "-zip" ] ; then
dependencycheck zip
echo "${txtblue}compressing .zip...${txtyellow}"
# main compression method
zip "${2}.zip" "${2}"
echo ".zip compression finished!${txtwhite}"
exit 0
fi
if [[ "${1}" == "-bzip" || "${1}" == "-bzip2" || "${1}" == "-bz2" || "${1}" == "-bz" ]] ; then
dependencycheck bzip2
echo "${txtblue}compressing .bz2...${txtyellow}"
# main compression method
bzip2 -k "${2}"
echo ".bz2 compression finished!${txtwhite}"
exit 0
fi
if [[ "${1}" == "-lzip" || "${1}" == "-lz" ]] ; then
dependencycheck lzip
echo "${txtblue}compressing .lz...${txtyellow}"
# main compression method
lzip -c "${2}" > "${2}.lz"
echo ".lz compression finished!${txtwhite}"
exit 0
fi
if [[ "${1}" == "-rzip" || "${1}" == "-rz" ]] ; then
dependencycheck rzip
echo "${txtblue}compressing .rz...${txtyellow}"
# main compression method
rzip -k "${2}"
echo ".rz compression finished!${txtwhite}"
exit 0
fi
if [ "${1}" == "-xz" ] ; then
dependencycheck xz
echo "${txtblue}compressing .xz...${txtyellow}"
# main compression method
xz -k "${2}"
echo ".xz compression finished!${txtwhite}"
exit 0
fi
if [[ "${1}" == "-zstd" || "${1}" == "-zst" ]] ; then
dependencycheck zstd
echo "${txtblue}compressing .zst...${txtyellow}"
# main compression method
zstd -k "${2}"
echo ".zst compression finished!${txtwhite}"
exit 0
fi
if [[ "${1}" == "-ar" || "${1}" == "-a" ]] ; then
dependencycheck ar
echo "${txtblue}compressing .a...${txtyellow}"
# main compression method
ar -rs "${2}.a" "${2}"
echo ".a compression finished!${txtwhite}"
exit 0
fi
if [[ "${1}" == "-brotli" || "${1}" == "-br" ]] ; then
dependencycheck brotli
echo "${txtblue}compressing .br...${txtyellow}"
# main compression method
brotli "${2}"
echo ".br compression finished!${txtwhite}"
exit 0
fi