#!/bin/sh # recompress as .tar.xz set -e p_err() { printf "ERR: %s\n" "$@" >&2 exit 1 } p_dbg() { exit $( e="$1" shift 1 2>/dev/null printf "\033[32m [*] %s\033[0m\n" "$@" >&2 echo "$e" ) } p_msg() { printf "\033[1m=>\033[0m %s\n" "$@" >&2 } p_warn() { printf "\033[1mWARN\033[0m %s\n" "$@" >&2 } p_name="${0##*/}" usage() { printf >&2 "%s\n" 'USAGE'\ " $p_name dir [opts...]"\ " $p_name -r DIR"\ 'where' ' dir tar-archive or folder to be repacked'\ ' opts options for xz'\ ' -r forget xz, (create raw tarfile)' exit 1 } raw=false while [ $# -gt 0 ]; do case "$1" in -r) raw=true; shift 1;; *) break;; esac; done f=$(basename "$1"); [ -n "${f:+s}" ]||usage l=$(dirname "$1") shift 1 p() {( p="$l/$f"; p="${p#./}"; echo "$p" )} opts="${@:--6}" trash() { if [ -e "$1" ]; then p_msg "reusing $1" return 1 else p_warn "new trash, $1" return 0 fi } t_ex() { f=$( case "$(p)" in *.t*bz2) O="-j" ;; *.t*gz) O="-z" ;; *.t*xz) O="-J" ;; *.tar) : ;; *) p_err "$(p) - archive not supported" ;; esac if [ 1 -lt "$(cd "$l"; tar $O -tf "$f" |grep -o "^[^/]*" |uniq |wc -l)" ]; then p="${f%.t*}" #p_dbg 2 "[$f : $p]" trash "$p" && (cd "$l"; mkdir "$p") (cd "$l/$p"; tar $O -xf "../$f") else p=$(cd "$l"; tar $O -tf "$f" |sed 'q') p="${p#./}" p="${p%/}" #p_dbg 2 "[$f : $p]" trash "$p" && (cd "$l"; tar $O -xf "$f") fi echo "$p" ) } t_pc() { #p_dbg 1 "[$l : $f : $(p)] >$(p).tar.xz" if ! $raw; then p_msg "cd $l; tar >$f.tar.xz" (cd "$l"; tar --group 0 --owner 0 -cf - "$f" |xz $opts >"$f.tar.xz") else p_msg "cd $l; tar >$f.tar" (cd "$l"; tar --group 0 --owner 0 -cf - "$f" >"$f.tar") fi } if [ -d "$(p)" ]; then t_pc elif [ -e "$(p)" ]; then t_ex t_pc else p_err "$(p) does not exist..." fi