summaryrefslogtreecommitdiff
blob: d7f90f0466af8f0a4e1ad23b75ce898a3e948efe (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*-eselect-*-  vim: ft=eselect
# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

DESCRIPTION="Manage fontconfig /etc/fonts/conf.d symlinks"
AUTHOR="cardoe@gentoo.org"
MAINTAINER="fonts@gentoo.org"
VERSION=20220403

find_targets() {
	local bc bcdirs=()

	for bc in /etc/fonts/conf.avail ${ES_FONTCONFIG_DIRS}; do
		bcdirs+=("${EROOT}${bc}/*.conf")
	done

	for bc in ${bcdirs[@]}; do
		[[ -e ${bc} ]] && echo "${bc##*/}"
	done | sort -u
}

is_enabled() {
	[[ -e ${EROOT}/etc/fonts/conf.d/${1} ]]
}

### list action ###

describe_list() {
	echo "List available fontconfig .conf files"
}

do_list() {
	local n targets=($(find_targets))
	write_list_start \
		"Available fontconfig .conf files ($(highlight '*') is enabled):"

	for (( n = 0; n < ${#targets[@]}; ++n )); do
		is_enabled "${targets[n]}" &&
			targets[n]=$(highlight_marker "${targets[n]}")
	done
	write_numbered_list -m "(none found)" "${targets[@]}"
}

### enable action ###

describe_enable() {
	echo "Enable specified fontconfig .conf file(s)"
}

describe_enable_parameters() {
	echo "<target>"
}

describe_enable_options() {
	echo "<target> : Target name or number (from 'list' action)"
}

do_enable() {
	local bc bcdir="${EROOT}/etc/fonts/conf.d" x

	[[ -z ${1} ]] && die -q "You didn't specify any .conf files to enable"

	# create directory if necessary
	if [[ ! -d ${bcdir} && -w ${bcdir%/*} ]] ; then
		mkdir ${bcdir} || die -q "Failed to create ${bcdir}"
	elif [[ ! -d ${bcdir} ]] ; then
		die -q "You don't have permission to create ${bcdir}"
	fi

	# make sure we have proper permissions
	[[ -w ${bcdir} ]] ||
		die -q "You don't have permission to write to ${bcdir}"

	local targets=($(find_targets))

	for bc; do
		local file target=${bc}

		is_number "${target}" &&
			target=${targets[${target} - 1]}

		[[ -z ${target} ]] &&
			die -q "Target \"${bc}\" doesn't appear to be valid!"

		bc=${target}

		# ignore any unrecognized options
		[[ ${bc} == --* ]] && continue

		# what form is the argument in?
		case ${bc} in
			# absolute path
			/*)
				file="${EROOT}${bc}"
				;;
			# relative path
			*/*)
				file="${EROOT}${PWD}/${bc}"
				;;
			# no path
			*)
				[[ -n ${ES_FONTCONFIG_DIRS} ]] &&
				for x in ${ES_FONTCONFIG_DIRS}; do
					[[ -f ${EROOT}${x}/${bc} ]] && file="${EROOT}${x}/${bc}" && break
				done || file="${EROOT}/etc/fonts/conf.avail/${bc}"
				;;
		esac

		# does it exist?
		if [[ ! -e ${file} ]]; then
			write_error_msg "${file} doesn't exist"
			continue
		fi

		# already installed?
		if [[ -e ${bcdir}/${bc##*/} ]]; then
			write_error_msg "${bc##*/} is already installed"
			continue
		fi

		# finally, create the symlink
		ln -rs "${file}" "${bcdir}" ||
			die -q "Failed to create symlink from \"${file}\" to \"${bcdir}\""
	done
}

### disable action ###

describe_disable() {
	echo "Disable specified fontconfig .conf file(s)"
}

describe_disable_parameters() {
	echo "<target>"
}

describe_disable_options() {
	echo "<target> : Target name or number (from 'list' action)"
}


do_disable() {
	local bc bcdir="${EROOT}/etc/fonts/conf.d"

	[[ -z ${1} ]] && die -q "You didn't specify any .conf files to disable"

	local targets=($(find_targets))

	for bc; do
		local file target=${bc}

		is_number "${target}" &&
			target=${targets[${target} - 1]}

		[[ -z ${target} ]] &&
			die -q "Target \"${bc}\" doesn't appear to be valid!"

		bc=${target}
		file="${bcdir}/${bc}"

		# ignore any unrecognized options
		[[ ${bc} == --* ]] && continue

		# is in installed?
		if [[ ! -e ${file} ]]; then
			write_error_msg "${bc} is not installed"
			continue
		fi

		# remove it if we have permissions
		if [[ -w ${file%/*} ]]; then
			rm "${file}" || die -q "Failed to remove ${file}"
		else
			die -q "You don't have permission to remove ${file}"
		fi
	done
}