File: repack.sh

package info (click to toggle)
zp 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 220 kB
  • ctags: 213
  • sloc: cpp: 2,443; sh: 251; makefile: 13
file content (374 lines) | stat: -rwxr-xr-x 7,319 bytes parent folder | download | duplicates (15)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/bin/sh
#
#   Copyright
#
#       Copyright (C) 2008-2010 Jari Aalto <jari.aalto@cante.net>
#
#   License
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program. If not, see <http://www.gnu.org/licenses/>.

set -e
set -u

DIR=""

Initialize ()
{
    # Check depends

    [ -x /bin/mktemp ] || Die "[ERROR]: mktemp (pkg: coreutils) not installed."
    [ -x /bin/bzip2  ] || Die "[ERROR]: bzip2 (pkg: bzip2) not installed."
    [ -x /bin/gzip   ] || Die "[ERROR]: gzip (pkg: gzip) not installed."
    [ -x /bin/tar    ] || Die "[ERROR]: tar (pkg: tar) not installed."
}

InitializeZip ()
{
    [ -x /usr/bin/unzip ] || Die "[ERROR]: unzip (pkg: unzip) not installed."
}

InitializeRar ()
{
    [ -x /usr/bin/unrar ] || Die "[ERROR]: unrar (pkg: unrar) not installed."
}

Help ()
{
    echo "
SYNOPSIS
  repack.sh [--upstream-source] <VER> <downloaded file> [PACKAGE]

DESCRIPTION
    Repackage upstream source. The command line arguments are
    according to to uscan(1) order. The PACKAGE argument is optional.

    Can also repack *.zip and *.rar files.

OPTIONS
    --upstream-source
	Option is ignored. It is passed from uscan(1) when debian/watch
	file is read.

EXAMPLES
    To manually repack original upstream source foo-1.1.tar.gz to
    bar-1.10.orig.tar.gz:

        repack.sh 1.10 foo-1.1.tar.gz bar

    Save this file to debian/repack.sh and add following line to debian/watch:

        version=3
        http://example.com .*package-(.+).tar.gz debian debian/repack.sh

AUTHOR
    Copyright (C) 2008-2010 Jari Aalto <jari.aalto@cante.net>

    Released under the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version."

    exit 0
}

Run ()
{
    if [ "${test+test_mode}" = "test_mode" ]; then
	echo "$@"
    else
	[ "${verbose+verbose_mode}" = "verbose_mode" ] && echo "$@" >&2
	"$@"
    fi
}

Warn ()
{
    echo "$*" >&2
}

Die ()
{
    Warn "$*"
    exit 1
}

AtExit ()
{
    if [ "$DIR" ]; then
	[ -d "$DIR" ] && rm -rf "$DIR"
    fi
}

DebianVersion ()
{
    # No version conversions yet
    echo $1
}

DebianTar ()
{
    local ver=$1
    local dver=$2
    local file=$3
    local pkg=$4

    # Convert suffixes

    file=$(echo $file | sed -e 's,\(tgz\|zip\|rar\)$,tar.gz,' )

    # If version is same, use original file

    if [ "$ver" = "$dver" ]; then
	if [ "$pkg" ]; then
	    echo $file | sed "s,.*$ver,${pkg}_$ver.orig,"
	else
            echo $file
	fi
	return 0
    fi

    if [ "$pkg" ]; then
	echo $file | sed -e "s,.*$ver,${pkg}_$dver.orig,"
    else
        # replace with new version
	echo $file | sed -e "s,$ver,$dver.orig,"
    fi
}

Pkg ()
{
    local file=$1

    if [ -f debian/changelog ]; then
	dpkg-parsechangelog | awk '/^Source:/ {print $2}'
    else

	# package-1.1.tar.gz => package
	echo $file | sed "s,-[0-9].*,,"
    fi
}

Version ()
{
    local file=$1
    local pkg=$(Pkg $file)

    if [ ! "$pkg" ]; then
	Die "[ERROR] Internal error. 'pkg' variable not set. Run with debug (-x)"
    fi

    echo $file |
    sed -e "s,\.tar.*,," \
	-e "s,\.tgz,," \
	-e "s,\.tbz,," \
	-e "s,\.tbz2,," \
	-e "s,\.zip,," \
	-e "s,\.7z,," \
	-e "s,\.rar,," \
	-e "s,\.lzma,," \
	-e "s,\.xz,," \
        -e "s,$pkg[-_],,"
}

