fix a link in the docs
[divverent/nexuiz.git] / misc / tools / zipdiff
1 #!/bin/sh
2
3 usage()
4 {
5         cat <<EOF
6 Usage:
7   $0 -o difference.zip -f from.zip -t to.zip
8   $0 -f from.zip -t to.zip
9 EOF
10         exit 1
11 }
12
13 output=
14 from=
15 to=
16 excludes=
17
18 while [ $# -gt 0 ]; do
19         o=$1
20         shift
21         case "$o" in
22                 -o)
23                         output=$1
24                         shift
25                         ;;
26                 -f)
27                         from=$1
28                         shift
29                         ;;
30                 -t)
31                         to=$1
32                         shift
33                         ;;
34                 -x)
35                         excludes="$excludes $1"
36                         shift
37                         ;;
38                 *)
39                         usage
40                         ;;
41         esac
42 done
43
44 [ -n "$from" ] || usage
45 [ -n "$to" ] || usage
46
47 found()
48 {
49         type=$1
50         source=$2
51         echo >&2 "$type: $source"
52         case "$type" in
53                 new|changed|deleted)
54                         echo "$source"
55                         ;;
56                 excluded)
57                         ;;
58                 deleted|*)
59                         echo >&2 " * Sorry, can't handle deletion of $source."
60                         ;;
61         esac
62 }
63
64 tempdir=`mktemp -d -t zipdiff.XXXXXX`
65
66 newline="
67 "
68 fromlist="$(zipinfo -1 "$from" | grep -v /\$)"
69 tolist="$(zipinfo -1 "$to" | grep -v /\$)"
70
71 diffit()
72 {
73         echo "$fromlist" | while IFS= read -r line; do
74                 case "$newline$tolist$newline" in
75                         *$newline$line$newline*)
76                                 ;;
77                         *)
78                                 isexcluded=false
79
80                                 for P in $excludes; do
81                                         case "$line" in
82                                                 $P)
83                                                         found excluded "$line"
84                                                         isexcluded=true
85                                                         break
86                                                         ;;
87                                         esac
88                                 done
89
90                                 if ! $isexcluded; then
91                                         found deleted "$line"
92                                 fi
93                                 ;;
94                 esac
95         done
96         echo "$tolist" | while IFS= read -r line; do
97                 case "$newline$fromlist$newline" in
98                         *$newline$line$newline*)
99                                 # check if equal
100                                 isexcluded=false
101
102                                 for P in $excludes; do
103                                         case "$line" in
104                                                 $P)
105                                                         found excluded "$line"
106                                                         isexcluded=true
107                                                         break
108                                                         ;;
109                                         esac
110                                 done
111
112                                 if ! $isexcluded; then
113                                         unzip -p "$from" "$line" > "$tempdir/v1"
114                                         unzip -p "$to" "$line" > "$tempdir/v2"
115                                         if ! diff --brief "$tempdir/v1" "$tempdir/v2" >/dev/null 2>&1; then
116                                                 found changed "$line"
117                                         fi
118                                         rm "$tempdir/v1"
119                                         rm "$tempdir/v2"
120                                 fi
121                                 ;;
122                         *)
123                                 # check if equal
124                                 isexcluded=false
125
126                                 for P in $excludes; do
127                                         case "$line" in
128                                                 $P)
129                                                         found excluded "$line"
130                                                         isexcluded=true
131                                                         break
132                                                         ;;
133                                         esac
134                                 done
135
136                                 if ! $isexcluded; then
137                                         found new "$line"
138                                 fi
139                                 ;;
140                 esac
141         done
142 }
143
144 result=`diffit`
145
146 case "$output" in
147         '')
148                 ;;
149         *)
150                 rm -f "$output"
151                 echo "$result" | while IFS= read -r line; do
152                         echo >&2 "extracting $line..."
153                         dline=./$line
154                         mkdir -p "$tempdir/${dline%/*}"
155                         unzip -p "$to" "$line" > "$tempdir/$line" # this may create an empty file - don't care, DP handles this as deletion
156                 done
157                 case "$output" in
158                         /*)
159                                 ;;
160                         *)
161                                 output=`pwd`/$output
162                                 ;;
163                 esac
164                 cd "$tempdir"
165                 #zip -9r "$output" .
166                 7za a -tzip -mx=9 "$output" .
167                 cd ..
168                 ;;
169 esac
170
171 rm -rf "$tempdir"