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