aboutsummaryrefslogtreecommitdiff
blob: ace28d83a55749333db8c6c8566f943f7091c13f (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
# -*-eselect-*-  vim: ft=eselect
# Copyright (c) 2005-2012 Gentoo Foundation
# $Id$
# This file is part of the 'eselect' tools framework.
#
# eselect 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.
#
# eselect 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
# eselect.  If not, see <http://www.gnu.org/licenses/>.

# store_config file key value PUBLIC
# Stores a $key/$value pair for given module in $configfile
store_config() {
	# we need at least "module" and "key"
	[[ ${#@} -ge 2 ]] || die
	local configfile=${1} key=${2} value content vars line="" changed=0
	shift 2
	value=${@}

	if [[ ! -e ${configfile} ]] ; then
		mkdir -p ${configfile%/*} \
			|| die -q \
			"Couldn't create directory ${configfile%/*}"
	fi

	store_config_header() {
		echo "# Configuration file for eselect" \
			> ${configfile}
		echo "# This file has been automatically generated." \
			>> ${configfile}
	}

	if [[ ! -f ${configfile} ]] ; then
		store_config_header
		echo "${key}=\"${value}\"" \
			>> ${configfile}
		return
	fi

	content=$(<${configfile})

	if [[ -z ${content} ]] ; then
		store_config_header
		echo "${key}=\"${value}\"" \
			>> ${configfile}
		return
	fi

	(
		# parse the names of all settings in the file
		local ifs_save=${IFS-$' \t\n'}
		IFS=$'\n'
		for line in ${content} ; do
			[[ ${line/=/} != ${line} ]] || continue
			line=${line/=*/}
			local ${line}=""
			vars=(${vars[@]} ${line})
		done
		IFS=${ifs_save}

		source ${configfile} 2>&1 > /dev/null \
			|| die "Failed to source ${configfile}."

		store_config_header
		for var in ${vars[@]} ; do
			if [[ ${var} == ${key} ]] ; then
				echo "${var}=\"${value}\"" \
					>> ${configfile}
				changed=1
			else
				echo "${var}=\"${!var}\"" \
					>> ${configfile}
			fi
		done
		[[ ${changed} == 1 ]] \
			|| echo "${key}=\"${value}\"" \
			>> ${configfile}
	)
}

# load_config module key PUBLIC
# Loads $key value from $configfile
load_config() {
	[[ ${#@} -eq 2 ]] || die
	local configfile key value

	configfile=${1}
	key=${2}
	[[ ! -e ${configfile} ]] \
		&& return 1
	value=$(
		unset ${key}
		source ${configfile} 1>&2 > /dev/null \
			|| die "Failed to source ${configfile}."
		echo "${!key}"
	)
	echo "${value}"
}

# append_config file key item ... PUBLIC
# Appends $item to already stored value of $key in $configfile
# if $item is not already part of $key
append_config() {
	[[ ${#@} -gt 2 ]] || die
	local configfile=${1} key=${2} item oldvalue newvalue
	shift 2
	item="$@"
	oldvalue=$(load_config ${configfile} ${key})
	if ! has ${item} ${oldvalue[@]} ; then
		newvalue=( ${oldvalue[@]} ${item} )
		store_config ${configfile} ${key} ${newvalue[@]}
	fi
}