summaryrefslogtreecommitdiff
path: root/contrib/bacman.in
blob: f7e3202fd6c7aa887bd2289be3ac5a0f15c872fe (plain)
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
#!/bin/bash
#
#   bacman: recreate a package from a running system
#   This script rebuilds an already installed package using metadata
#   stored into the pacman database and system files
#
#   Copyright (c) 2008 locci <carlocci_at_gmail_dot_com>
#   Copyright (c) 2008-2012 Pacman Development Team <pacman-dev@archlinux.org>
#
#   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/>.
#

shopt -s extglob
shopt -s nullglob

readonly progname="bacman"
readonly progver="0.2.1"

#
# User Friendliness
#
usage() {
	echo "This program recreates a package using pacman's db and system files"
	echo "Usage:   $progname <installed package name>"
	echo "Example: $progname kernel26"
}

if (( $# != 1 )); then
	usage
	exit 1
fi

if [[ $1 == "--help" || $1 == "-h" ]]; then
	usage
	exit 0
fi

if [[ $1 == "--version" || $1 == "-v" ]]; then
	echo "$progname version $progver"
	echo "Copyright (C) 2008 locci"
	exit 0
fi

#
# Fakeroot support
#
if (( EUID )); then
	if [[ -f /usr/bin/fakeroot ]]; then
		echo "Entering fakeroot environment"
		export INFAKEROOT="1"
		/usr/bin/fakeroot -u -- "$0" "$@"
		exit $?
	else
		echo "WARNING: installing fakeroot or running ${progname} as root is required to"
		echo "         preserve the ownership permissions of files in some packages"
		echo ""
	fi
fi

#
# Setting environmental variables
#
if [[ ! -r @sysconfdir@/pacman.conf ]]; then
	echo "ERROR: unable to read @sysconfdir@/pacman.conf"
	exit 1
fi

eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
pac_db="${DBPath:-@localstatedir@/lib/pacman/}/local"

if [[ ! -r @sysconfdir@/makepkg.conf ]]; then
	echo "ERROR: unable to read @sysconfdir@/makepkg.conf"
	exit 1
fi

source "@sysconfdir@/makepkg.conf"
if [[ -r ~/.makepkg.conf ]]; then
	source ~/.makepkg.conf
fi

pkg_dest="${PKGDEST:-$PWD}"
pkg_pkger=${PACKAGER:-'Unknown Packager'}

pkg_name="$1"
pkg_dir=("$pac_db/$pkg_name"-+([^-])-+([^-]))
pkg_namver=("${pkg_dir[@]##*/}")

#
# Checks everything is in place
#
if [[ ! -d $pac_db ]]; then
	echo "ERROR: pacman database directory ${pac_db} not found"
	exit 1
fi

if (( ${#pkg_dir[@]} != 1 )); then
	printf "ERROR: %d entries for package %s found in pacman database\n" \
		${#pkg_dir[@]} "${pkg_name}"
	printf "%s\n" "${pkg_dir[@]}"
	exit 1
fi

if [[ ! -d $pkg_dir ]]; then
	printf "ERROR: package %s is found in pacman database,\n" "${pkg_name}"
	printf "       but \`%s' is not a directory\n" "${pkg_dir}"
	exit 1
fi

#
# Begin
#
echo "Package: ${pkg_namver}"
work_dir=$(mktemp -d --tmpdir bacman.XXXXXXXXXX)
cd "$work_dir" || exit 1

#
# File copying
#
echo "Copying package files..."

cat "$pkg_dir"/files |
while read i; do
	if [[ -z $i ]]; then
		continue
	fi

	if [[ $i == %+([A-Z])% ]]; then
		current=$i
		continue
	fi

	case "$current" in
		%FILES%)
			ret=0
			if [[ -e /$i ]]; then
				bsdtar -cnf - "/$i" 2> /dev/null | bsdtar -xpf -

				# Workaround to bsdtar not reporting a missing file as an error
				if ! [[ -e $work_dir/$i || -L $work_dir/$i ]]; then
					echo ""
					echo "ERROR: unable to add /$i to the package"
					echo "       If your user does not have permssion to read this file then"
					echo "       you will need to run $progname as root"
					rm -rf "$work_dir"
					exit 1
				fi
			else
				echo ""
				echo "WARNING: package file /$i is missing"
				echo ""
			fi
			;;
	esac
done

ret=$?
if (( ret )); then
	rm -rf "$work_dir"
	exit 1
fi

pkg_size=$(du -sk | awk '{print $1 * 1024}')

#
# .PKGINFO stuff
# TODO adopt makepkg's write_pkginfo() into this or scripts/library
#
echo Generating .PKGINFO metadata...
echo "# Generated by $progname $progver"    > .PKGINFO
if [[ $INFAKEROOT == "1" ]]; then
	echo "# Using $(fakeroot -v)"    >> .PKGINFO
fi
echo "# $(LC_ALL=C date)"            >> .PKGINFO
echo "#"                    >> .PKGINFO

while read i; do
	if [[ -z $i ]]; then
		continue;
	fi

	if [[ $i == %+([A-Z])% ]]; then
		current=$i
		continue
	fi

	case "$current" in
		# desc
		%NAME%)
			echo "pkgname = $i"    >> .PKGINFO
			;;
		%VERSION%)
			echo "pkgver = $i"    >> .PKGINFO
			;;
		%DESC%)
			echo "pkgdesc = $i"    >> .PKGINFO
			;;
		%URL%)
			echo "url = $i"    >> .PKGINFO
			;;
		%LICENSE%)
			echo "license = $i"    >> .PKGINFO
			;;
		%ARCH%)
			echo "arch = $i"    >> .PKGINFO
			pkg_arch="$i"
			;;
		%BUILDDATE%)
			echo "builddate = $(date -u "+%s")"    >> .PKGINFO
			;;
		%PACKAGER%)
			echo "packager = $pkg_pkger"        >> .PKGINFO
			;;
		%SIZE%)
			echo "size = $pkg_size"        >> .PKGINFO
			;;
		%GROUPS%)
			echo "group = $i"    >> .PKGINFO
			;;
		%REPLACES%)
			echo "replaces = $i"    >> .PKGINFO
			;;
		%DEPENDS%)
			echo "depend = $i"   >> .PKGINFO
			;;
		%OPTDEPENDS%)
			echo "optdepend = $i" >> .PKGINFO
			;;
		%CONFLICTS%)
			echo "conflict = $i" >> .PKGINFO
			;;
		%PROVIDES%)
			echo "provides = $i"  >> .PKGINFO
			;;

		# files
		%BACKUP%)
			# strip the md5sum after the tab
			echo "backup = ${i%%$'\t'*}"   >> .PKGINFO
			;;
	esac
