#!/bin/sh
#
# zme
# Copyright (C) 2000,2005  Dmitry V. Levin <ldv@altlinux.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

printmsg()
{
	printf %s\\n "${0##*/}: $*" >&2
}

sizeof_file()
{
	find "$1" -maxdepth 0 -path "$1" -printf '%s'
}

compress_file()
{
	local src dst size_src size_dst file_src file_dst

	file_src="$1" && shift
	src="$(readlink -ev "$file_src")" || return

	if [ -e "$FILE_TMP" -o -L "$FILE_TMP" ]; then
		printmsg "$FILE_TMP already exists, skipping"
		return 1
	fi

	file_dst="$FILE_TMP$SUFFIX"
	if [ -e "$file_dst" -o -L "$file_dst" ]; then
		printmsg "$file_dst already exists, skipping"
		return 1
	fi

	printf %s "$file_src -> $FILE_TMP "

	size_src="$(sizeof_file "$src")"
	if ! [ "$size_src" -ge 0 ] 2>/dev/null; then
		printmsg "$file_src: failed to determine file size"
		return 1
	fi

	if ! $UNARCHIVE -- "$file_src" >"$FILE_TMP"; then
		printmsg "$file_src: failed to uncompress"
		rm -f -- "$FILE_TMP"
		return 1
	fi

	printf %s "-> $file_dst "

	if ! $ARCHIVE -- "$FILE_TMP"; then
		printmsg "$FILE_TMP: failed to compress"
		rm -f -- "$FILE_TMP"
		return 1
	fi

	if ! dst="$(readlink -ev "$file_dst")"; then
		rm -f -- "$FILE_TMP"
		return 1
	fi

	touch -r "$file_src" -- "$file_dst"

	size_dst="$(sizeof_file "$dst")"
	if ! [ "$size_src" -ge 0 ] 2>/dev/null; then
		printmsg "$file_dst: failed to determine file size"
		rm -f -- "$file_dst"
		return 1
	fi

	if [ "$size_src" -le "$size_dst" ]; then
		if [ "$size_src" = "$size_dst" ]; then
			printf %s "has the same size as original ($size_src)"
		else
			printf %s "is bigger than original ($size_src -> $size_dst)"
		fi
		echo " SKIP"
		rm -f -- "$file_dst"
	else
		printf %s "($size_src -> $size_dst)"
		echo " OK"
		rm -f -- "$file_src"
	fi
}

compress_gzip()
{
	UNARCHIVE=bzcat
	ARCHIVE="gzip -9nf"
	SUFFIX=".gz"
	compress_file "$@" || rc=1
}

compress_bzip2()
{
	UNARCHIVE=zcat
	ARCHIVE="bzip2 -9f"
	SUFFIX=".bz2"
	compress_file "$@" || rc=1
}

rc=0
while [ -n "$1" ]; do
	case "$1" in
		*.tgz)
			FILE_TMP="${1%.tgz}.tar"
			compress_bzip2 "$1"
			;;
		*.tbz2)
			FILE_TMP="${1%.tbz2}.tar"
			compress_gzip "$1"
			;;
		*.gz)
			FILE_TMP="${1%.gz}"
			compress_bzip2 "$1"
			;;
		*.Z)
			FILE_TMP="${1%.Z}"
			compress_bzip2 "$1"
			;;
		*.bz2)
			FILE_TMP="${1%.bz2}"
			compress_gzip "$1"
			;;
		*)	printmsg "$1: unknown file suffix, ignored"
			rc=1
			;;
	esac
	shift
done

exit $rc
