summaryrefslogtreecommitdiff
path: root/src/librefetch/librefetch-install.in
blob: 23c192828e50d69e05d43a97029cde1febc41d49 (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
#!/usr/bin/env bash
# lirefetch-install: (un)install librefetch to /etc/makepkg.conf
#
# Copyright (C) 2013-2015 Luke Shumaker <lukeshu@sbcglobal.net>
#
# License: GNU GPLv3+
#
# This file is part of Parabola.
#
# Parabola 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 3 of the License, or
# (at your option) any later version.
#
# Parabola 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 Parabola. If not, see <http://www.gnu.org/licenses/>.

set -ueE
. "$(librelib messages)"

# This line should be installed
new_code='. @pkgconfdir@/librefetch-makepkg.conf # This line was added by librefetch-install'

# These lines were installed by previous versions of this script
old_code=(
	'# The following line is added by the libretools post_install script'
	'[[ ! -x /usr/bin/librefetch ]] || DLAGENTS+=("libre::/usr/bin/librefetch -p \"\$BUILDFILE\" %u %o")'
	'[[ ! -x /usr/bin/librefetch ]] || DLAGENTS+=({https,libre}"::/usr/bin/librefetch -p \"\$BUILDFILE\" -- %u %o")'
	'DLAGENTS+=({https,libre}"::/usr/bin/librefetch -p $(printf "%q" "$BUILDFILE") -- %u %o")'
	'DLAGENTS+=({https,libre}'\''::/usr/bin/librefetch -p "$BUILDFILE" -- %u %o'\'')'
)

# has_line $file $line
has_line() {
	local file=$1
	local line=$2
	grep -Fxq -- "$line" "$file"
}
# del_line $file $line
del_line() {
	local file=$1
	local line=$2
	local lineno=($(grep -Fxn -- "$line" "$file" | cut -d: -f1))
	if [[ "${#lineno[@]}" -gt 0 ]]; then
		sed -i "$(printf '%dd;' "${lineno[@]}")" "$file"
	fi
}

# post_install $file
post_install() {
	local file=$1
	if grep -q 'librefetch' "$file"; then
		print ":: %s: librefetch is already in %q" "${0##*/}" "$(realpath -s "$file")"
		local line del=false
		for line in "${old_code[@]}"; do
			if has_line "$file" "$line"; then
				print ":: %s: ... but it's an old version" "${0##*/}"
				pre_remove "$file"
				post_install "$file"
				return $?
			fi
		done
	else
		print ":: %s: adding librefetch to %q" "${0##*/}" "$(realpath -s "$file")"
		printf '%s\n' "$new_code" >> "$file"
	fi
}

# pre_remove $file
pre_remove() {
	local file=$1
	print ":: %s: removing librefetch from %q" "${0##*/}" "$(realpath -s "$file")"

	# Check for the old stuff
	sed -ri 's/^#(.*) # commented out by the libretools post_install script/\1/' "$file"
	local line
	for line in "${old_code[@]}"; do
		del_line "$file" "$line"
	done

	# Check for the current stuff
	del_line "$file" "$new_code"
}

usage() {
	print "Usage: %s [install|remove] MAKEPKG_CONF_FILE" "${0##*/}"
	print "Adds or removes librefetch to/from makepkg.conf:DLAGENTS"
}

main() {
	if [[ $# != 2 ]]; then
		usage >&2
		return 1
	fi
	local file=$2
	if [[ ! -f "$file" ]]; then
		error "%s: does not exist: %q" "${0##*/}" "$(realpath -s "$file")"
	fi
	if [[ ! -w "$file" ]]; then
		error "%s: cannot write to file: %q" "${0##*/}" "$(realpath -s "$file")"
	fi
	case "$1" in
		install) post_install "$file";;
		remove) pre_remove "$file";;
		*) usage >&2; return 1;;
	esac
}

main "$@"