1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-11-05 09:05:03 -05:00
pacman/contrib/pacscripts.in
Lukas Fleischer a77e638c77 contrib/*: Support the "--help" and "--version" options
Add "--help"/"-h" and "--version"/"-V" support to all contrib scripts.
Also, update scripts that used "-v" as a short option for "--version"
and use "-V" for the sake of consistency.

Additionally:

* Move version and usage messages to separate convenience functions in
  all scripts.

* Add a workaround to paccache to support "--help" and "--version". This
  should be replaced by a proper POSIX-compliant command line parser
  that supports long options in a future patch.

* Add a "$myver" variable to all scripts and use it whenever we refer to
  the program version (e.g. in version messages). Also, use the pacman
  version number everywhere instead of using a different versioning
  scheme for each contrib script. This is achieved by adding a
  "PACKAGE_VERSION" placeholder that is replaced by sed(1) when the
  script is built.

* Ensure we always return with exit status 0 if "--help" is used and
  return with exit status 1 if we display the usage message due to
  invalid arguments.

* Add "AUTOMAKE_OPTIONS = std-options" and add all scripts to
  "bin_SCRIPTS" to make `make installcheck` check that installed scripts
  actually support the "--help" and "--version" options.

Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
Signed-off-by: Dan McGee <dan@archlinux.org>
2011-12-23 14:49:46 -06:00

139 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
#
# pacscripts : tries to print out the {pre,post}_{install,remove,upgrade}
# scripts of a given package
#
# Copyright (c) 2009 Giulio "giulivo" Fidente <giulivo.navigante@gmail.com>
# Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# bash options
set -o nounset
set -o errexit
declare -r myname='pacscripts'
declare -r myver='@PACKAGE_VERSION@'
conf="@sysconfdir@/pacman.conf"
if [ ! -r "$conf" ]; then
echo "ERROR: unable to read $conf"
exit 1
fi
eval $(awk '/DBPath/ {print $1$2$3}' "$conf")
eval $(awk '/CacheDir/ {print $1$2$3}' "$conf")
pac_db="${DBPath:-@localstatedir@/lib/pacman}/local"
pac_cache="${CacheDir:-@localstatedir@/cache/pacman/pkg}"
error() {
local mesg=$1; shift
printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
}
usage() {
echo "This program prints out the {pre,post}_{install,remove,upgrade} scripts"
echo "of a given package."
echo "Usage: $myname pkgname|pkgfile"
echo
echo " OPTIONS:"
echo " -h, --help Print this help message"
echo " -v, --version Print program name and version"
echo
echo "Example: $myname gconf-editor"
echo "Example: $myname gconf-editor-2.24.1-1-x86_64.pkg.tar.gz"
}
version() {
printf "%s %s\n" "$myname" "$myver"
echo 'Copyright (c) 2009 Giulio "giulivo" Fidente <giulivo.navigante@gmail.com>'
echo 'Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>'
}
spacman() {
if [ $EUID -eq 0 ]; then
pacman "$@"
else
if ! type -p sudo; then
error "Cannot find the sudo binary! Is sudo installed?"
error "Otherwise try to run the program as root"
exit 1
else
sudo pacman "$@"
fi
fi
}
print_db() {
pkg=$(pacman -Q "$1")
pkg=${pkg/ /-}
if [ -f $pac_db/$pkg*/install ]; then
cat $pac_db/$pkg*/install
echo
return 0
else
error "Package $1 does not include any .INSTALL script"
return 1
fi
}
print_pkg() {
if ! bsdtar -xOf "$1" .INSTALL 2>/dev/null; then
error "Package $1 does not include any .INSTALL script"
return 1
fi
echo
}
print_scriptlet() {
if [ -f "$1" ]; then
if bsdtar tf "$1" .PKGINFO &>/dev/null; then
print_pkg "$1"
return
fi
fi
if pacman -Q "$1" &>/dev/null; then
print_db "$1"
return
fi
if ! pacman -Si $1 &>/dev/null; then
error "Package $1 not found"
return 1
fi
url=$(spacman -Sdp $1 | tail -n1)
filename=$(basename $url)
if [ ! -f "$pac_cache/$filename" ]; then
if ! spacman -Sdw --noconfirm $1 >&2; then
error "Failed to download $1"
return 1
fi
echo >&2
fi
print_pkg "$pac_cache/$filename"
return
}
if [ $# -ne 1 ] ; then
usage
exit 1
fi
case "$1" in
--help|-h) usage; exit 0 ;;
--version|-V) version; exit 0 ;;
*) print_scriptlet $1 ;;
esac