diff --git a/profile.d/compress.sh b/profile.d/compress.sh index 7364926..2756733 100644 --- a/profile.d/compress.sh +++ b/profile.d/compress.sh @@ -39,8 +39,47 @@ # ------------------------------------------------------------------------------ utaz() { + _ununzip() + { + unzip -o "$1" -d "$2" >/dev/null 2>&1 + } + + _untar() + { + tar -xf "$1" -C "$2" + } + + _ungzip() + { + tar -xzf "$1" -C "$2" + } + + _unbzip2() + { + tar -xjf "$1" -C "$2" + } + + _unxz() + { + tar -xJf "$1" -C "$2" + } + + _unlzop() + { + lzop -d "$1" -o "$2/$(basename "${1%.*}")" + } + + _unlzip() + { + if command -v plzip >/dev/null 2>&1; then + plzip -d -c "$1" > "$2/$(basename "${1%.*}")" + else + lzip -d -c "$1" > "$2/$(basename "${1%.*}")" + fi + } + for opt in $@; do - case $opt in + case ${opt} in "-h" | "--help") echo "utaz: uncompress all the given files and/or the ones found in the given" echo " directories creating an host directory where needed." @@ -76,62 +115,92 @@ utaz() *) # The ${opt%/} writing is to remove trailing / if any - local LIST="$LIST ${opt%/}" + local LIST="${LIST} ${opt%/}" ;; esac done - [[ $createdir && $nodir ]] && \ + [[ -n ${createdir} && -n ${nodir} ]] && \ disp E "The --create-dir and --no-dir options are mutually exclusive." - [[ ! $LIST ]] && local LIST="." + [[ -z ${LIST} ]] && local LIST="." + for zitem in ${LIST}; do + for f in "${zitem}"/*; do + local dir="${f%.*}" + local extractor="" + case "$f" in + *.zip) + extractor="_ununzip" + ;; + *.tar.gz|*.tgz) + extractor="_ungzip" + ;; + *.tar.bz2) + extractor="_unbzip2" + ;; + *.tar.xz) + extractor="_unxz" + ;; + *.tar.lz) + extractor="_unlzop" + ;; + *.tar) + extractor="_untar" + ;; + *) + disp I "File ${f} is not a supported archive, skipping." + continue + ;; # Skip non-archive files + esac - for zitem in $LIST; do - shopt -s nullglob - local zips=("$zitem"/*.zip) - shopt -u nullglob - [[ ${#zips[@]} -eq 0 ]] && \ - disp W "$zitem contains no supported archive file, skipping." && \ - continue + # Verify binary existence + local cmd=${extractor//_un/} + command -v ${cmd} >/dev/null 2>&1 || { + disp E "Binary ${cmd} necessary to extract ${f} is missing." + continue + } - for f in "$zitem"/*.zip; do - disp I "Processing archive $f... " - local dir=${f::-4} - - mkdir -p "$dir" + disp I "Processing archive ${f} with ${extractor}..." + mkdir -p "${dir}" [[ $? -gt 0 ]] && disp E "The filesystem can't create directories, exit!" && return 1 - unzip -o "$f" -d "$dir" >/dev/null 2>&1 + ${extractor} "${f}" "${dir}" case $? in 0) - [[ $willrm ]] && rm -f "$f" && disp I "File $zitem/$f deleted." + [[ -n ${willrm} ]] && + rm -f "${f}" && disp I "File ${zitem}/${f} deleted." ;; - 1) disp W "Compression program returned a warning: deletion canceled." ;; *) disp E "The zip file seems corrupted, failed." - rm -rf "$dir" >/dev/null 2>&1 + rm -rf "${dir}" >/dev/null 2>&1 continue ;; esac - if [[ $createdir ]]; then + if [[ -n ${createdir} ]]; then disp I "Archive extracted successfully in subdirectory." - elif [[ $nodir ]]; then - mv "./$dir/"* ./ && rmdir "$dir" + elif [[ -n ${nodir} ]]; then + mv "./${dir}/"* ./ && rmdir "${dir}" disp I "Archive extracted successfully, no subdirectory needed." else - subdirs=$(find $dir -maxdepth 1 | wc -l) - if [[ $subdirs -eq 2 ]]; then - mv ./$dir/* ./ && rmdir $dir + # Set nullglob to ensure the array is empty if no files match + shopt -s nullglob + local contents=( "${dir}"/* ) + + # Check if exactly one item exists and if that item is a directory + if [[ ${#contents[@]} -eq 1 ]] && [[ -d "${contents[0]}" ]]; then + # Single directory detected + mv "${contents[0]}"/* ./ && rmdir "${dir}" disp I "Archive extracted successfully, no subdirectory needed." else disp I "Archive extracted successfully in subdirectory." fi + shopt -u nullglob fi done done diff --git a/profile.d/filefct.sh b/profile.d/filefct.sh index c8c05e9..87df535 100644 --- a/profile.d/filefct.sh +++ b/profile.d/filefct.sh @@ -336,6 +336,7 @@ file_stats() } }' } +export -f file_stats # ------------------------------------------------------------------------------ diff --git a/profile.sh b/profile.sh index e90acca..9e64173 100644 --- a/profile.sh +++ b/profile.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Begin profile # ------------------------------------------------------------------------------ # Copyright (c) 2013-2022 Geoffray Levasseur