summaryrefslogtreecommitdiffstats
path: root/zdiff.in
blob: 9ac0a58cc3a459c9b191a180694dc75c57c8a4f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#! /bin/sh
# Zdiff - 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
args=
diff_prog=diff
file1=
file2=
two_hyphens=0

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

	case "$1" in
	--help | --he* | -h)
		echo "Zdiff - Diff/cmp wrapper for compressed files."
		echo
		echo "Zdiff 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, lzip and xz."
		echo
		echo "Zcmp is a shortcut for \"zdiff --cmp\""
		echo
		echo "Usage: $0 [OPTIONS] [DIFF_OPTIONS] FILE1 [FILE2]"
		echo
		echo "Compares FILE1 to FILE2. If FILE2 is omitted and FILE1 is compressed,"
		echo "compares FILE1 to the file with the corresponding decompressed file"
		echo "name (removes the extension from FILE1). If FILE2 is omitted and FILE1"
		echo "is not compressed, compares FILE1 to the uncompressed contents of"
		echo "FILE1.[gz|bz2|lz|xz] (the first one that is found)."
		echo "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 "      --diff          use diff to compare files (default)"
		echo "      --cmp           use cmp to compare files"
		echo
		echo "Report bugs to zutils-bug@nongnu.org"
		echo "Zutils home page: http://www.nongnu.org/zutils/zutils.html"
		exit 0 ;;
	--version | --ve* | -V)
		echo "Zdiff VERSION"
		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 ;;
	--diff)
		diff_prog=diff ;;
	--cmp)
		diff_prog=cmp ;;
	-)
		echo "$0: reading from stdin not supported"
		exit 1 ;;
	--)
		shift; two_hyphens=1 ; break ;;
	-?*)
		args="${args} $1" ;;
	*)
		break ;;
	esac
	shift
done

# Loop over files
for i in "$@" ; do
	if [ "$i" = "--" ] && [ ${two_hyphens} = 0 ] ; then two_hyphens=1
	else
		if [ -f "$i" ]; then
			if [ -z "${file1}" ]; then file1="$i"
			else
				if [ -z "${file2}" ]; then file2="$i"
				else
					echo "$0: Too many files; use --help for usage." 1>&2
				fi
			fi
		else
			echo "$0: File \"$i\" not found or not a regular file" 1>&2
			exit 1
		fi
	fi
done

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

if [ -z "${file2}" ]; then
	case "${file1}" in
	*.gz)	file2=`printf "%s" "${file1}" | sed -e 's,.gz$,,'` ;;
	*.tgz)	file2=`printf "%s" "${file1}" | sed -e 's,tgz$,tar,'` ;;
	*.bz2)	file2=`printf "%s" "${file1}" | sed -e 's,.bz2$,,'` ;;
	*.tbz)	file2=`printf "%s" "${file1}" | sed -e 's,tbz$,tar,'` ;;
	*.tbz2)	file2=`printf "%s" "${file1}" | sed -e 's,tbz2$,tar,'` ;;
	*.lz)	file2=`printf "%s" "${file1}" | sed -e 's,.lz$,,'` ;;
	*.tlz)	file2=`printf "%s" "${file1}" | sed -e 's,tlz$,tar,'` ;;
	*.xz)	file2=`printf "%s" "${file1}" | sed -e 's,.xz$,,'` ;;
	*.txz)	file2=`printf "%s" "${file1}" | sed -e 's,txz$,tar,'` ;;
	*)
		if [ -f "${file1}.gz" ];  then file2="${file1}.gz"
		elif [ -f "${file1}.bz2" ]; then file2="${file1}.bz2"
		elif [ -f "${file1}.lz" ];  then file2="${file1}.lz"
		elif [ -f "${file1}.xz" ];  then file2="${file1}.xz"
		else
			echo "$0: Compressed version of ${file1} not found; use --help for usage." 1>&2
			exit 1
		fi ;;
	esac
fi

prog1=
prog2=
bindir=`echo "$0" | sed -e 's,[^/]*$,,'`
if [ -f "${file1}" ]; then
	prog_name=`"${bindir}"zutils -t -- "${file1}"`
	case "${prog_name}" in
	gzip)	prog1=gzip ;;
	bzip2)	prog1=bzip2 ;;
	lzip)	prog1=lzip ;;
	xz)	prog1=xz ;;
	esac
fi
if [ -f "${file2}" ]; then
	prog_name=`"${bindir}"zutils -t -- "${file2}"`
	case "${prog_name}" in
	gzip)	prog2=gzip ;;
	bzip2)	prog2=bzip2 ;;
	lzip)	prog2=lzip ;;
	xz)	prog2=xz ;;
	esac
fi

retval=0
if [ -n "${prog1}" ]; then
	if [ -n "${prog2}" ]; then
		tmp_file=`mktemp "${TMPDIR:-/tmp}"/zdiff.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 [ -n "${prog2}" ]; then
		${prog2} -cdfq -- "${file2}" | ${diff_prog} ${args} -- "${file1}" -
		retval=$?
	else
		${diff_prog} ${args} -- "${file1}" "${file2}"
		retval=$?
	fi
fi

exit ${retval}