done < <(cat "$pkg_dir"/{desc,files})

comp_files=".PKGINFO"

if [[ -f $pkg_dir/install ]]; then
	cp "$pkg_dir/install" "$work_dir/.INSTALL"
	comp_files+=" .INSTALL"
fi
if [[ -f $pkg_dir/changelog ]]; then
	cp "$pkg_dir/changelog" "$work_dir/.CHANGELOG"
	comp_files+=" .CHANGELOG"
fi

#
# Fixes owner:group and permissions for .PKGINFO, .CHANGELOG, .INSTALL
#
chown root:root "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null

#
# Generate the package
#
echo "Generating the package..."

pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
ret=0

# TODO: Maybe this can be set globally for robustness
shopt -s -o pipefail
bsdtar -cf - $comp_files * |
case "$PKGEXT" in
	*tar.gz)  gzip -c -f -n ;;
	*tar.bz2) bzip2 -c -f ;;
	*tar.xz)  xz -c -z - ;;
	*tar.Z)   compress -c -f ;;
	*tar)     cat ;;
	*) echo "WARNING: '%s' is not a valid archive extension." \
	"$PKGEXT" >&2; cat ;;
esac > "${pkg_file}"; ret=$?

if (( ret )); then
	echo "ERROR: unable to write package to $pkg_dest"
	echo "       Maybe the disk is full or you do not have write access"
	rm -rf "$work_dir"
	exit 1
fi

rm -rf "$work_dir"

echo Done

exit 0

# vim: set ts=2 sw=2 noet: