#! /bin/sh
# Lzdiff - Diff/cmp wrapper for compressed files.
# Copyright (C) 2008, 2009 Antonio Diaz Diaz.
#
# This script is free software: you have unlimited permission
# to copy, distribute and modify it.

LC_ALL=C
export LC_ALL
invocation_name=$0
args=
default_ext=.lz
diff_prog=diff
file1=
file2=

# Loop over args
while [ x"$1" != x ] ; do

	case "$1" in
	--help | --he* | -h)
		echo "Lzdiff - Diff/cmp wrapper for compressed files."
		echo
		echo "Lzdiff is a wrapper script around the diff and cmp commands that allows"
		echo "transparent comparison of any combination of compressed and"
		echo "non-compressed files. If any given file is compressed, its uncompressed"
		echo "content is used. The supported compressors are gzip, bzip2 and lzip."
		echo
		echo "Usage: ${invocation_name} [OPTIONS] [DIFF_OPTIONS] FILE1 [FILE2]"
		echo
		echo "Compares FILE1 to FILE2. If FILE2 is omitted, compares FILE1 to the"
		echo "uncompressed contents of FILE1.[gz|bz2|lz] (depending on the default"
		echo "compressor selected). DIFF_OPTIONS are passed directly to diff or cmp."
		echo "The exit status from diff or cmp is preserved."
		echo
		echo "Options:"
		echo "  -h, --help          display this help and exit"
		echo "  -V, --version       output version information and exit"
		echo "      --gzip          use gzip as default decompressor"
		echo "      --bzip2         use bzip2 as default decompressor"
		echo "      --lzip          use lzip as default decompressor (default)"
		echo "      --diff          use diff to compare files (default)"
		echo "      --cmp           use cmp to compare files"
		echo
		echo "Report bugs to lzip-bug@nongnu.org"
		echo "Lzip home page: http://www.nongnu.org/lzip/lzip.html"
		exit 0 ;;
	--version | --ve* | -V)
		echo "Lzdiff 0.5"
		echo "Copyright (C) 2009 Antonio Diaz Diaz."
		echo "This script is free software: you have unlimited permission"
		echo "to copy, distribute and modify it."
		exit 0 ;;
	--gz*)
		default_ext=.gz ;;
	--bz*)
		default_ext=.bz2 ;;
	--lz*)
		default_ext=.lz ;;
	--diff)
		diff_prog=diff ;;
	--cmp)
		diff_prog=cmp ;;
	-)
		echo "${invocation_name}: reading from stdin not supported"
		exit 1 ;;
	--)
		;;
	-?*)
		args="${args} $1" ;;
	*)
		if test -f "$1"; then
			if test -z "${file1}"; then file1="$1"
			else
				if test -z "${file2}"; then file2="$1"
				else
				echo "${invocation_name}: Too many files; use --help for usage." 1>&2
				fi
			fi
		else
			echo "${invocation_name}: File \"$1\" not found or not a regular file" 1>&2
			exit 1
		fi ;;
	esac
	shift
done

if test -z "${file1}"; then
	echo "${invocation_name}: No files given; use --help for usage." 1>&2
	exit 1
fi

if test -z "${file2}"; then
	case "${file1}" in
	*.gz)
		file2=`printf "%s" "${file1}" | sed 's/.gz$//'` ;;
	*.tgz)
		file2=`printf "%s" "${file1}" | sed 's/tgz$/tar/'` ;;
	*.bz2)
		file2=`printf "%s" "${file1}" | sed 's/.bz2$//'` ;;
	*.tbz)
		file2=`printf "%s" "${file1}" | sed 's/tbz$/tar/'` ;;
	*.tbz2)
		file2=`printf "%s" "${file1}" | sed 's/tbz2$/tar/'` ;;
	*.lz)
		file2=`printf "%s" "${file1}" | sed 's/.lz$//'` ;;
	*.tlz)
		file2=`printf "%s" "${file1}" | sed 's/tlz$/tar/'` ;;
	*)
		file2="${file1}${default_ext}" ;;
	esac
fi

prog1=
prog2=
case "${file1}" in
*.gz | *.tgz)		prog1=gzip ;;
*.bz2 | *.tbz | *.tbz2)	prog1=bzip2 ;;
*.lz | *.tlz)		prog1=lzip ;;
esac
case "${file2}" in
*.gz | *.tgz)		prog2=gzip ;;
*.bz2 | *.tbz | *.tbz2)	prog2=bzip2 ;;
*.lz | *.tlz)		prog2=lzip ;;
esac

retval=0
if test -n "${prog1}"; then
	if test -n "${prog2}"; then
		tmp_file=`mktemp "${TMPDIR:-/tmp}"/lzdiff.XXXXXXXXXX` || {
			echo 'cannot create a temporary file' 1>&2
			exit 1
		}
		${prog2} -cdfq "${file2}" > "${tmp_file}" || exit 1
		${prog1} -cdfq "${file1}" | ${diff_prog} ${args} - "${tmp_file}"
		retval=$?
		rm -f "${tmp_file}" || retval=$?
	else
		${prog1} -cdfq "${file1}" | ${diff_prog} ${args} - "${file2}"
		retval=$?
	fi
else
	if test -n "${prog2}"; then
		${prog2} -cdfq "${file2}" | ${diff_prog} ${args} "${file1}" -
		retval=$?
	else
		${diff_prog} ${args} "${file1}" "${file2}"
		retval=$?
	fi
fi

exit ${retval}
