Rehash efficiently
[pacman-ng.git] / contrib / pacscripts.in
blobd366409134d4a73bb3f83b63af1ec4b167fff54d
1 #!@BASH_SHELL@
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 progname=$(basename $0)
28 progver="0.4"
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: $progname 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: $progname gconf-editor"
57         echo "Example: $progname gconf-editor-2.24.1-1-x86_64.pkg.tar.gz"
60 spacman() {
61         if [ $EUID -eq 0 ]; then
62                 pacman "$@"
63         else
64                 if [ ! "$(type -p sudo)" ]; then
65                         error "Cannot find the sudo binary! Is sudo installed?"
66                         error "Otherwise try to run the program as root"
67                         exit 1
68                 else
69                         sudo pacman "$@"
70                 fi
71         fi
74 print_db() {
75         pkg=$(pacman -Q "$1")
76         pkg=${pkg/ /-}
77         if [ -f $pac_db/$pkg*/install ]; then
78                 cat $pac_db/$pkg*/install
79                 echo
80                 return 0
81         else
82                 error "Package $1 does not include any .INSTALL script"
83                 return 1
84         fi
87 print_pkg() {
88         if ! bsdtar -xOf "$1" .INSTALL 2>/dev/null; then
89                 error "Package $1 does not include any .INSTALL script"
90                 return 1
91         fi
92         echo
95 print_scriptlet() {
96         if [ -f "$1" ]; then
97                 if bsdtar tf "$1" .PKGINFO &>/dev/null; then
98                         print_pkg "$1"
99                         return
100                 fi
101         fi
102         if pacman -Q "$1" &>/dev/null; then
103                 print_db "$1"
104                 return
105         fi
106         if ! pacman -Si $1 &>/dev/null; then
107                 error "Package $1 not found"
108                 return 1
109         fi
110         url=$(spacman -Sdp $1 | tail -n1)
111         filename=$(basename $url)
112         if [ ! -f "$pac_cache/$filename" ]; then
113                 if ! spacman -Sdw --noconfirm $1 >&2; then
114                         error "Failed to download $1"
115                         return 1
116                 fi
117                 echo >&2
118         fi
119         print_pkg "$pac_cache/$filename"
120         return
123 if [ $# -ne 1 ] ; then
124         usage
125         exit 1
128 case "$1" in
129         --help|-h) usage; exit 0 ;;
130         --version|-v) echo "$progname version $progver"; exit 0 ;;
131         *) print_scriptlet $1 ;;
132 esac