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.1"
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
40 if [ "$1" = "--help" -o "$1" = "-h" ] ; then
45 if [ "$1" = "--version" -o "$1" = "-v" ]; then
46 echo "$progname version $progver"
47 echo "Copyright (C) 2008 locci"
54 if [ $EUID -gt 0 ]; then
55 if [ -f /usr/bin/fakeroot ]; then
56 echo "Entering fakeroot environment"
58 /usr/bin/fakeroot -u -- $0 $1
61 echo "WARNING: installing fakeroot or running ${progname} as root is required to"
62 echo " preserve the ownership permissions of files in some packages"
68 # Setting environmental variables
70 if [ ! -r @sysconfdir@/pacman.conf ]; then
71 echo "ERROR: unable to read @sysconfdir@/pacman.conf"
75 eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
76 pac_db="${DBPath:-@localstatedir@/lib/pacman/}/local"
78 if [ ! -r @sysconfdir@/makepkg.conf ]; then
79 echo "ERROR: unable to read @sysconfdir@/makepkg.conf"
83 source "@sysconfdir@/makepkg.conf"
84 if [ -r ~/.makepkg.conf ]; then
85 source ~/.makepkg.conf
88 pkg_arch=${CARCH:-'unknown'}
89 pkg_dest="${PKGDEST:-$PWD}"
90 pkg_pkger=${PACKAGER:-'Unknown Packager'}
93 pkg_dir="$(echo $pac_db/$pkg_name-[0-9]*)"
94 pkg_namver="${pkg_dir##*/}"
97 # Checks everything is in place
99 if [ ! -d "$pac_db" ] ; then
100 echo "ERROR: pacman database directory ${pac_db} not found"
104 if [ ! -d "$pkg_dir" ] ; then
105 echo "ERROR: package ${pkg_name} not found in pacman database"
112 echo Package: ${pkg_namver}
113 work_dir=$(mktemp -d -p /tmp)
114 cd "$work_dir" || exit 1
119 echo "Copying package files..."
121 cat "$pkg_dir"/files |
123 if [ -z "$i" ] ; then
127 if [[ "$i" =~ %[A-Z]*% ]] ; then
135 if [ -e "/$i" ]; then
136 bsdtar -cnf - "/$i" 2> /dev/null | bsdtar -xpf -
138 # Workaround to bsdtar not reporting a missing file as an error
139 if [ ! -e "$work_dir/$i" -a ! -L "$work_dir/$i" ]; then
141 echo "ERROR: unable to add /$i to the package"
142 echo " If your user does not have permssion to read this file then"
143 echo " you will need to run $progname as root"
149 echo "WARNING: package file /$i is missing"
159 if [ $ret -ne 0 ]; then
164 pkg_size=$(du -sk | awk '{print $1 * 1024}')
169 echo Generating .PKGINFO metadata...
170 echo "# Generated by $progname $progver" > .PKGINFO
171 if [ "$INFAKEROOT" = "1" ]; then
172 echo "# Using $(fakeroot -v)" >> .PKGINFO
174 echo "# $(LC_ALL=C date)" >> .PKGINFO
177 cat "$pkg_dir"/{desc,files,depends} |
179 if [[ -z "$i" ]]; then
183 if [[ "$i" =~ %[A-Z]*% ]] ; then
191 echo "pkgname = $i" >> .PKGINFO
194 echo "pkgver = $i" >> .PKGINFO
197 echo "pkgdesc = $i" >> .PKGINFO
200 echo "url = $i" >> .PKGINFO
203 echo "license = $i" >> .PKGINFO
206 echo "arch = $i" >> .PKGINFO
209 echo "builddate = $(date -u "+%s")" >> .PKGINFO
212 echo "packager = $pkg_pkger" >> .PKGINFO
215 echo "size = $pkg_size" >> .PKGINFO
218 echo "group = $i" >> .PKGINFO
221 echo "replaces = $i" >> .PKGINFO
226 # strip the md5sum after the tab
227 echo "backup = ${i%%$'\t'*}" >> .PKGINFO
232 echo "depend = $i" >> .PKGINFO
235 echo "optdepend = $i" >> .PKGINFO
238 echo "conflict = $i" >> .PKGINFO
241 echo "provides = $i" >> .PKGINFO
246 comp_files=".PKGINFO"
248 if [ -f "$pkg_dir/install" ] ; then
249 cp "$pkg_dir/install" "$work_dir/.INSTALL"
250 comp_files+=" .INSTALL"
252 if [ -f $pkg_dir/changelog ] ; then
253 cp "$pkg_dir/changelog" "$work_dir/.CHANGELOG"
254 comp_files+=" .CHANGELOG"
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..."
269 *tar.gz) EXT=${PKGEXT%.gz} ;;
270 *tar.bz2) EXT=${PKGEXT%.bz2} ;;
271 *tar.xz) EXT=${PKGEXT%.xz} ;;
272 *tar) EXT=${PKGEXT} ;;
273 *) echo "WARNING: '%s' is not a valid archive extension." \
274 "$PKGEXT" ; EXT=$PKGEXT ;;
277 pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
280 # when fileglobbing, we want * in an empty directory to expand to
281 # the null string rather than itself
283 # TODO: Maybe this can be set globally for robustness
285 bsdtar -cf - $comp_files * |
287 *tar.gz) gzip -c -f -n ;;
288 *tar.bz2) bzip2 -c -f ;;
289 *tar.xz) xz -c -z - ;;
291 esac > ${pkg_file} || ret=$?
293 if [ $ret -ne 0 ]; then
294 echo "ERROR: unable to write package to $pkg_dest"
295 echo " Maybe the disk is full or you do not have write access"
306 # vim: set ts=2 sw=2 noet: