Fix bash completion for *.pkg.tar
[pacman-ng.git] / contrib / bacman.in
blobc55d716157713e3c7f951bde1cbbdc870afb42dc
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 readonly progname="bacman"
27 readonly progver="0.2.1"
30 # User Friendliness
32 usage() {
33 echo "This program recreates a package using pacman's db and system files"
34 echo "Usage: $progname <installed package name>"
35 echo "Example: $progname kernel26"
38 if (( $# != 1 )); then
39 usage
40 exit 1
43 if [[ $1 == "--help" || $1 == "-h" ]]; then
44 usage
45 exit 0
48 if [[ $1 == "--version" || $1 == "-v" ]]; then
49 echo "$progname version $progver"
50 echo "Copyright (C) 2008 locci"
51 exit 0
55 # Fakeroot support
57 if (( EUID )); then
58 if [[ -f /usr/bin/fakeroot ]]; then
59 echo "Entering fakeroot environment"
60 export INFAKEROOT="1"
61 /usr/bin/fakeroot -u -- "$0" "$@"
62 exit $?
63 else
64 echo "WARNING: installing fakeroot or running ${progname} as root is required to"
65 echo " preserve the ownership permissions of files in some packages"
66 echo ""
71 # Setting environmental variables
73 if [[ ! -r @sysconfdir@/pacman.conf ]]; then
74 echo "ERROR: unable to read @sysconfdir@/pacman.conf"
75 exit 1
78 eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf)
79 pac_db="${DBPath:-@localstatedir@/lib/pacman/}/local"
81 if [[ ! -r @sysconfdir@/makepkg.conf ]]; then
82 echo "ERROR: unable to read @sysconfdir@/makepkg.conf"
83 exit 1
86 source "@sysconfdir@/makepkg.conf"
87 if [[ -r ~/.makepkg.conf ]]; then
88 source ~/.makepkg.conf
91 pkg_arch=${CARCH:-'unknown'}
92 pkg_dest="${PKGDEST:-$PWD}"
93 pkg_pkger=${PACKAGER:-'Unknown Packager'}
95 pkg_name="$1"
96 pkg_dir=("$pac_db/$pkg_name"-+([^-])-+([^-]))
97 pkg_namver=("${pkg_dir[@]##*/}")
100 # Checks everything is in place
102 if [[ ! -d $pac_db ]]; then
103 echo "ERROR: pacman database directory ${pac_db} not found"
104 exit 1
107 if (( ${#pkg_dir[@]} != 1 )); then
108 printf "ERROR: %d entries for package %s found in pacman database\n" \
109 ${#pkg_dir[@]} "${pkg_name}"
110 printf "%s\n" "${pkg_dir[@]}"
111 exit 1
114 if [[ ! -d $pkg_dir ]]; then
115 printf "ERROR: package %s is found in pacman database,\n" "${pkg_name}"
116 printf " but \`%s' is not a directory\n" "${pkg_dir}"
117 exit 1
121 # Begin
123 echo "Package: ${pkg_namver}"
124 work_dir=$(mktemp -d --tmpdir bacman.XXXXXXXXXX)
125 cd "$work_dir" || exit 1
128 # File copying
130 echo "Copying package files..."
132 cat "$pkg_dir"/files |
133 while read i; do
134 if [[ -z $i ]]; then
135 continue
138 if [[ $i == %+([A-Z])% ]]; then
139 current=$i
140 continue
143 case "$current" in
144 %FILES%)
145 ret=0
146 if [[ -e /$i ]]; then
147 bsdtar -cnf - "/$i" 2> /dev/null | bsdtar -xpf -
149 # Workaround to bsdtar not reporting a missing file as an error
150 if ! [[ -e $work_dir/$i || -L $work_dir/$i ]]; then
151 echo ""
152 echo "ERROR: unable to add /$i to the package"
153 echo " If your user does not have permssion to read this file then"
154 echo " you will need to run $progname as root"
155 rm -rf "$work_dir"
156 exit 1
158 else
159 echo ""
160 echo "WARNING: package file /$i is missing"
161 echo ""
164 esac
165 done
167 ret=$?
168 if (( ret )); then
169 rm -rf "$work_dir"
170 exit 1
173 pkg_size=$(du -sk | awk '{print $1 * 1024}')
176 # .PKGINFO stuff
177 # TODO adopt makepkg's write_pkginfo() into this or scripts/library
179 echo Generating .PKGINFO metadata...
180 echo "# Generated by $progname $progver" > .PKGINFO
181 if [[ $INFAKEROOT == "1" ]]; then
182 echo "# Using $(fakeroot -v)" >> .PKGINFO
184 echo "# $(LC_ALL=C date)" >> .PKGINFO
185 echo "#" >> .PKGINFO
187 cat "$pkg_dir"/{desc,files} |
188 while read i; do
189 if [[ -z $i ]]; then
190 continue;
193 if [[ $i == %+([A-Z])% ]]; then
194 current=$i
195 continue
198 case "$current" in
199 # desc
200 %NAME%)
201 echo "pkgname = $i" >> .PKGINFO
203 %VERSION%)
204 echo "pkgver = $i" >> .PKGINFO
206 %DESC%)
207 echo "pkgdesc = $i" >> .PKGINFO
209 %URL%)
210 echo "url = $i" >> .PKGINFO
212 %LICENSE%)
213 echo "license = $i" >> .PKGINFO
215 %ARCH%)
216 echo "arch = $i" >> .PKGINFO
218 %BUILDDATE%)
219 echo "builddate = $(date -u "+%s")" >> .PKGINFO
221 %PACKAGER%)
222 echo "packager = $pkg_pkger" >> .PKGINFO
224 %SIZE%)
225 echo "size = $pkg_size" >> .PKGINFO
227 %GROUPS%)
228 echo "group = $i" >> .PKGINFO
230 %REPLACES%)
231 echo "replaces = $i" >> .PKGINFO
233 %DEPENDS%)
234 echo "depend = $i" >> .PKGINFO
236 %OPTDEPENDS%)
237 echo "optdepend = $i" >> .PKGINFO
239 %CONFLICTS%)
240 echo "conflict = $i" >> .PKGINFO
242 %PROVIDES%)
243 echo "provides = $i" >> .PKGINFO
246 # files
247 %BACKUP%)
248 # strip the md5sum after the tab
249 echo "backup = ${i%%$'\t'*}" >> .PKGINFO
251 esac
252 done
254 comp_files=".PKGINFO"
256 if [[ -f $pkg_dir/install ]]; then
257 cp "$pkg_dir/install" "$work_dir/.INSTALL"
258 comp_files+=" .INSTALL"
260 if [[ -f $pkg_dir/changelog ]]; then
261 cp "$pkg_dir/changelog" "$work_dir/.CHANGELOG"
262 comp_files+=" .CHANGELOG"
266 # Fixes owner:group and permissions for .PKGINFO, .CHANGELOG, .INSTALL
268 chown root:root "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
269 chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
272 # Generate the package
274 echo "Generating the package..."
276 pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
277 ret=0
279 # TODO: Maybe this can be set globally for robustness
280 shopt -s -o pipefail
281 bsdtar -cf - $comp_files * |
282 case "$PKGEXT" in
283 *tar.gz) gzip -c -f -n ;;
284 *tar.bz2) bzip2 -c -f ;;
285 *tar.xz) xz -c -z - ;;
286 *tar.Z) compress -c -f ;;
287 *tar) cat ;;
288 *) echo "WARNING: '%s' is not a valid archive extension." \
289 "$PKGEXT" >&2; cat ;;
290 esac > "${pkg_file}"; ret=$?
292 if (( ret )); then
293 echo "ERROR: unable to write package to $pkg_dest"
294 echo " Maybe the disk is full or you do not have write access"
295 rm -rf "$work_dir"
296 exit 1
299 rm -rf "$work_dir"
301 echo Done
303 exit 0
305 # vim: set ts=2 sw=2 noet: