2010-01-31 13:13:20 +01:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# xzize, Copyright © 2010 Matteo Cypriani
|
|
|
|
#
|
|
|
|
# This program is free software. It comes without any warranty, to
|
|
|
|
# the extent permitted by applicable law. You can redistribute it
|
|
|
|
# and/or modify it under the terms of the Do What The Fuck You Want
|
|
|
|
# To Public License, Version 2, as published by Sam Hocevar. See
|
|
|
|
# http://sam.zoy.org/wtfpl/COPYING for more details.
|
|
|
|
#
|
|
|
|
# This script recompresses to xz (with maximum compression level) a
|
|
|
|
# GZip (.gz), BZip2 (.bz2), LZMA (.lzma), and Lempel-Ziv (.Z)
|
|
|
|
# compressed file. The original file is keeped.
|
|
|
|
# If the file extension does not correspond to a known compression
|
|
|
|
# format, the file is compressed to xz. In that case, the original
|
|
|
|
# file is removed.
|
|
|
|
|
|
|
|
|
|
|
|
set -u
|
|
|
|
|
|
|
|
|
|
|
|
test_extension()
|
|
|
|
{
|
|
|
|
local - FILE EXTENSION BASE
|
|
|
|
FILE="$1"
|
|
|
|
EXTENSION="$2"
|
|
|
|
BASE="`basename "$FILE" ."$EXTENSION"`"
|
|
|
|
if [ "$BASE" != "$FILE" ] ; then
|
|
|
|
DEST="$BASE".xz
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
DEST="$FILE"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
do_recompress()
|
|
|
|
{
|
|
|
|
if [ -e "$DEST" ] ; then
|
|
|
|
echo "Skipping source file « $SOURCE »: destination file « $DEST » already exists!" >&2
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
echo "Recompressing « $SOURCE » to « $DEST »…"
|
2010-01-31 15:27:39 +01:00
|
|
|
$ACTION -c "$SOURCE" | xz -9 >"$DEST"
|
2010-01-31 13:13:20 +01:00
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
## Verify number of arguments ##
|
|
|
|
|
|
|
|
if [ $# -lt 1 ] ; then
|
2010-01-31 15:27:39 +01:00
|
|
|
echo "$0 requires at least one argument!" >&2
|
2010-01-31 13:13:20 +01:00
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
## Process files ##
|
|
|
|
|
|
|
|
for SOURCE in "$@" ; do
|
2010-01-31 15:27:39 +01:00
|
|
|
# Regular file?
|
|
|
|
if [ ! -f "$SOURCE" ] ; then
|
|
|
|
echo "Skipping source file « $SOURCE »: is not a regular file!" >&2
|
|
|
|
continue
|
|
|
|
fi
|
2010-01-31 13:13:20 +01:00
|
|
|
|
|
|
|
# GZip
|
|
|
|
test_extension "$SOURCE" gz
|
|
|
|
if [ "$DEST" != "$SOURCE" ] ; then
|
|
|
|
ACTION=gunzip
|
|
|
|
do_recompress
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# BZip2
|
|
|
|
test_extension "$SOURCE" bz2
|
|
|
|
if [ "$DEST" != "$SOURCE" ] ; then
|
|
|
|
ACTION=bunzip2
|
|
|
|
do_recompress
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# LZMA
|
|
|
|
test_extension "$SOURCE" lzma
|
|
|
|
if [ "$DEST" != "$SOURCE" ] ; then
|
|
|
|
ACTION=unlzma
|
|
|
|
do_recompress
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Compress
|
|
|
|
test_extension "$SOURCE" Z
|
|
|
|
if [ "$DEST" != "$SOURCE" ] ; then
|
|
|
|
ACTION=uncompress
|
|
|
|
do_recompress
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# XZ
|
|
|
|
if test_extension "$SOURCE" xz ; then
|
2010-01-31 15:27:39 +01:00
|
|
|
echo "Skipping source file « $DEST »: has already .xz suffix!" >&2
|
2010-01-31 13:13:20 +01:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Uncompressed file: fall back to simple compression
|
2010-01-31 15:27:39 +01:00
|
|
|
echo "Compressing « $SOURCE » to xz…"
|
2010-01-31 13:13:20 +01:00
|
|
|
xz -9 "$SOURCE"
|
|
|
|
|
|
|
|
done
|