1#!/bin/sh
2
3#
4# ardiff tries to make it easy for you to compare two archives. It makes no
5# claims about originality, correctness or usefulness, but it saved a lot of
6# time for at least one port maintainer.
7#
8# DEPS: vim, p7zip
9# TODO: word-by-word, more intelligent diffs
10#
11
12usage () {
13	if [ "$#" -gt 0 ]; then
14		echo "Error: $*" >&2
15	fi
16	echo "Usage: $0 <archive1> <archive2>" >&2
17	exit 0
18}
19
20debug () {
21	echo "Debug: $*" >&2
22}
23
24die () {
25	echo "Fatal: $*" >&2
26	exit 2
27}
28
29extract () {
30	if [ "$#" != 2 ]; then
31		die "extract () miscalled" >&2
32	fi
33	cd $2
34	if tar tf $1 >&-; then
35		debug "file $1 looks like a good tar archive"
36		tar xf $1 && debug "file $1 untar'ed successfully" || die "file $1 failed to untar"
37	elif 7z t $1 >&-; then
38		debug "file $1 looks like a good non-tar archive"
39		7z x $1 >&- && debug "file $1 un7z'ed successfully" || die "file $1 failed to un7z"
40	else
41		die "file $1 was not recognized"
42	fi
43}
44
45if [ "$#" != "2" ]; then
46	usage
47fi
48
49ar1=`realpath $1`
50ar2=`realpath $2`
51
52for i in $ar1 $ar2;do if [ ! -r $i ]; then
53	usage "file \"$i\" unreadable"
54fi;done
55
56for i in tar 7z;do if [ ! -x `which $i` ]; then
57	die "missing a decompressor, please make sure tar and 7z are available"
58fi;done
59if [ ! -x `which vim` ]; then
60	die "missing vim, please install it"
61fi
62
63art1=`mktemp -d -t /tmp` && debug "created tmp dir $art1" || usage "could not create a tmp dir"
64art2=`mktemp -d -t /tmp` && debug "created tmp dir $art2" || usage "could not create a tmp dir"
65
66extract $ar1 $art1
67extract $ar2 $art2
68
69if [ "`ls $art1|wc -l`" -eq 1 ]; then
70	dart1=$art1/`ls $art1|head -n1`
71else
72	dart1=$art1
73fi
74if [ "`ls $art2|wc -l`" -eq 1 ]; then
75	dart2=$art2/`ls $art2|head -n1`
76else
77	dart2=$art2
78fi
79#if [ "`ls $art1|wc -l`" -eq 1 ]&&[ "`ls $art2|wc -l`" -eq 1 ]; then
80#	dart1=$art1/`ls $art1|head -n1`
81#	dart2=$art2/`ls $art2|head -n1`
82#else
83#	dart1=$art1
84#	dart2=$art2
85#fi
86
87{
88	echo "====== Appeared and disappeared ======"
89	diff -urq $dart1 $dart2|sed -e "s|$art1|OLD|;s|$art2|NEW|"|grep '^Only'
90	echo "====== Modified ======"
91	diff -urq $dart1 $dart2|sed -e "s|$art1|OLD|;s|$art2|NEW|"|grep -v '^Only'
92	echo "====== Modifications ======"
93	diff -ur $dart1 $dart2|sed -e "s|$art1|OLD|;s|$art2|NEW|"
94}|vim -R -c "syntax on" -c "set syntax=diff" -c "set nowrap" \
95	-c 'let @/="^--- "' -c "set so=20" -
96
97rm -rf $art1 $art2 && debug "removed tmp dirs"
98