makepkg: devel_check(): cleanup for hg version
[pacman-ng.git] / contrib / bacman.in
blob936235656b0463e58e931eced64c1cb8276eb9b6
1 #!/bin/bash
3 # bacman: recreate a package from a running system
4 # This script rebuilds an already installed package using metadata
5 # stored into the pacman database and system files
7 # (c) 2008 - locci <carlocci_at_gmail_dot_com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 shopt -s extglob
24 shopt -s nullglob
26 declare -r myname='bacman'
27 declare -r myver='@PACKAGE_VERSION@'
30 # User Friendliness
32 usage() {
33 echo "This program recreates a package using pacman's db and system files"
34 echo "Usage: $myname <installed package name>"
35 echo "Example: $myname kernel26"
38 version() {
39 printf "%s %s\n" "$myname" "$myver"
40 echo 'Copyright (C) 2008 locci <carlocci_at_gmail_dot_com>'
43 if (( $# != 1 )); then
44 usage
45 exit 1
48 if [[ $1 = -@(h|-help) ]]; then
49 usage
50 exit 0
51 elif [[ $1 = -@(V|-version) ]]; then
52 version
53 exit 0
57 # Fakeroot support
59 if (( EUID )); then
60 if [[ -f /usr/bin/fakeroot ]]; then
61 echo "Entering fakeroot environment"
62 export INFAKEROOT="1"
63 /usr/bin/fakeroot -u -- "$0" "$@"
64 exit $?
65 else
66 echo "WARNING: installing fakeroot or running $myname as root is required to"
67 echo " preserve the ownership permissions of files in some packages"
68 echo ""
73 # Setting environmental variables
75 if [[ ! -r @sysconfdir@/pacman.conf ]]; then
76 echo "ERROR: unable to read @sysconfdir@/pacman.conf"
77 exit 1
80 eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
81 pac_db="${DBPath:-@localstatedir@/lib/pacman/}/local"
83 if [[ ! -r @sysconfdir@/makepkg.conf ]]; then
84 echo "ERROR: unable to read @sysconfdir@/makepkg.conf"
85 exit 1
88 source "@sysconfdir@/makepkg.conf"
89 if [[ -r ~/.makepkg.conf ]]; then
90 source ~/.makepkg.conf
93 pkg_arch=${CARCH:-'unknown'}
94 pkg_dest="${PKGDEST:-$PWD}"
95 pkg_pkger=${PACKAGER:-'Unknown Packager'}
97 pkg_name="$1"
98 pkg_dir=("$pac_db/$pkg_name"-+([^-])-+([^-]))
99 pkg_namver=("${pkg_dir[@]##*/}")
102 # Checks everything is in place
104 if [[ ! -d $pac_db ]]; then
105 echo "ERROR: pacman database directory ${pac_db} not found"
106 exit 1
109 if (( ${#pkg_dir[@]} != 1 )); then
110 printf "ERROR: %d entries for package %s found in pacman database\n" \
111 ${#pkg_dir[@]} "${pkg_name}"
112 printf "%s\n" "${pkg_dir[@]}"
113 exit 1
116 if [[ ! -d $pkg_dir ]]; then
117 printf "ERROR: package %s is found in pacman database,\n" "${pkg_name}"
118 printf " but \`%s' is not a directory\n" "${pkg_dir}"
119 exit 1
123 # Begin
125 echo "Package: ${pkg_namver}"
126 work_dir=$(mktemp -d --tmpdir bacman.XXXXXXXXXX)
127 cd "$work_dir" || exit 1
130 # File copying
132 echo "Copying package files..."
134 cat "$pkg_dir"/files |
135 while read i; do
136 if [[ -z $i ]]; then
137 continue
140 if [[ $i == %+([A-Z])% ]]; then
141 current=$i
142 continue
145 case "$current" in
146 %FILES%)
147 ret=0
148 if [[ -e /$i ]]; then
149 bsdtar -cnf - "/$i" 2> /dev/null | bsdtar -xpf -
151 # Workaround to bsdtar not reporting a missing file as an error
152 if ! [[ -e $work_dir/$i || -L $work_dir/$i ]]; then
153 echo ""
154 echo "ERROR: unable to add /$i to the package"
155 echo " If your user does not have permssion to read this file then"
156 echo " you will need to run $myname as root"
157 rm -rf "$work_dir"
158 exit 1
160 else
161 echo ""
162 echo "WARNING: package file /$i is missing"
163 echo ""
166 esac
167 done
169 ret=$?
170 if (( ret )); then
171 rm -rf "$work_dir"
172 exit 1
175 pkg_size=$(du -sk | awk '{print $1 * 1024}')
178 # .PKGINFO stuff
179 # TODO adopt makepkg's write_pkginfo() into this or scripts/library
181 echo Generating .PKGINFO metadata...
182 echo "# Generated by $myname $myver" > .PKGINFO
183 if [[ $INFAKEROOT == "1" ]]; then
184 echo "# Using $(fakeroot -v)" >> .PKGINFO
186 echo "# $(LC_ALL=C date)" >> .PKGINFO
187 echo "#" >> .PKGINFO
189 cat "$pkg_dir"/{desc,files} |
190 while read i; do
191 if [[ -z $i ]]; then
192 continue;
195 if [[ $i == %+([A-Z])% ]]; then
196 current=$i
197 continue
200 case "$current" in
201 # desc
202 %NAME%)
203 echo "pkgname = $i" >> .PKGINFO
205 %VERSION%)
206 echo "pkgver = $i" >> .PKGINFO
208 %DESC%)
209 echo "pkgdesc = $i" >> .PKGINFO
211 %URL%)
212 echo "url = $i" >> .PKGINFO
214 %LICENSE%)
215 echo "license = $i" >> .PKGINFO
217 %ARCH%)
218 echo "arch = $i" >> .PKGINFO
220 %BUILDDATE%)
221 echo "builddate = $(date -u "+%s")" >> .PKGINFO
223 %PACKAGER%)
224 echo "packager = $pkg_pkger" >> .PKGINFO
226 %SIZE%)
227 echo "size = $pkg_size" >> .PKGINFO
229 %GROUPS%)
230 echo "group = $i" >> .PKGINFO
232 %REPLACES%)
233 echo "replaces = $i" >> .PKGINFO
235 %DEPENDS%)
236 echo "depend = $i" >> .PKGINFO
238 %OPTDEPENDS%)
239 echo "optdepend = $i" >> .PKGINFO
241 %CONFLICTS%)
242 echo "conflict = $i" >> .PKGINFO
244 %PROVIDES%)
245 echo "provides = $i" >> .PKGINFO
248 # files
249 %BACKUP%)
250 # strip the md5sum after the tab
251 echo "backup = ${i%%$'\t'*}" >> .PKGINFO
253 esac
254 done
256 comp_files=".PKGINFO"
258 if [[ -f $pkg_dir/install ]]; then
259 cp "$pkg_dir/install" "$work_dir/.INSTALL"
260 comp_files+=" .INSTALL"
262 if [[ -f $pkg_dir/changelog ]]; then
263 cp "$pkg_dir/changelog" "$work_dir/.CHANGELOG"
264 comp_files+=" .CHANGELOG"
268 # Fixes owner:group and permissions for .PKGINFO, .CHANGELOG, .INSTALL
270 chown root:root "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
271 chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
274 # Generate the package
276 echo "Generating the package..."
278 pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
279 ret=0
281 # TODO: Maybe this can be set globally for robustness
282 shopt -s -o pipefail
283 bsdtar -cf - $comp_files * |
284 case "$PKGEXT" in
285 *tar.gz) gzip -c -f -n ;;
286 *tar.bz2) bzip2 -c -f ;;
287 *tar.xz) xz -c -z - ;;
288 *tar.Z) compress -c -f ;;
289 *tar) cat ;;
290 *) echo "WARNING: '%s' is not a valid archive extension." \
291 "$PKGEXT" >&2; cat ;;
292 esac > "${pkg_file}"; ret=$?
294 if (( ret )); then
295 echo "ERROR: unable to write package to $pkg_dest"
296 echo " Maybe the disk is full or you do not have write access"
297 rm -rf "$work_dir"
298 exit 1
301 rm -rf "$work_dir"
303 echo Done
305 exit 0
307 # vim: set ts=2 sw=2 noet: