summaryrefslogtreecommitdiff
path: root/src/lib/conf.sh
blob: 67aeb9635253c6169342eb7a69e0740897c11139 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash -euE
#!/bin/bash
#   Copyright (c) 2012-2013 by Luke Shumaker <lukeshu@sbcglobal.net>
#
#   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 3 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 <http://www.gnu.org/licenses/>.

INCLUDE_CONF_SH=conf.sh

LIBREUSER="${SUDO_USER:-$USER}"
if [[ $LIBREUSER == $USER ]]; then
	LIBREHOME=$HOME
else
	LIBREHOME="$(eval echo ~$LIBREUSER)"
fi
if [[ -z ${XDG_CONFIG_HOME:-} ]]; then
	export XDG_CONFIG_HOME="${LIBREHOME}/.config"
fi
if [[ -z ${XDG_CACHE_HOME:-} ]]; then
	export XDG_CACHE_HOME="${LIBREHOME}/.cache"
fi

# Generic functions ############################################################

# Usage: list_files $slug
# Lists the configuration files to be considered for $slug.
# Later files should take precedence over earlier files.
list_files() {
	local slug=$1
	case $slug in
		abs)
			echo /etc/$slug.conf
			echo "$LIBREHOME/.$slug.conf"
			;;
		makepkg)
			if [[ ${MAKEPKG_CONF:-} != /etc/$slug.conf && -r ${MAKEPKG_CONF:-} ]]; then
				echo "$MAKEPKG_CONF"
			else
				echo /etc/$slug.conf
				echo "$LIBREHOME/.$slug.conf"
			fi
			;;
		libretools)
			echo /etc/$slug.conf
			echo "$XDG_CONFIG_HOME/libretools/$slug.conf"
			;;
		*)
			echo /etc/libretools.d/$slug.conf
			echo "$XDG_CONFIG_HOME/libretools/$slug.conf"
			;;
	esac
}

# Usage: list_envvars $slug
# Lists the environmental variables that take precidence over the configuration
# files for $slug.
list_envvars() {
	local slug=$1
	case $slug in
		makepkg)
			printf '%s\n' \
				PKGDEST SRCDEST SRCPKGDEST LOGDEST \
				BUILDDIR \
				PKGEXT SRCEXT \
				GPGKEY PACKAGER
			;;
		*) :;;
	esac
}

# Usage: load_files $slug
# Loads the configuration files for $slug in the proper order.
load_files() {
	local slug=$1
	local var
	local file

	# Save the existing versions at _VARNAME
	for var in $(list_envvars $slug); do
		[[ -n ${!var:-} ]] && eval "_$var=\${$var}"
	done

	# Load the files
	for file in $(list_files $slug); do
		if [[ -r $file ]]; then
			. "$file"
		fi
	done

	# Restore the _SAVED versions
	for var in $(list_envvars $slug); do
		eval "$var=\${_$var:-\${$var:-}}"
	done
}

# Usage: check_vars $slug VAR1 VAR2...
# Check whether the variables listed are properly set.
# If not, it prints a message saying to set them in the configuration file(s)
# for $slug.
check_vars() {
	local slug=$1
	shift
	local ret=0
	local VAR
	for VAR in "$@"; do
		if [[ -z ${!VAR:-} ]]; then
			if [[ $(list_files $slug|wc -l) -gt 1 ]]; then
				echo "Configure '$VAR' in one of:"
				list_files $slug | sed 's/./  -> &/'
			else
				echo "Configure '$VAR' in $(list_files $slug)"
			fi
			ret=1
		fi
	done >&2
	if [[ $ret != 0 ]]; then
		return 1
	fi
}

# makepkg configuration ########################################################

# Usage: get_conf_makepkg <var_name> <default_value>
get_conf_makepkg() (
	set +euE
	local setting=$1
	local default=$2
	load_files makepkg
	printf '%s\n' "${!setting:-${default}}"
)

set_conf_makepkg() {
	local key=$1
	local val=$2
	local file
	for file in $(list_files makepkg|tac); do
		if [[ -w $file ]]; then
			sed -i "/^\s*$key=/d" "$file"
			echo "$key='$val'" >> "$file"
			return 0
		fi
	done
	return 1
}