Cleanup ()
{
    [ "$1" ] || return 1

    local dir
    dir=$1

    find "$dir" \
	\( \
	-iname "*.exe" \
	-o -name "*.swp" \
	-o -name "DEADJOE" \
	-o -name "*.[~#]" \
	-o -name ".gitignore" \
	-o -name ".bzrignore" \
	-o -name ".svnignore" \
	-o -name ".cvsignore" \
	-o -path "*/debian/*" \
	-o -path "*/CVS/*" \
	-o -path "*/.svn/*" \
	-o -path "*/.darcs/*" \
	-o -path "*/.bzr/*" \
	-o -path "*/.hg/*" \
	-o -path "*/.git/*" \
	\) \
	-print0 |
    xargs --null --no-run-if-empty rm -rf --verbose

    #	Remove executables

    find "$dir" -type f -print0 |
        xargs --null --no-run-if-empty file |
    awk -F: '$2 ~ /ELF|COFF|LSB exe/ {print $1}' |
        xargs --no-run-if-empty rm --verbose
}

Main ()
{
    if [ $# -eq 0 ]; then
        Help
    fi

    Initialize

    case "$1" in
	--help|-h)
		Help
		;;
	--*)    shift
		#  Ignore uscan(1) argument --upstream-version in $1
		;;
    esac

    VER="$1"
    FILENAME="$2"
    DIR=

    if [ ! -f "$FILENAME" ]; then
	Die "[ERROR] Arg 2. File does not exist: $FILENAME"
    fi

    FILE_DIR=$(dirname $FILENAME)
    FILE=$(basename $FILENAME)

    PKG=${3:-$(Pkg $FILE)}

    if [ ! "$PKG" ]; then
	Die "[ERROR] Internal error. PKG not set. Run with debug (-x)"
    fi

    CURVER=$(Version $FILE)

    if [ ! "$CURVER" ]; then
	Die "[ERROR] Internal error. CURVER not set. Run with debug (-x)"
    fi

    DVER=$(DebianVersion "$VER")
    DFILE=$(DebianTar "$CURVER" "$DVER" "$FILE" $PKG)

    #  Debian Developer's Reference 6.7.8.2 Repackaged upstream source

    REPACK_DIR="$PKG-$DVER.orig"

    DIR=$(mktemp -d ./tmp.repack.XXXXXX)

    if [ ! "$DIR" ]; then
	Die "[INTERNAL ERROR] mktemp(1) failed. Debug with 'sh -x PROGRAM'"
    fi

    echo "Repacking $FILENAME as $PKG-$DVER"

    #	Create an extra directory to cope with tarballs that
    #	do not have root/ directory

    UP_BASE="$DIR/unpack"
    Run mkdir "$UP_BASE"

    curdir=$(pwd)

    case "$FILENAME" in
	*.gz | *.bz2 )

	    Run tar -C "$UP_BASE" -xf "$FILENAME"
	    ;;

	*.zip)
	    InitializeZip

	    adir=$(dirname $FILENAME)
	    name=$(basename $FILENAME)

	    Run cd "$UP_BASE"
	    Run unzip "$curdir/$adir/$name" || return 1

	    cd $curdir
	    ;;

	*.rar)
	    InitializeRar

	    adir=$(dirname $FILENAME)
	    name=$(basename $FILENAME)

	    Run cd "$UP_BASE"
	    Run unrar x "$curdir/$adir/$name" || return 1

	    cd $curdir

	    ;;

	*)  Die "Unknonw file format: $FILENAME"
	    ;;
    esac

    if [ $(ls -1 "$UP_BASE" | wc -l) -eq 1 ]; then
	# Tarball does contain a root directory
	UP_BASE="$UP_BASE/$(ls -1 "$UP_BASE")"
    fi

    if [ ! "$UP_BASE" ]; then
	Die "[INTERNAL ERROR] UP_BASE not set"
    fi

    Cleanup "$UP_BASE"

    #	Repack

    Run mv "$UP_BASE" "$DIR/$REPACK_DIR"

    #	Don't use pipes. Errors are not handled well with them.

    Run tar -C "$DIR" -cf "$DIR/repacked.tar" "$REPACK_DIR"

    #   The .orig file must use gzip compression

    tar="$DIR/repacked.tar"

    case "$DFILE" in
	*.bz2)
	    DFILE=$(echo $DFILE | sed "s/.bz2/.gz/")
	    ;;
	*.gz)
	    ;;
	*.zip)
	    DFILE=$(echo $DFILE | sed "s/.zip/.gz/")
	    ;;
	*.rar)
	    DFILE=$(echo $DFILE | sed "s/.rar/.gz/")
	    ;;
	 *)
	    Die "Unknown *.suffix in $DFILE"
	    ;;
    esac

    suffix=".gz"

    Run gzip --best "$tar"

    if [ -f "$DFILE" ]; then
	echo "Warning, overwriting $DFILE"
    fi

    Run mv "$tar$suffix" "$DFILE"

    echo "Done $DFILE"
}

trap AtExit QUIT INT EXIT
Main "$@"

# End of file