makepkg: do not bail on failure to install built package
[pacman-ng.git] / contrib / bacman
blob22940ecbf2ac1795ec64447f5d85676d7c7b97d3
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 readonly progname="bacman"
24 readonly progver="0.2.0"
27 # User Friendliness
29 function usage(){
30 echo "This program recreates a package using pacman's db and system files"
31 echo "Usage: $progname <installed package name>"
32 echo "Example: $progname kernel26"
35 if [ $# -ne 1 ] ; then
36 usage
37 exit 1
40 if [ "$1" = "--help" -o "$1" = "-h" ] ; then
41 usage
42 exit 0
45 if [ "$1" = "--version" -o "$1" = "-v" ]; then
46 echo "$progname version $progver"
47 echo "Copyright (C) 2008 locci"
48 exit 0
52 # Fakeroot support
54 if [ $EUID -gt 0 ]; then
55 if [ -f /usr/bin/fakeroot ]; then
56 echo "Entering fakeroot environment"
57 export INFAKEROOT="1"
58 /usr/bin/fakeroot -u -- $0 $1
59 exit $?
60 else
61 echo "WARNING: installing fakeroot or running ${progname} as root is required to"
62 echo " preserve the ownership permissions of files in some packages"
63 echo ""
68 # Setting environmental variables
70 if [ ! -r /etc/pacman.conf ]; then
71 echo "ERROR: unable to read /etc/pacman.conf"
72 exit 1
75 eval $(awk '/DBPath/ {print $1$2$3}' /etc/pacman.conf)
76 pac_db="${DBPath:-/var/lib/pacman/}/local"
78 if [ ! -r /etc/makepkg.conf ]; then
79 echo "ERROR: unable to read /etc/makepkg.conf"
80 exit 1
83 source "/etc/makepkg.conf"
84 if [ -r ~/.makepkg.conf ]; then
85 source ~/.makepkg.conf
88 pkg_arch=${CARCH:-'unknown'}
89 pkg_dest="${PKGDEST:-$PWD}"
90 pkg_ext=${PKGEXT:-'.pkg.tar.gz'}
91 pkg_pkger=${PACKAGER:-'Unknown Packager'}
93 pkg_name="$1"
94 pkg_dir="$(echo $pac_db/$pkg_name-[0-9]*)"
95 pkg_namver="${pkg_dir##*/}"
98 # Checks everything is in place
100 if [ ! -d "$pac_db" ] ; then
101 echo "ERROR: pacman database directory ${pac_db} not found"
102 exit 1
105 if [ ! -d "$pkg_dir" ] ; then
106 echo "ERROR: package ${pkg_name} not found in pacman database"
107 exit 1
111 # Begin
113 echo Package: ${pkg_namver}
114 work_dir=$(mktemp -d -p /tmp)
115 cd "$work_dir" || exit 1
118 # File copying
120 echo "Copying package files..."
122 cat "$pkg_dir"/files |
123 while read i; do
124 if [ -z "$i" ] ; then
125 continue
128 if [[ "$i" =~ %[A-Z]*% ]] ; then
129 current=$i
130 continue
133 case $current in
134 %FILES%)
135 ret=0
136 if [ -e "/$i" ]; then
137 bsdtar -cnf - "/$i" 2> /dev/null | bsdtar -xpf -
139 # Workaround to bsdtar not reporting a missing file as an error
140 if [ ! -e "$work_dir/$i" ] && [ -L "$work_dir/$i"]; then
141 echo ""
142 echo "ERROR: unable to add /$i to the package"
143 echo " If your user does not have permssion to read this file then"
144 echo " you will need to run $progname as root"
145 rm -rf "$work_dir"
146 exit 1
148 else
149 echo ""
150 echo "WARNING: package file /$i is missing"
151 echo ""
156 esac
157 done
159 ret=$?
160 if [ $ret -ne 0 ]; then
161 rm -rf "$work_dir"
162 exit 1
165 pkg_size=$(du -sk | awk '{print $1 * 1024}')
167 if [ -f "$pkg_dir/install" ] ; then
168 cp "$pkg_dir/install" "$work_dir/.INSTALL"
170 if [ -f $pkg_dir/changelog ] ; then
171 cp "$pkg_dir/changelog" "$work_dir/.CHANGELOG"
175 # .PKGINFO stuff
177 echo Generating .PKGINFO metadata...
178 echo "# Generated by $progname $progver" > .PKGINFO
179 if [ "$INFAKEROOT" = "1" ]; then
180 echo "# Using $(fakeroot -v)" >> .PKGINFO
182 echo "# $(LC_ALL=C date)" >> .PKGINFO
183 echo "#" >> .PKGINFO
185 cat "$pkg_dir"/{desc,files,depends} |
186 while read i; do
187 if [[ -z "$i" ]]; then
188 continue;
191 if [[ "$i" =~ %[A-Z]*% ]] ; then
192 current=$i
193 continue
196 case "$current" in
197 # desc
198 %NAME%)
199 echo "pkgname = $i" >> .PKGINFO
201 %VERSION%)
202 echo "pkgver = $i" >> .PKGINFO
204 %DESC%)
205 echo "pkgdesc = $i" >> .PKGINFO
207 %URL%)
208 echo "url = $i" >> .PKGINFO
210 %LICENSE%)
211 echo "license = $i" >> .PKGINFO
213 %ARCH%)
214 echo "arch = $i" >> .PKGINFO
216 %BUILDDATE%)
217 echo "builddate = $(date -u "+%s")" >> .PKGINFO
219 %PACKAGER%)
220 echo "packager = $pkg_pkger" >> .PKGINFO
222 %SIZE%)
223 echo "size = $pkg_size" >> .PKGINFO
225 %GROUPS%)
226 echo "group = $i" >> .PKGINFO
228 %REPLACES%)
229 echo "replaces = $i" >> .PKGINFO
231 %FORCE%)
232 echo "force = true" >> .PKGINFO
235 # files
236 %BACKUP%)
237 # strip the md5sum after the tab
238 echo "backup = ${i%%$'\t'*}" >> .PKGINFO
241 # depends
242 %DEPENDS%)
243 echo "depend = $i" >> .PKGINFO
245 %OPTDEPENDS%)
246 echo "optdepend = $i" >> .PKGINFO
248 %CONFLICTS%)
249 echo "conflict = $i" >> .PKGINFO
251 %PROVIDES%)
252 echo "provides = $i" >> .PKGINFO
254 esac
255 done
258 # Fixes owner:group and permissions for .PKGINFO, .CHANGELOG, .INSTALL
260 chown root:root "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
261 chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
264 # Generate the package
266 echo "Generating the package..."
268 ret=0
269 bsdtar -czf "$pkg_dest/$pkg_namver-$pkg_arch$pkg_ext" $(ls -A) || ret=$?
270 if [ $ret -ne 0 ]; then
271 echo "ERROR: unable to write package to $pkg_dest"
272 echo " Maybe the disk is full or you do not have write access"
273 rm -rf "$work_dir"
274 exit 1
277 rm -rf "$work_dir"
279 echo Done
281 exit 0
283 # vim: set ts=2 sw=2 noet: