summaryrefslogtreecommitdiff
path: root/libre-testing/libretools/libretools.install
blob: 202456feababe78726f60715b9854628118646a8 (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
#!/bin/sh

_edit_code="          'https::/usr/bin/curl -fLC - --retry 3 --retry-delay 3 -o %o %u'"
_edit_cmnt='#%s # commented out by the libretools post_install script'

_add_cmnt='# The following line is added by the libretools post_install script'
_add_code='DLAGENTS+=({https,libre}"::/usr/bin/librefetch -p $(printf "%q" "$BUILDFILE") -- %u %o")'

# These lines were installed by previous versions of this script
_old_code=(
	'[[ ! -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")'
)

# has_line $line
has_line() { grep -Fxq -- "$1" etc/makepkg.conf; }
# add_line $line
add_line() { printf '%s\n' "$1" >> etc/makepkg.conf; }
# del_line $line
del_line() {
	local lineno=($(grep -Fxn -- "$1" etc/makepkg.conf | cut -d: -f1))
	if [[ "${#lineno[@]}" -gt 0 ]]; then
		sed -i "$(printf '%dd;' "${lineno[@]}")" etc/makepkg.conf;
	fi
}
# rep_line $orig $replacement_bre
rep_line() {
	local lineno=($(grep -Fxn -- "$1" etc/makepkg.conf | cut -d: -f1))
	if [[ "${#lineno[@]}" == 1 ]]; then
		sed -i "${lineno}s|.*|${2//|/\\|}|" etc/makepkg.conf;
	fi
}

# arg 1:  the new package version
post_install() {
	if grep -q 'librefetch' etc/makepkg.conf; then
		libremessages msg2 "libretools: librefetch is already in /etc/makepkg.conf"
		local line del=false
		for line in "${_old_code[@]}"; do
			if has_line "$line"; then
				pre_remove
				post_install
				return $?
			fi
		done
	else
		libremessages msg2 "libretools: adding librefetch to /etc/makepkg.conf"
		rep_line "$_edit_code" "$(printf "$_edit_cmnt" '&')"
		add_line "$_add_cmnt"
		add_line "$_add_code"
	fi
}

## arg 1:  the new package version
## arg 2:  the old package version
post_upgrade() {
	post_install
}

pre_remove() {
	libremessages msg2 "libretools: removing librefetch from /etc/makepkg.conf"

	rep_line "$(printf "$_edit_cmnt" "$_edit_code")" "$_edit_code"
	del_line "$_add_cmnt"
	del_line "$_add_code"

	local line
	for line in "${_old_code[@]}"; do
		del_line "$line"
	done
}