autoclean.sh: abide by POSIX shebang
[pacman-ng.git] / contrib / pacscripts.in
blob84687145118fb6e5b66468cd0e4b883a53ffeac0
1 #!/bin/bash
3 # pacscripts : tries to print out the {pre,post}_{install,remove,upgrade}
4 # scripts of a given package
6 # Copyright (c) 2009 Giulio "giulivo" Fidente <giulivo.navigante@gmail.com>
7 # Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.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 # bash options
24 set -o nounset
25 set -o errexit
27 declare -r myname='pacscripts'
28 declare -r myver='@PACKAGE_VERSION@'
30 conf="@sysconfdir@/pacman.conf"
32 if [ ! -r "$conf" ]; then
33 echo "ERROR: unable to read $conf"
34 exit 1
37 eval $(awk '/DBPath/ {print $1$2$3}' "$conf")
38 eval $(awk '/CacheDir/ {print $1$2$3}' "$conf")
39 pac_db="${DBPath:-@localstatedir@/lib/pacman}/local"
40 pac_cache="${CacheDir:-@localstatedir@/cache/pacman/pkg}"
42 error() {
43 local mesg=$1; shift
44 printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
47 usage() {
48 echo "This program prints out the {pre,post}_{install,remove,upgrade} scripts"
49 echo "of a given package."
50 echo "Usage: $myname pkgname|pkgfile"
51 echo
52 echo " OPTIONS:"
53 echo " -h, --help Print this help message"
54 echo " -v, --version Print program name and version"
55 echo
56 echo "Example: $myname gconf-editor"
57 echo "Example: $myname gconf-editor-2.24.1-1-x86_64.pkg.tar.gz"
60 version() {
61 printf "%s %s\n" "$myname" "$myver"
62 echo 'Copyright (c) 2009 Giulio "giulivo" Fidente <giulivo.navigante@gmail.com>'
63 echo 'Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>'
66 spacman() {
67 if [ $EUID -eq 0 ]; then
68 pacman "$@"
69 else
70 if ! type -p sudo; then
71 error "Cannot find the sudo binary! Is sudo installed?"
72 error "Otherwise try to run the program as root"
73 exit 1
74 else
75 sudo pacman "$@"
80 print_db() {
81 pkg=$(pacman -Q "$1")
82 pkg=${pkg/ /-}
83 if [ -f $pac_db/$pkg*/install ]; then
84 cat $pac_db/$pkg*/install
85 echo
86 return 0
87 else
88 error "Package $1 does not include any .INSTALL script"
89 return 1
93 print_pkg() {
94 if ! bsdtar -xOf "$1" .INSTALL 2>/dev/null; then
95 error "Package $1 does not include any .INSTALL script"
96 return 1
98 echo
101 print_scriptlet() {
102 if [ -f "$1" ]; then
103 if bsdtar tf "$1" .PKGINFO &>/dev/null; then
104 print_pkg "$1"
105 return
108 if pacman -Q "$1" &>/dev/null; then
109 print_db "$1"
110 return
112 if ! pacman -Si $1 &>/dev/null; then
113 error "Package $1 not found"
114 return 1
116 url=$(spacman -Sdp $1 | tail -n1)
117 filename=$(basename $url)
118 if [ ! -f "$pac_cache/$filename" ]; then
119 if ! spacman -Sdw --noconfirm $1 >&2; then
120 error "Failed to download $1"
121 return 1
123 echo >&2
125 print_pkg "$pac_cache/$filename"
126 return
129 if [ $# -ne 1 ] ; then
130 usage
131 exit 1
134 case "$1" in
135 --help|-h) usage; exit 0 ;;
136 --version|-V) version; exit 0 ;;
137 *) print_scriptlet $1 ;;
138 esac