add note about license
[small-misc-bash] / ex
diff --git a/ex b/ex
index c99cd12abe83a35eb118fc0c622df16fb608f3f7..dd81d1ee616ccef6f00f40fe0a076da5fa7c2df1 100755 (executable)
--- a/ex
+++ b/ex
@@ -1,5 +1,12 @@
 #!/bin/bash
-# Copyright (C) 2014 Ian Kelling
+# I, Ian Kelling, follow the GNU license recommendations at
+# https://www.gnu.org/licenses/license-recommendations.en.html. They
+# recommend that small programs, < 300 lines, be licensed under the
+# Apache License 2.0. This file contains or is part of one or more small
+# programs. If a small program grows beyond 300 lines, I plan to switch
+# its license to GPL.
+
+# Copyright 2024 Ian Kelling
 
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+
 ex() {
-    local help="Usage: ex [--help] FILE...
-Extract many types of files based on their extensions
+  local help="Usage: ex [--help|-q] FILE...
+Extract many types of files
 
-7z bz2 deb gz iso dsc rar rpm tar xz zip sfs & some combinations.
+Based on their extensions,
+7z [tar.]bz2 deb [tar.]gz iso dsc rar rpm sfs tar xpi [tar.]xz zip Z.
 See source for exact file extensions.
-Note: apt-get install dtrx will do the same for
-most of these types, plus some more, I'm going
-to try it out sometime."
-    if [[ $1 == --help ]]; then
-        echo "$help"
-    fi
-    local x super
-    for x in "$@"; do
-        case "$x" in
-            *.tar.bz2 | *.tbz2 ) tar xjf "$x" ;;
-            *.tar.gz | *.tgz ) tar xzf "$x" ;;
-            *.bz2 ) bunzip2 "$x" ;;
-            *.gz ) gunzip  "$x" ;;
-            *.tar ) tar xf "$x" ;;
-            *.zip ) unzip "$x" ;;
-            *.Z ) uncompress "$x" ;;
-            *.7z ) 7za x "$x" ;;
-            *.deb ) ar x "$x" ;;
-            *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories ;;
-            *.tar.xz ) tar Jxf "$x" ;;
-            *.xz) xz -d "$x" ;;
-            *.iso )
-                local super
-                if [[ $EUID != 0 ]]; then
-                    super=sudo
-                fi
-                local temp_dir=$(mktemp -d)
-                $super losetup -f
-                $super mount -o loop "$x" "$temp_dir"
-                local dir="${x%%.iso}"
-                mkdir "$dir"
-                cp -a "$temp_dir"/* "$dir"
-                $super umount "$temp_dir"
-                ;;
-            *.r[0-9][0-9]|*.rar ) unrar x "$x" ;;
-            *.dsc) dpkg-source -x "$x" ;;
-            *.sfs) $user unsquashfs "$x" ;;
-            *)  echo "I don't kno how to extract $x";;
-        esac
-    done
+Note: dtrx (package & command) extracts most of these plus some others."
+
+  local quiet=false
+  case $1 in
+    --help)
+      echo "$help"
+      return 0
+      ;;
+    -q)
+      quiet=true
+      shift
+      ;;
+  esac
+  local x super restore_nullglob f cmd
+  for x in "$@"; do
+    case "$x" in
+      # tars first, so they are higher pri than non-tar extensions
+      *.tar )
+        $quiet || echo tar xf "$x"
+        tar xf "$x"
+        ;;
+      *.tar.bz2 | *.tbz2 )
+        $quiet || echo tar xjf "$x"
+        tar xjf "$x"
+        ;;
+      *.tar.gz | *.tgz )
+        $quiet || echo tar xzf "$x"
+        tar xzf "$x"
+        ;;
+      *.tar.xz )
+        $quiet || echo tar Jxf "$x"
+        tar Jxf "$x"
+        ;;
+      *.tar.zst )
+        $quiet || echo tar -I unzstd -xf "$x"
+        tar -I unzstd -xf "$x"
+        ;;
+      *.zst )
+        $quiet || echo unzstd "$x"
+        unzstd "$x"
+        ;;
+      *.xz)
+        $quiet || echo pixz -d "$x"
+        pixz -d "$x"
+        ;;
+      *.7z )
+        if type -p 7za &>/dev/null; then
+          cmd=7za
+        else
+          cmd=7zr
+        fi
+        $quiet || echo 7zr x "$x"
+        $cmd x "$x"
+        ;;
+      *.bz2 )
+        $quiet || echo bunzip2 "$x"
+        bunzip2 "$x"
+        ;;
+      *.deb )
+        $quiet || echo ar x "$x"
+        ar x "$x"
+        restore_nullglob=false
+        if ! shopt -q nullglob; then
+          restore_nullglob=true
+        fi
+        tmp=(data.tar.* control.tar.*)
+        if $restore_nullglob; then
+          shopt -u nullglob
+        fi
+        if (( ${#tmp[@]} != 2 )); then
+          echo "error: could not find data.tar.* or control.tar.*" >&2
+          return 1
+        fi
+        for f in ${tmp[@]}; do
+          case $f in
+            *.xz)
+              $quiet || echo tar xJf $f
+              tar xJf $f
+              rm -f $f
+              ;;
+            *.zst)
+              $quiet || echo tar -I unzstd -xf $f
+              tar -I unzstd -xf $f
+              rm -f $f
+              ;;
+            *.gz)
+              $quiet || echo tar xzf $f
+              tar xzf $f
+              rm -f $f
+              ;;
+            *)
+              echo "ex: ERROR: file i didnt expect: $f"
+              ;;
+          esac
+        done
+        ;;
+      *.dsc)
+        $quiet || echo dpkg-source -x "$x"
+        dpkg-source -x "$x"
+        ;;
+      *.gz )
+        $quiet || echo gunzip "$x"
+        gunzip "$x"
+        ;;
+      *.iso )
+        local super
+        if [[ $EUID != 0 ]]; then
+          super=sudo
+        fi
+        local temp_dir
+        temp_dir=$(mktemp -d)
+        $super losetup -f
+        $quiet || echo $super mount -o loop "$x" "$temp_dir"
+        $super mount -o loop "$x" "$temp_dir"
+        local dir="${x%%.iso}"
+        mkdir "$dir"
+        $quiet || cp -a "$temp_dir"/* "$dir"
+        cp -a "$temp_dir"/* "$dir"
+        $quiet || $super umount "$temp_dir"
+        $super umount "$temp_dir"
+        ;;
+      *.jar)
+        $quiet || echo jar xf "$x"
+        jar xf "$x"
+        ;;
+      *.r[0-9][0-9]|*.rar )
+        $quiet || echo unrar x "$x"
+        unrar x "$x"
+        ;;
+      *.rpm )
+        $quiet || echo "rpm2cpio $x | cpio --extract --make-directories"
+        rpm2cpio "$x" | cpio --extract --make-directories
+        ;;
+      *.sfs)
+        $quiet || echo $super unsquashfs "$x"
+        $super unsquashfs "$x"
+        ;;
+      *.Z )
+        $quiet || echo uncompress "$x"
+        uncompress "$x"
+        ;;
+      *.zip|*.xpi )
+        $quiet || echo unzip "$x"
+        unzip "$x"
+        ;;
+
+      *)  echo "I don't know how to extract $x";;
+    esac
+  done
 }
 ex "$@"