Move important information up in -Si output
[pacman-ng.git] / contrib / bacman.sh.in
blob7895a40c6a236ecf2db3743fa98f60b59019841f
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 # 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/>.
24 shopt -s extglob
25 shopt -s nullglob
27 declare -r myname='bacman'
28 declare -r myver='@PACKAGE_VERSION@'
31 # User Friendliness
33 usage() {
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"
39 version() {
40 printf "%s %s\n" "$myname" "$myver"
41 echo 'Copyright (C) 2008 locci <carlocci_at_gmail_dot_com>'
44 if (( $# != 1 )); then
45 usage
46 exit 1
49 if [[ $1 = -@(h|-help) ]]; then
50 usage
51 exit 0
52 elif [[ $1 = -@(V|-version) ]]; then
53 version
54 exit 0
58 # Fakeroot support
60 if (( EUID )); then
61 if [[ -f /usr/bin/fakeroot ]]; then
62 echo "Entering fakeroot environment"
63 export INFAKEROOT="1"
64 /usr/bin/fakeroot -u -- "$0" "$@"
65 exit $?
66 else
67 echo "WARNING: installing fakeroot or running $myname as root is required to"
68 echo " preserve the ownership permissions of files in some packages"
69 echo ""
74 # Setting environmental variables
76 if [[ ! -r @sysconfdir@/pacman.conf ]]; then
77 echo "ERROR: unable to read @sysconfdir@/pacman.conf"
78 exit 1
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"
86 exit 1
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'}
97 pkg_name="$1"
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"
106 exit 1
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[@]}"
113 exit 1
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}"
119 exit 1
123 # Begin
125 echo "Package: ${pkg_namver}"
126 work_dir=$(mktemp -d --tmpdir bacman.XXXXXXXXXX)
127 cd "$work_dir" || exit 1
130 # File copying
132 echo "Copying package files..."
134 cat "$pkg_dir"/files |
135 while read i; do
136 if [[ -z $i ]]; then
137 continue
140 if [[ $i == %+([A-Z])% ]]; then
141 current=$i
142 continue
145 case "$current" in
146 %FILES%)
147 ret=0
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
153 echo ""
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"
157 rm -rf "$work_dir"
158 exit 1
160 else
161 echo ""
162 echo "WARNING: package file /$i is missing"
163 echo ""
166 esac
167 done
169 ret=$?
170 if (( ret )); then
171 rm -rf "$work_dir"
172 exit 1
175 pkg_size=$(du -sk | awk '{print $1 * 1024}')
178 # .PKGINFO stuff
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
187 echo "#" >> .PKGINFO
189 while read i; do
190 if [[ -z $i ]]; then
191 continue;
194 if [[ $i == %+([A-Z])% ]]; then
195 current=$i
196 continue
199 case "$current" in
200 # desc
201 %NAME%)
202 echo "pkgname = $i" >> .PKGINFO
204 %VERSION%)
205 echo "pkgver = $i" >> .PKGINFO
207 %DESC%)
208 echo "pkgdesc = $i" >> .PKGINFO
210 %URL%)
211 echo "url = $i" >> .PKGINFO
213 %LICENSE%)
214 echo "license = $i" >> .PKGINFO
216 %ARCH%)
217 echo "arch = $i" >> .PKGINFO
218 pkg_arch="$i"
220 %BUILDDATE%)
221 echo "builddate = $(date -u "+%s")" >> .PKGINFO
223 %PACKAGER%)
224 echo "packager = $pkg_pkger" >> .PKGINFO
226 %SIZE%)
227 echo "size = $pkg_size" >> .PKGINFO
229 %GROUPS%)
230 echo "group = $i" >> .PKGINFO
232 %REPLACES%)
233 echo "replaces = $i" >> .PKGINFO
235 %DEPENDS%)
236 echo "depend = $i" >> .PKGINFO
238 %OPTDEPENDS%)
239 echo "optdepend = $i" >> .PKGINFO
241 %CONFLICTS%)
242 echo "conflict = $i" >> .PKGINFO
244 %PROVIDES%)
245 echo "provides = $i" >> .PKGINFO
248 # files
249 %BACKUP%)
250 # strip the md5sum after the tab
251 echo "backup = ${i%%$'\t'*}" >> .PKGINFO
253 esac
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}"
279 ret=0
281 # TODO: Maybe this can be set globally for robustness
282 shopt -s -o pipefail
283 bsdtar -cf - $comp_files * |
284 case "$PKGEXT" in
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 ;;
289 *tar) cat ;;
290 *) echo "WARNING: '%s' is not a valid archive extension." \
291 "$PKGEXT" >&2; cat ;;
292 esac > "${pkg_file}"; ret=$?
294 if (( ret )); then
295 echo "ERROR: unable to write package to $pkg_dest"
296 echo " Maybe the disk is full or you do not have write access"
297 rm -rf "$work_dir"
298 exit 1
301 rm -rf "$work_dir"
303 echo Done
305 exit 0
307 # vim: set ts=2 sw=2 noet: