summaryrefslogtreecommitdiffstats
path: root/lib/spec2series.in
blob: 6f6b63655b3a5d3dc5f98308ad4a2f0694a89a00 (plain) (blame)
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
#!@BASH@

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

# defaults
debug=0
specfile=""
outfile=""

function usage() {
cat <<EOF
Usage: quilt spec2series [ options ] specfile

-h	print this text
-d	debug mode (prints lots additional info
	as comments into the output file)
-o <file>
	specify output file, stdout if unspecified

EOF
}

# parse args
while test "$1" != ""; do
	case "$1" in
		-h | --help)
			usage; exit 0
			;;
		-d | --debug)
			debug=1; shift
			;;
		-o | --outfile)
			outfile="$2"; shift; shift
			;;
		*)
			specfile="$1"
			break;
			;;
	esac
done

if test ! -f "$specfile"; then
	usage
	exit 1
fi

# get absolute patch for specfile location
specdir=`dirname $specfile`
specfile=`basename $specfile`
case "$specdir" in
	.)	specdir="`pwd`"
		;;
	/*)	# nothing
		;;
	*)	specdir="`pwd`/$specdir"
		;;
esac

# create tmp work dir
WORK="${TMPDIR-/tmp}/rpmlog-$$"
mkdir -p "$WORK" || exit 1
trap 'rm -rf "$WORK"' EXIT
mkdir -p "$WORK/build"
mkdir -p "$WORK/bin"

# create md5 sums, also for uncompressed files
echo -n "### md5: " >&2
(cd $specdir; for file in /dev/null *; do
	case "$file" in
		ready | bigpack | MD5SUMS | MD5SUMS.meta | *.spec | *.changes)
			continue
			;;
	esac
	type="`file -b $file | cut -d" " -f1`"
	case "$type" in
		compress*)
			echo -n "z" >&2
			set -- `zcat $file | md5sum`
			echo "$1 zcat  ${file}"
			;;
		gzip)
			echo -n "g" >&2
			set -- `zcat $file | md5sum`
			echo "$1 zcat  ${file}"
			;;
		bzip2)
			echo -n "b" >&2
			set -- `bzcat $file | md5sum`
			echo "$1 bzcat ${file}"
			;;
	esac
	echo -n "." >&2
	set -- `md5sum < $file`
	echo "$1 cat   ${file}"
done) > $WORK/md5sum
echo " done" >&2

# prepare rpm dir fixups and hooks
RPM="rpm --rcfile=/usr/lib/rpm/rpmrc:$WORK/rpmrc"
PATH="$WORK/bin:$PATH"
grep ^macrofiles /usr/lib/rpm/rpmrc			\
	| sed -e "/macrofiles/s|$|:$WORK/rpmmacros|"	\
	> $WORK/rpmrc
cat <<-EOF > "$WORK/rpmmacros"
	%_sourcedir	$specdir
	%_specdir	$specdir
	%_builddir	$WORK/build
EOF

# wrapper script for patch and tar
cat <<-'EOF' > "$WORK/bin/patch"
	#!/bin/sh

	# save stuff for log
	unpackcmd=`basename $0`
	unpackdir=`pwd`
	unpackargs="$*"
	uncompress="false"
	unpackfile="[oops]"

	# sort of progress bar
	case $unpackcmd in
		tar)	echo -n "t" >&2;;
		patch)	echo -n "p" >&2;;
		*)	echo -n "?" >&2;;
	esac

	# find real binary
	realcmd=""
	test -x "/bin/$unpackcmd"	&& realcmd="/bin/$unpackcmd"
	test -x "/usr/bin/$unpackcmd"	&& realcmd="/usr/bin/$unpackcmd"
	test "$realcmd" = ""		&& exit 1

	# put data into tmpfile, exec real cmd, return on failure
	WORK=`dirname $RPM_BUILD_DIR`
	cat > "$WORK/data"
	if test -x /bin/$unpackcmd; then
		cmddir="/bin"
	fi
	$realcmd $* < "$WORK/data"
	retval="$?"
	test "$retval" != "0" && exit $retval

	# find original data file by md5sum
	set -- `md5sum < "$WORK/data"`
	unpackfile="[$1]"
	set -- `cat "$WORK/md5sum"`
	while test "$1" != ""; do
		if test "[$1]" = "$unpackfile"; then
			uncompress="$2"
			unpackfile="$3"
			break
		fi
		shift
	done

	# print results
	unpackdir=`echo $unpackdir | sed -e "s|$RPM_BUILD_DIR|BUILD|"`
	echo -n "# log: [$unpackdir] $uncompress $unpackfile" >>$WORK/cmdlog
	echo    " | $unpackcmd $unpackargs"		      >>$WORK/cmdlog
	if test "$unpackcmd" = "patch" -a \
           -f "$RPM_SOURCE_DIR/$unpackfile"; then
		patchdir="${unpackdir#BUILD/}"
		if test ! -f "$WORK/patchdir"; then
			echo -n $patchdir > $WORK/patchdir
		fi
		if test "`cat $WORK/patchdir`" = "$patchdir"; then
			level=`echo $unpackargs | tr " " "\n" | grep ^-p`
			echo "$unpackfile $level" >> $WORK/patchlog
		fi
	fi
	if test "$unpackcmd" = "tar" -a \
           -f "$RPM_SOURCE_DIR/$unpackfile"; then
		echo -n "# Source: $unpackfile" >>$WORK/tarlog
		if test "$unpackdir" != "BUILD"; then
			echo -n " -C ${unpackdir#BUILD/}" >>$WORK/tarlog
		fi
		echo "" >>$WORK/tarlog
	fi
EOF
chmod 755 "$WORK/bin/patch"
ln -s patch "$WORK/bin/tar"

# let rpm do all the dirty specfile stuff ...
echo -n "### rpm: " >&2
touch $WORK/patchlog
$RPM --nodeps --quiet -bp "$specdir/$specfile" </dev/null
echo " done" >&2

# print results saved by the wrapper script
(
	# header
	echo "# Patch series file for quilt, created by $0"
	echo "#"
	echo "# Sourcedir: $specdir"
	echo "# Specfile: $specfile"
	if test -f $WORK/patchdir; then
		echo "# Patchdir: `cat $WORK/patchdir`"
	fi
	echo "#"

	# additional info for trouble shooting
	if test "$debug" = "1"; then
		cat $WORK/md5sum | sed -e 's/^/# md5: /'
		echo "#"

		test -f $WORK/cmdlog && cat $WORK/cmdlog && echo "#"
	fi

	# list tarballs + patches
	test -f $WORK/tarlog   && cat $WORK/tarlog && echo "#"
	test -f $WORK/patchlog && cat $WORK/patchlog
)|(
	if test "$outfile" != ""; then
		cat > "$outfile"
	else
		cat
	fi
)