summaryrefslogtreecommitdiff
path: root/src/pacscripts.sh.in
blob: e4ea8529e732c54bd304cfe9223b0f863f80e0ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
#
#   pacscripts : tries to print out the {pre,post}_{install,remove,upgrade}
#   scripts of a given package
#
#   Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>
#   Copyright (c) 2009-2016 Pacman Development Team <pacman-dev@archlinux.org>
#
#   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 <https://www.gnu.org/licenses/>.
#

# bash options
set -o nounset
set -o errexit

declare -r myname='pacscripts'
declare -r myver='@PACKAGE_VERSION@'

error() {
	local mesg=$1; shift
	printf "==> $(gettext "ERROR:") %s\n ${mesg}" "$@" >&2
}

usage() {
	cat <<EOF
${myname} v${myver}

Print the {pre,post}_{install,remove,upgrade} scripts of a given package.

Usage: ${myname} <pkgname | pkgfile>

Options:
  -h, --help     show this help message and exit
  -V, --version  display version information and exit

Example: ${myname} gconf-editor
Example: ${myname} gconf-editor-3.0.1-3-x86_64.pkg.tar.xz
EOF
}


if ! DBPath="$(pacman-conf DBPath)"; then
	error "unable to read @sysconfdir@/pacman.conf"
	exit 1
fi
pac_db="${DBPath:-@localstatedir@/lib/pacman}/local"

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!"
			error "${myname} requires root privileges. Either install \"sudo\" or run as root."
			exit 1
		else
			sudo pacman "$@"
		fi
	fi
}

print_db() {
	pkg=$(pacman -Q "$1")
	pkg=${pkg/ /-}
	for file in "$pac_db"/"$pkg"*/install; do
		if [ -f "$file" ]; then
			cat "$pac_db/$pkg"*/install
			echo
			return 0
		else
			error "Package $1 does not include any .INSTALL script"
			return 1
		fi
	done
}

print_pkg() {
	if ! bsdtar -xqOf "$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=$(pacman -Sddp "$1")
	if [[ $url != file://* ]]; then
		if ! spacman -Sddw --logfile /dev/null --noconfirm "$1" >&2; then
			error "Failed to download $1"
			return 1
		fi
		echo >&2
		url=$(pacman -Sddp "$1")
	fi
	print_pkg "${url#file://}"
	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