summaryrefslogtreecommitdiffstats
path: root/quilt/setup.in
blob: ef37773816a23108b5b30ab8ad5931e92fc3cb60 (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
#! @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
	if [ -n "$SUBDIR" ]
	then
		cd $SUBDIR
		unset SUBDIR
	fi
fi

usage()
{
	printf $"Usage: quilt setup [-d path-prefix] [-v] {specfile|seriesfile}\n"
	if [ x$1 = x-h ]
	then
		printf $"
Initializes a source tree from an rpm spec file or a quilt series file.

-d	optional path prefix (sub-directory).

-v	verbose debug output.
"
		exit 0
	else
		exit 1
	fi
}

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

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

prefix=

while true
do
	case "$1" in
	-d)
		prefix=${2%/}/
		shift 2 ;;
	-h)
		usage -h ;;
	-v)
		verbose=-v
		shift ;;
	--)
		shift
		break ;;
	esac
done

if [ $# -ne 1 ]
then
	usage
fi

up() {
	if [ "${1:0:1}" = / ]
	then
		echo $1
	else
		local up=
		while ! [ "$1$up" -ef . -o "$1$up" -ef / ]
		do
			up=$up/..
		done
		echo ${up:1}
	fi
}

check_for_existing_files() {
	local dir status=0
	for dir in $(@AWK@ ' $1 = "patch" { print $2 }' $tmpfile | uniq)
	do
		if [ -e "$prefix$dir/$QUILT_PATCHES" ]
		then
			printf $"Directory %s exists\n" \
			       "$prefix$dir/$QUILT_PATCHES" >&2
			status=1
		fi
		if [ -e "$prefix$dir/series" ]
		then
			printf $"File %s exists\n" "$prefix$dir/series" >&2
			status=1
		fi
	done
	return $status
}

tmpfile=$(gen_tempfile)
trap "rm -f $tmpfile" EXIT

case "$1" in
*.spec)
	spec_file="$1"

	if ! @SCRIPTS@/inspect $verbose "$spec_file" 2>&1 > $tmpfile
	then
		printf $"The %%prep section of %s failed; results may be incomplete\n" "$spec_file"
		if [ -z "$verbose" ]
		then
			printf $"The -v option will show rpm's output\n"
		fi
	fi
	;;
*)
	# parse series file
	while read line; do
		set -- $line
		case "$@" in
		"# Sourcedir: "*)
			shift 2
			tar_dir="$@" ;;
		"# Source: "*)
			shift 2
			echo "tar ${tar_dir:-.} $@" ;;
		"# Patchdir: "*)
			shift 2
			patch_dir="$@" ;;
		''|'#'*)
			;;
		*)
			echo "patch ${patch_dir:-.} $@" ;;
		esac
	done < "$1" > $tmpfile
	;;
esac

check_for_existing_files || exit 1

while read tag dir arg1 arg2
do
	mkdir -p "$prefix$dir"
	case "$tag" in
	tar)
		printf $"Unpacking archive %s\n" "$arg1"
		cat_file "$arg1" \
		| tar xf - -C "$prefix$dir"
		;;
	esac
done < $tmpfile

check_for_existing_files || exit 1

while read tag dir arg1 arg2
do
	case "$tag" in
	tar)
		tar_dir="$dir"
		[ "$tar_dir" = . ] && tar_dir=
		tar_file="$arg1"
		;;
	patch)
		[ -e "$prefix$dir/$QUILT_PATCHES" ] \
		|| ln -s "$(up $prefix$dir)" $prefix$dir/$QUILT_PATCHES

		if ! [ -e "$prefix$dir/series" ]
		then
			# (There is no reliable way to associate archives with
			#  patches; we assume that all pathes applied after
			#  archive extraction belong to that archive.)
			(	echo "# Patch series file for quilt," \
				     "created by ${0##*/}"
				[ -n "$tar_dir" ] \
					&& echo "# Sourcedir: $tar_dir"
				[ -n "$tar_file" ] \
					&& echo "# Source: $tar_file"
				echo "# Patchdir: $dir"
				echo "#"
			) > "$prefix$dir/series"
		fi
		echo "$arg1" $arg2 >> "$prefix$dir/series"
		;;
	esac
done < $tmpfile
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh