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 # Copyright (c) 2008 locci <carlocci_at_gmail_dot_com>
8 # Copyright (c) 2008-2012 Pacman Development Team <pacman-dev@archlinux.org>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 declare -r myname
='bacman'
28 declare -r myver
='@PACKAGE_VERSION@'
34 echo "This program recreates a package using pacman's db and system files"
35 echo "Usage: $myname <installed package name>"
36 echo "Example: $myname kernel26"
40 printf "%s %s\n" "$myname" "$myver"
41 echo 'Copyright (C) 2008 locci <carlocci_at_gmail_dot_com>'
44 if (( $# != 1 )); then
49 if [[ $1 = -@
(h|
-help) ]]; then
52 elif [[ $1 = -@
(V|
-version) ]]; then
61 if [[ -f /usr
/bin
/fakeroot
]]; then
62 echo "Entering fakeroot environment"
64 /usr
/bin
/fakeroot
-u -- "$0" "$@"
67 echo "WARNING: installing fakeroot or running $myname as root is required to"
68 echo " preserve the ownership permissions of files in some packages"
74 # Setting environmental variables
76 if [[ ! -r @sysconfdir@
/pacman.conf
]]; then
77 echo "ERROR: unable to read @sysconfdir@/pacman.conf"
81 eval $
(awk '/DBPath/ {print $1$2$3}' @sysconfdir@
/pacman.conf
)
82 pac_db
="${DBPath:-@localstatedir@/lib/pacman/}/local"
84 if [[ ! -r @sysconfdir@
/makepkg.conf
]]; then
85 echo "ERROR: unable to read @sysconfdir@/makepkg.conf"
89 source "@sysconfdir@/makepkg.conf"
90 if [[ -r ~
/.makepkg.conf
]]; then
91 source ~
/.makepkg.conf
94 pkg_dest
="${PKGDEST:-$PWD}"
95 pkg_pkger
=${PACKAGER:-'Unknown Packager'}
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"
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[@]}"
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}"
125 echo "Package: ${pkg_namver}"
126 work_dir
=$
(mktemp
-d --tmpdir bacman.XXXXXXXXXX
)
127 cd "$work_dir" ||
exit 1
132 echo "Copying package files..."
134 cat "$pkg_dir"/files |
140 if [[ $i == %+([A-Z
])% ]]; then
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
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"
162 echo "WARNING: package file /$i is missing"
175 pkg_size
=$
(du
-sk |
awk '{print $1 * 1024}')
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
194 if [[ $i == %+([A-Z
])% ]]; then
202 echo "pkgname = $i" >> .PKGINFO
205 echo "pkgver = $i" >> .PKGINFO
208 echo "pkgdesc = $i" >> .PKGINFO
211 echo "url = $i" >> .PKGINFO
214 echo "license = $i" >> .PKGINFO
217 echo "arch = $i" >> .PKGINFO
221 echo "builddate = $(date -u "+%s
")" >> .PKGINFO
224 echo "packager = $pkg_pkger" >> .PKGINFO
227 echo "size = $pkg_size" >> .PKGINFO
230 echo "group = $i" >> .PKGINFO
233 echo "replaces = $i" >> .PKGINFO
236 echo "depend = $i" >> .PKGINFO
239 echo "optdepend = $i" >> .PKGINFO
242 echo "conflict = $i" >> .PKGINFO
245 echo "provides = $i" >> .PKGINFO
250 # strip the md5sum after the tab
251 echo "backup = ${i%%$'\t'*}" >> .PKGINFO
254 done < <(cat "$pkg_dir"/{desc
,files
})
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}"
281 # TODO: Maybe this can be set globally for robustness
283 bsdtar
-cf - $comp_files * |
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 ;;
290 *) echo "WARNING: '%s' is not a valid archive extension." \
291 "$PKGEXT" >&2; cat ;;
292 esac > "${pkg_file}"; ret
=$?
295 echo "ERROR: unable to write package to $pkg_dest"
296 echo " Maybe the disk is full or you do not have write access"
307 # vim: set ts=2 sw=2 noet: