aboutsummaryrefslogtreecommitdiffstats
path: root/hyperterm/tools
diff options
context:
space:
mode:
Diffstat (limited to 'hyperterm/tools')
-rw-r--r--hyperterm/tools/rar2zip.sh94
1 files changed, 52 insertions, 42 deletions
diff --git a/hyperterm/tools/rar2zip.sh b/hyperterm/tools/rar2zip.sh
index 54ec926..d2ba15a 100644
--- a/hyperterm/tools/rar2zip.sh
+++ b/hyperterm/tools/rar2zip.sh
@@ -7,51 +7,50 @@
function check_and_install_7z() {
if command -v 7z &>/dev/null; then
- return
+ return 0
fi
- echo "7z not found. Attempting to install..."
-
- INSTALLER=""
- SUDO=""
- USER_CMD=$(command -v sudo || command -v doas)
+ install_package p7zip
+}
- [[ "$(id -u)" -ne 0 ]] && SUDO="$USER_CMD"
+function check_and_install_unar_unrar() {
+ local unar_missing=0
+ local unrar_missing=0
- OS_ID=$(
- cat /etc/*release 2>/dev/null |
- tr '[:upper:]' '[:lower:]' |
- grep "^id=" | head -n1 | cut -d= -f2 | tr -d '"'
- )
+ command -v unar &>/dev/null || unar_missing=1
+ command -v unrar &>/dev/null || unrar_missing=1
- case "$OS_ID" in
- arch|manjaro|artix|hyperbola)
- INSTALLER="pacman -Sy --noconfirm p7zip"
- ;;
- debian|ubuntu|linuxmint|elementary|pop)
- INSTALLER="apt-get update && apt-get install -y p7zip-full"
- ;;
- fedora)
- INSTALLER="dnf install -y p7zip p7zip-plugins"
- ;;
- void)
- INSTALLER="xbps-install -Sy p7zip"
+ case "$unar_missing$unrar_missing" in
+ 00)
+ # both installed
+ return 0
;;
- gentoo)
- INSTALLER="emerge app-arch/p7zip"
+ 10)
+ # unar installed, unrar missing
+ install_package unrar && return 0
;;
- alpine)
- INSTALLER="apk add p7zip"
+ 01)
+ # unrar installed, unar missing
+ install_package unar && return 0
;;
- *)
- echo "Unsupported distro. Cannot install 7z automatically."
+ 11)
+ # both missing, try unar first, then unrar
+ install_package unar && return 0
+ install_package unrar && return 0
+ echo "Error: could not install either unar or unrar."
return 1
;;
esac
+}
- if [ -n "$INSTALLER" ]; then
- echo "Installing 7z using: $SUDO $INSTALLER"
- $SUDO bash -c "$INSTALLER"
+function extract_rar() {
+ if command -v unar &>/dev/null; then
+ unar "$1" -o "$2"
+ elif command -v unrar &>/dev/null; then
+ unrar x -o+ "$1" "$2"
+ else
+ echo "Error: neither unar nor unrar available to extract $1"
+ return 1
fi
}
@@ -61,34 +60,45 @@ function rar2zip() {
exit 1
}
+ check_and_install_unar_unrar || {
+ echo "Failed to install unar or unrar. Aborting."
+ exit 1
+ }
+
echo "Converting RAR files to ZIP..."
- WORKDIR="/dev/shm/"
+ WORKDIR="/dev/shm"
for INFILE in "$@"; do
- OLDFILE=$(realpath "${INFILE}")
+ OLDFILE=$(realpath "$INFILE")
BASENAME=$(basename "${OLDFILE%.*}")
DIRNAME=$(dirname "$OLDFILE")
- NEWNAME="${DIRNAME}/$BASENAME.zip"
+ NEWNAME="${DIRNAME}/${BASENAME}.zip"
- if [ ! -e "${NEWNAME}" ]; then
+ if [ ! -e "$NEWNAME" ]; then
TEMPDIR=$(mktemp -p "$WORKDIR" -d)
echo "Extracting $OLDFILE"
- unar "$OLDFILE" -o "${TEMPDIR}/"
+ extract_rar "$OLDFILE" "$TEMPDIR/" || {
+ echo "Extraction failed for $OLDFILE"
+ rm -rf "$TEMPDIR"
+ continue
+ }
7z a -tzip -mx=9 "$NEWNAME" "${TEMPDIR}/*"
touch -r "$OLDFILE" "$NEWNAME"
- rm -r "$TEMPDIR"
+ rm -rf "$TEMPDIR"
- gio trash "$OLDFILE"
- echo "${OLDFILE}: Original file moved to trash"
+ gio trash "$OLDFILE" 2>/dev/null || rm -f "$OLDFILE"
+ echo "$OLDFILE: Original file moved to trash"
else
- echo "${NEWNAME}: File already exists!"
+ echo "$NEWNAME: File already exists!"
fi
done
echo "Conversion complete."
}
+
+rar2zip "$@"