ambevar-dotfiles/.scripts/extract

105 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
_usage () {
cat<<EOF
Usage: ${1##*/} [OPTIONS] FILES|FOLDERS
Wrapper around various archive extraction tools. The archives are recognized
according to their file extension. Folders will be searched recursively for
archives.
Extract password-protected 7z archives in one run. 7z archives are found in all
subfolders.
If password is provided, it will be used on all archives. Password will not show
up on screen.
WARNING: This script is rather weak, most notably regarding existing files.
Options.
-f: Remove archive if extraction was succesful.
-p: Ask for password.
EOF
}
OPT_DELAFTER=false
OPT_PASS=false
PASS=''
while getopts ":hfp" opt; do
case $opt in
h)
_usage "$0"
exit 1 ;;
f)
OPT_DELAFTER=true ;;
p)
OPT_PASS=true ;;
\?)
_usage "$0"
exit 1 ;;
esac
done
shift $((OPTIND - 1))
if [ $# -eq 0 ]; then
_usage "$0"
exit 1
fi
command -v atool >/dev/null 2>&1 && echo "You should use atool instead." && echo
if $OPT_PASS; then
echo -n "Password: "
stty_orig="$(stty -g)"
stty -echo
read PASS
stty "$stty_orig"
PASS="-p$PASS"
fi
_extract () {
echo "[$1]"
case "$1" in
*/*) FOLDER="${1%/*}";;
*) FOLDER="." ;;
esac
case "$1" in
*.tar*|*.tbz2|*.tbz|*.tgz|*.txz) tar xf "$1" -C "$FOLDER" ;;
*.7z) 7z x "$PASS" -o"$FOLDER" "$1";;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.xz) unxz "$1" ;;
*.exe) cabextract "$1" ;;
*)
case "$(file "$1")" in
*7-zip*) 7z x "$PASS" -o"$FOLDER" "$1" ;;
*gzip*|*bzip2*|*XZ*) tar xf "$1" -C "$FOLDER" ;;
*)
echo "[$1]: unrecognized archive format." ;;
esac
esac
$OPT_DELAFTER && rm -v "$1"
}
for i ; do
if [ -d "$i" ]; then
while IFS= read -r FILE; do
_extract "$FILE"
done <<EOF
$(find "$i" -type f)
EOF
else
_extract "$i"
fi
done