summaryrefslogtreecommitdiffstats
path: root/quilt/setup.in
blob: f4efdc7240ead9ce7b991d13cb2c535e3870ba2d (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
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
#! @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.
# Read in library functions
if [ "$(type -t patch_file_name)" != function ]
then
	if ! [ -r @SCRIPTS@/patchfns ]
	then
		echo "Cannot read library @SCRIPTS@/patchfns" >&2
		exit 1
	fi
	. @SCRIPTS@/patchfns
fi

usage()
{
	echo $"Usage: quilt setup [-d sourcedir] [-l seriesfile]  {seriesfile|specfile}"
	if [ x$1 = x-h ]
	then
		echo $"

Initializes a source tree from a patch series file.  The patch series
file must contain the name of the relevant tar archive, in addition to
the list of patches.

-d	The directory that contains the archives and patches. Defaults
	to the directory of the series/spec file.

-l	Make the patches directory a symbolic link. If a series file is
	specified, also create a symlink to the series file.
"
		exit 0
	else
		exit 1
	fi
}

parse_series()
{
	local series="$1"

	perl -e '
		while(<>) {
			if (/^#\s*Sourcedir:\s*(\S+)/) {
				print "SOURCEDIR $1\n";
			} elsif (/^#\s*[Ss]ource:\s*(\S+)\s*(-C\s*(\S+))?/) {
				print "SOURCE $1 ", ($3 ? $3 : "."), "\n";
			} elsif (/^#\s*[Pp]atchdir:\s*(\S+)/) {
				print "PATCHDIR $1\n";
			} elsif (/^([^#\s]+)/) {
				print "PATCH $1\n";
			}
		}
	' $series
	echo "SERIES $series"
}

options=`getopt -o d:lh -- "$@"`

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"
link_opt=0
while true
do
	case "$1" in
	-d)
		opt_source=$2
		shift 2 ;;
	-l)
		opt_link=1
		shift ;;
	-h)
		usage -h ;;
	--)
		shift
		break ;;
	esac
done

status=0
packagedir=.

if [ $# -ne 1 ]
then
	usage
fi

case "$1" in
*.spec)
	spec_file="$1"
	tmpfile=$(gen_tempfile)
	series_file=$tmpfile
	if ! @SCRIPTS@/spec2series "$spec_file" > $tmpfile
	then
		exit 1
	fi
	;;
*)
	series_file=$1
	if ! [ -e "$series_file" ]
	then
		echo $"Series file $series_file not found"
		exit 1
	fi
	;;
esac

if [ -n "$opt_source" ]
then
	source="$opt_source"
elif [ -n "$spec_file" ]
then
	source="$(dirname "$spec_file")"
else
	source="$(dirname $(dirname $series_file))/patches"
fi

status=0
packagedir=.
while read cmd arg arg2
do
	case $cmd in
	SOURCEDIR)
		# Directory for package sources
		if [ -z "$source" ]
		then
			source="$arg"
			echo $"Reading sources from $arg"
		fi
		;;
	SOURCE)
		echo $"Unpacking archive $source/$arg"
		mkdir -p "$arg2" && \
		cat_file "$source/$arg" \
		| tar xf - -C "${arg2:-.}"
		;;
	PATCHDIR)
		# Directory where package is expanded
		packagedir="$arg"
		if [ -d "$packagedir" ]
		then
			echo $"Directory $packagedir exists already."
			status=1
			break
		elif [ -e "$packagedir" ]
		then
			echo $"File $packagedir exists."
			status=1
			break
		fi
		;;
	PATCH)
		if [ -n "$opt_link" ]
		then
			if ! [ -e "$packagedir/patches" ]
			then
				arg="$source"
				if [ "${arg:0:1}" != "/" ]
				then
					arg="$PWD/$arg"
				fi
				echo $"Creating link to patches directory $arg"
				ln -s "$arg" "$packagedir/patches"
			elif [ -z "`readlink $packagedir/patches`" ]
			then
				echo $"$packagedir/patches exist, but not symlink" >&2
				status=1
				break
			fi
		else
			echo $"Copying patch $source/$arg"
			mkdir -p "$packagedir/patches/" && \
			cp "$source/$arg" "$packagedir/patches/"
		fi
		;;
	SERIES)
		if [ -n "$opt_link" ]
		then
			# The patches dir is a symlink to the directory
			# containing the patches. Don't write the series
			# file into that directory.
			if [ -e "$packagedir/series" ]
			then
				echo $"File $packagedir/series exists; will not override." >&2
				status=1
			elif [ -n "$spec_file" ]
			then
				# There is no series file to link to; the
				# series file we are reading from is a
				# temporary file.
				echo $"Copying series file"
				cp "$arg" "$packagedir/series"
			else
				# Create a symlink to the series file. We don't
				# want to mess with changing relative symlink
				# targets...
				if [ "${arg:0:1}" != "/" ]
				then
					arg="$PWD/$arg"
				fi
				echo $"Creatig link to series file $arg"
				ln -s "$arg" "$packagedir/series"
			fi
		else
			echo $"Copying series file"
			cp "$arg" "$packagedir/patches/series"
		fi
		;;
	*)
		status=1
		break
		;;
	esac
	s=$?
	if [ $status -eq 0 ]
	then
		status=$s
		[ $status != 0 ] && break
	fi
done \
< <(parse_series "$series_file")

if [ -n "$tmpfile" ]
then
	rm -f $tmpfile
fi

if [ $status -ne 0 ]
then
	exit 1
fi

if [ "$packagedir" != "." ]
then
	echo $"Directory $packagedir set up."
fi
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh