1#!/bin/bash
2
3####################
4#    Copyright (C) 2007, 2008 by Raphael Geissert <atomo64@gmail.com>
5#
6#    This file is free software: you can redistribute it and/or modify
7#    it under the terms of the GNU General Public License as published by
8#    the Free Software Foundation, either version 3 of the License, or
9#    (at your option) any later version.
10#
11#    This file is distributed in the hope that it will be useful,
12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#    GNU General Public License for more details.
15#
16#    You should have received a copy of the GNU General Public License
17#    along with this file  If not, see <https://www.gnu.org/licenses/>.
18#
19#    On Debian systems, the complete text of the GNU General
20#    Public License 3 can be found in '/usr/share/common-licenses/GPL-3'.
21####################
22
23set -e
24
25PROGNAME=$(basename "$0")
26
27usage () {
28    echo \
29"Usage: $PROGNAME [options] FILE.diff.gz
30  Options:
31    --help          Show this message
32    --version       Show version and copyright information
33  debian/control must exist on the current path for this script to work
34  If debian/patches exists and is a directory, patches are extracted there,
35  otherwise they are extracted under debian/ (unless the environment variable
36  DEB_PATCHES is defined and points to a valid directory, in which case
37  patches are extracted there)."
38}
39
40version () {
41    echo \
42"This is $PROGNAME, from the Debian devscripts package, version ###VERSION###
43This code is copyright 2007, 2008 by Raphael Geissert, all rights reserved.
44This program comes with ABSOLUTELY NO WARRANTY.
45You are free to redistribute this code under the terms of the
46GNU General Public License, version 3 or later."
47}
48
49case "$1" in
50	--help) usage; exit 0 ;;
51	--version) version; exit 0 ;;
52esac
53
54if ! which lsdiff >/dev/null 2>&1; then
55	echo "lsdiff was not found in \$PATH, package patchutils probably not installed!"
56	exit 1
57fi
58
59diffgz="$1"
60
61if [ ! -f "$diffgz" ]; then
62	[ -z "$diffgz" ] && diffgz="an unspecified .diff.gz"
63	echo "Couldn't find $diffgz, aborting!"
64	exit 1
65fi
66
67if [ -x /usr/bin/dh_testdir ]; then
68	/usr/bin/dh_testdir || exit 1
69else
70	[ ! -f debian/control ] && echo "Couldn't find debian/control!" && exit 1
71fi
72
73if [ -z "$DEB_PATCHES" ] || [ ! -d "$DEB_PATCHES" ]; then
74	DEB_PATCHES=debian
75	[ -d debian/patches ] && DEB_PATCHES=debian/patches
76else
77	DEB_PATCHES="$(readlink -f "$DEB_PATCHES")"
78fi
79
80echo "Patches will be extracted under $DEB_PATCHES/"
81
82FILES=$(zcat "$diffgz" | lsdiff --strip 1 | grep -v ^debian/) || \
83	echo "$(basename "$diffgz") doesn't contain any patch outside debian/"
84
85for file in $FILES; do
86	[ ! -z "$file" ] || continue
87	echo -n "Extracting $file..."
88	newFileName="$DEB_PATCHES/$(echo "$file" | sed 's#/#___#g').patch"
89	zcat "$diffgz" | filterdiff -i "$file" -p1 > "$newFileName"
90	echo "done"
91done
92
93exit
94