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"
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 /etc
/pacman.conf
]; then
71 echo "ERROR: unable to read /etc/pacman.conf"
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"
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'}
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"
105 if [ ! -d "$pkg_dir" ] ; then
106 echo "ERROR: package ${pkg_name} not found in pacman database"
113 echo Package
: ${pkg_namver}
114 work_dir
=$
(mktemp
-d -p /tmp
)
115 cd "$work_dir" ||
exit 1
120 echo "Copying package files..."
122 cat "$pkg_dir"/files |
124 if [ -z "$i" ] ; then
128 if [[ "$i" =~
%[A-Z
]*% ]] ; then
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
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"
150 echo "WARNING: package file /$i is missing"
160 if [ $ret -ne 0 ]; then
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"
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
185 cat "$pkg_dir"/{desc
,files
,depends
} |
187 if [[ -z "$i" ]]; then
191 if [[ "$i" =~
%[A-Z
]*% ]] ; then
199 echo "pkgname = $i" >> .PKGINFO
202 echo "pkgver = $i" >> .PKGINFO
205 echo "pkgdesc = $i" >> .PKGINFO
208 echo "url = $i" >> .PKGINFO
211 echo "license = $i" >> .PKGINFO
214 echo "arch = $i" >> .PKGINFO
217 echo "builddate = $(date -u "+%s
")" >> .PKGINFO
220 echo "packager = $pkg_pkger" >> .PKGINFO
223 echo "size = $pkg_size" >> .PKGINFO
226 echo "group = $i" >> .PKGINFO
229 echo "replaces = $i" >> .PKGINFO
232 echo "force = true" >> .PKGINFO
237 # strip the md5sum after the tab
238 echo "backup = ${i%%$'\t'*}" >> .PKGINFO
243 echo "depend = $i" >> .PKGINFO
246 echo "optdepend = $i" >> .PKGINFO
249 echo "conflict = $i" >> .PKGINFO
252 echo "provides = $i" >> .PKGINFO
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 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"
283 # vim: set ts=2 sw=2 noet: