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/>.
26 readonly progname
="bacman"
27 readonly progver
="0.2.1"
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
43 if [[ $1 == "--help" ||
$1 == "-h" ]]; then
48 if [[ $1 == "--version" ||
$1 == "-v" ]]; then
49 echo "$progname version $progver"
50 echo "Copyright (C) 2008 locci"
58 if [[ -f /usr
/bin
/fakeroot
]]; then
59 echo "Entering fakeroot environment"
61 /usr
/bin
/fakeroot
-u -- "$0" "$@"
64 echo "WARNING: installing fakeroot or running ${progname} as root is required to"
65 echo " preserve the ownership permissions of files in some packages"
71 # Setting environmental variables
73 if [[ ! -r @sysconfdir@
/pacman.conf
]]; then
74 echo "ERROR: unable to read @sysconfdir@/pacman.conf"
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"
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'}
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"
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[@]}"
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}"
123 echo "Package: ${pkg_namver}"
124 work_dir
=$
(mktemp
-d --tmpdir bacman.XXXXXXXXXX
)
125 cd "$work_dir" ||
exit 1
130 echo "Copying package files..."
132 cat "$pkg_dir"/files |
138 if [[ $i == %+([A-Z
])% ]]; then
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
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"
160 echo "WARNING: package file /$i is missing"
173 pkg_size
=$
(du
-sk |
awk '{print $1 * 1024}')
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
187 cat "$pkg_dir"/{desc
,files
} |
193 if [[ $i == %+([A-Z
])% ]]; then
201 echo "pkgname = $i" >> .PKGINFO
204 echo "pkgver = $i" >> .PKGINFO
207 echo "pkgdesc = $i" >> .PKGINFO
210 echo "url = $i" >> .PKGINFO
213 echo "license = $i" >> .PKGINFO
216 echo "arch = $i" >> .PKGINFO
219 echo "builddate = $(date -u "+%s
")" >> .PKGINFO
222 echo "packager = $pkg_pkger" >> .PKGINFO
225 echo "size = $pkg_size" >> .PKGINFO
228 echo "group = $i" >> .PKGINFO
231 echo "replaces = $i" >> .PKGINFO
234 echo "depend = $i" >> .PKGINFO
237 echo "optdepend = $i" >> .PKGINFO
240 echo "conflict = $i" >> .PKGINFO
243 echo "provides = $i" >> .PKGINFO
248 # strip the md5sum after the tab
249 echo "backup = ${i%%$'\t'*}" >> .PKGINFO
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}"
279 # TODO: Maybe this can be set globally for robustness
281 bsdtar
-cf - $comp_files * |
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 ;;
288 *) echo "WARNING: '%s' is not a valid archive extension." \
289 "$PKGEXT" >&2; cat ;;
290 esac > "${pkg_file}"; ret
=$?
293 echo "ERROR: unable to write package to $pkg_dest"
294 echo " Maybe the disk is full or you do not have write access"
305 # vim: set ts=2 sw=2 noet: