aboutsummaryrefslogtreecommitdiff
blob: 74007092b1ac7997eb23277244cd0e6390712ace (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/bin/bash

# $Header$

# bash replacement for the original euse by Arun Bhanu
# Author: Marius Mauch <genone@gentoo.org>
# Version: 0.2
# Licensed under the GPL v2

PROGRAM_NAME=euse
PROGRAM_VERSION=0.1

MAKE_CONF_PATH=/etc/make.conf
MAKE_GLOBALS_PATH=/etc/make.globals
MAKE_PROFILE_PATH=/etc/make.profile
MAKE_CONF_BACKUP_PATH=/etc/make.conf.euse_backup

[ -z "${MODE}" ] && MODE="showhelp"		# available operation modes: showhelp, showversion, showdesc, showflags, modify

parse_arguments() {
	if [ -z "${1}" ]; then
		return
	fi
	while [ -n "${1}" ]; do
		case "${1}" in
			-h | --help)           MODE="showhelp";;
			-v | --version)        MODE="showversion";;
			-i | --info)           MODE="showdesc";;
			-I | --info-installed) MODE="showinstdesc";;
			-l | --local)          SCOPE="local";;
			-g | --global)         SCOPE="global";;
			-a | --active)         MODE="showflags";;
			-E | --enable)         MODE="modify"; ACTION="add";;
			-D | --disable)        MODE="modify"; ACTION="remove";;
			-P | --prune)          MODE="modify"; ACTION="prune";;
			-*)
				echo "ERROR: unknown option ${1} specified."
				echo
				MODE="showhelp"
				;;
			"%active")
				get_useflags
				ARGUMENTS="${ARGUMENTS} ${ACTIVE_FLAGS[9]}"
				;;
			*)
				ARGUMENTS="${ARGUMENTS} ${1}"
				;;
		esac
		shift
	done
}

error() {
	echo "ERROR: ${1}"
	set +f
	exit 1
}

get_real_path() {
	set -P
	cd "$1"
	pwd
	cd "$OLDPWD"
	set +P
}

check_sanity() {
	# file permission tests
	local descdir
	local make_defaults
	
	descdir="$(get_portdir)/profiles"
	
	[ ! -r "${MAKE_CONF_PATH}" ] && error "${MAKE_CONF_PATH} is not readable"
	[ ! -r "${MAKE_GLOBALS_PATH}" ] && error "${MAKE_GLOBALS_PATH} is not readable"
	[ ! -h "${MAKE_PROFILE_PATH}" ] && error "${MAKE_PROFILE_PATH} is not a symlink"
	[ -z "$(get_portdir)" ] && error "\$PORTDIR couldn't be determined"
	[ ! -d "${descdir}" ] && error "${descdir} does not exist or is not a directory"
	[ ! -r "${descdir}/use.desc" ] && error "${descdir}/use.desc is not readable"
	[ ! -r "${descdir}/use.local.desc" ] && error "${descdir}/use.local.desc is not readable"
	for make_defaults in $(get_all_make_defaults); do
		[ ! -r "$make_defaults" ]  && error "$_make_defaults is not readable"
	done
#	[ ! -r "$(get_make_defaults)" ] && error "$(get_make_defaults) is not readable"
	[ "${MODE}" == "modify" -a ! -w "${MAKE_CONF_PATH}" ] && error ""${MAKE_CONF_PATH}" is not writable"
}

showhelp() {
cat << HELP
${PROGRAM_NAME} v${PROGRAM_VERSION}

Syntax: ${PROGRAM_NAME} <option> [suboptions] [useflaglist]

Options: -h, --help           - show this message
         -v, --version        - show version information
         -i, --info           - show descriptions for the given useflags
         -I, --info-installed - show descriptions for the given useflags and
                                their current impact on the installed system
         -g, --global         - show only global use flags (suboption)
         -l, --local          - show only local use flags (suboption)
         -a, --active         - show currently active useflags and their origin
         -E, --enable         - enable the given useflags
         -D, --disable        - disable the given useflags
         -P, --prune          - remove all references to the given flags from
                                make.conf to revert to default settings

Notes: ${PROGRAM_NAME} currently only works for global flags defined
       in make.globals, make.defaults or make.conf, it doesn't handle
       use.defaults, use.mask or package.use yet (see portage(5) for details on
       these files). It also might have issues with cascaded profiles.
       If multiple options are specified only the last one will be used.
HELP
}

showversion() {
cat << VER
${PROGRAM_NAME} v${PROGRAM_VERSION}
Written by Marius Mauch

Copyright (C) 2004-2008 Gentoo Foundation, Inc.
This is free software; see the source for copying conditions.
VER
}

# remove duplicate flags from the given list in both positive and negative forms
# (but unlike portage always keep the last value even if it's negative)
# Otherwise the status flags could be incorrect if a flag appers multiple times in
# one location (like make.conf).
# Using python here as bash sucks for list handling.
# NOTE: bash isn't actually that bad at handling lists -- sh is. This may be
#       worth another look to avoid calling python unnecessariy. Or we could
#       just write the whole thing in python. ;)
reduce_incrementals() {
	echo $@ | python -c "import sys
r=[]
for x in sys.stdin.read().split():
	if x[0] == '-' and x[1:] in r:
		r.remove(x[1:])
		r.append(x)
	elif x[0] != '-' and '-'+x in r:
		r.remove('-'+x)
		r.append(x)
	elif x == '-*':
		r = []
		r.append(x)
	elif x not in r:
		r.append(x)
print ' '.join(r)" 
}

# the following function creates a bash array ACTIVE_FLAGS that contains the
# global use flags, indexed by origin: 0: environment, 1: make.conf, 
# 2: make.defaults, 3: make.globals
get_useflags() {
	# only calculate once as calling emerge is painfully slow
	[ -n "${USE_FLAGS_CALCULATED}" ] && return
	
	# backup portdir so get_portdir() doesn't give false results later
	portdir_backup="${PORTDIR}"

	ACTIVE_FLAGS[0]="$(reduce_incrementals ${USE})"
	USE=""
	source "${MAKE_CONF_PATH}"
	ACTIVE_FLAGS[1]="$(reduce_incrementals ${USE})"
	USE=""
	for x in $(get_all_make_defaults); do
		source "${x}"
		ACTIVE_FLAGS[2]="$(reduce_incrementals ${ACTIVE_FLAGS[2]} ${USE})"
	done
	USE=""
	source "${MAKE_GLOBALS_PATH}"
	ACTIVE_FLAGS[3]="$(reduce_incrementals ${USE})"

	# restore saved env variables
	USE="${ACTIVE_FLAGS[0]}"
	PORTDIR="${portdir_backup}"

	# get the currently active USE flags as seen by portage, this has to be after
	# restoring USE or portage won't see the original environment
	ACTIVE_FLAGS[9]="$(emerge --info | grep 'USE=' | cut -b 5- | sed -e 's:"::g')" #'
	USE_FLAGS_CALCULATED=1
}

# get the list of all known USE flags by reading use.desc and/or use.local.desc
# (depending on the value of $SCOPE)
get_useflaglist() {
	local descdir

	descdir="$(get_portdir)/profiles"
	
	if [ -z "${SCOPE}" -o "${SCOPE}" == "global" ]; then
		egrep "^[^# ]+ +-" "${descdir}/use.desc" | cut -d\  -f 1
	fi
	if [ -z "${SCOPE}" -o "${SCOPE}" == "local" ]; then
		egrep "^[^# :]+:[^ ]+ +-" "${descdir}/use.local.desc" | cut -d: -f 2 | cut -d\  -f 1
	fi
}

# get all make.defaults by traversing the cascaded profile directories
get_all_make_defaults() {
	local curdir
	local parent
	local rvalue
	
	curdir="${1:-$(get_real_path ${MAKE_PROFILE_PATH})}"
	
	[ -f "${curdir}/make.defaults" ] && rvalue="${curdir}/make.defaults ${rvalue}"
	if [ -f "${curdir}/parent" ]; then
		for parent in $(egrep -v '(^#|^ *$)' ${curdir}/parent); do
			pdir="$(get_real_path ${curdir}/${parent})"
			rvalue="$(get_all_make_defaults ${pdir}) ${rvalue}"
		done
	fi

	echo "${rvalue}"
}

# get the path to make.defaults by traversing the cascaded profile directories
get_make_defaults() {
	local curdir
	local parent
	
	curdir="${1:-$(get_real_path ${MAKE_PROFILE_PATH})}"
	
	if [ ! -f "${curdir}/make.defaults" -a -f "${curdir}/parent" ]; then
		for parent in $(egrep -v '(^#|^ *$)' ${curdir}/parent); do
			if [ -f "$(get_make_defaults ${curdir}/${parent})" ]; then
				curdir="${curdir}/${parent}"
				break
			fi
		done
	fi

	echo "${curdir}/make.defaults"
}

# little helper function to get the status of a given flag in one of the 
# ACTIVE_FLAGS elements. Arguments are 1: flag to test, 2: index of ACTIVE_FLAGS,
# 3: echo value for positive (and as lowercase for negative) test result, 
# 4 (optional): echo value for "missing" test result, defaults to blank
get_flagstatus_helper() {
	if echo " ${ACTIVE_FLAGS[${2}]} " | grep " ${1} " > /dev/null; then
		echo -n "${3}"
	elif echo " ${ACTIVE_FLAGS[${2}]} " | grep " -${1} " > /dev/null; then
		echo -n "$(echo ${3} | tr [[:upper:]] [[:lower:]])"
	else
		echo -n "${4:- }"
	fi
}

# prints a status string for the given flag, each column indicating the presence
# for portage, in the environment, in make.conf, in make.defaults and in make.globals.
# full positive value would be "[+ECDG]", full negative value would be [-ecdg],
# full missing value would be "[-    ]" (portage only sees present or not present)
get_flagstatus() {
	get_useflags

	echo -n '['
	get_flagstatus_helper "${1}" 9 "+" "-"
	get_flagstatus_helper "${1}" 0 "E"
	get_flagstatus_helper "${1}" 1 "C"
	get_flagstatus_helper "${1}" 2 "D"
	get_flagstatus_helper "${1}" 3 "G"
	echo -n '] '
}

# faster replacement to `portageq portdir`
get_portdir() {
	if [ -z "${PORTDIR}" ]; then
		use_backup="${USE}"
		source "${MAKE_GLOBALS_PATH}"
		for x in $(get_all_make_defaults); do
			source "${x}"
		done
		source "${MAKE_CONF_PATH}"
		USE="${use_backup}"
	fi
	echo "${PORTDIR}"
}

# This function takes a list of use flags and shows the status and 
# the description for each one, honoring $SCOPE
showdesc() {
	local descdir
	local current_desc
	local found_one
	local args
	
	args="${*:-*}"
	
	if [ -z "${SCOPE}" ]; then
		SCOPE="global" showdesc ${args}
		echo
		SCOPE="local" showdesc ${args}
		return
	fi
	
	descdir="$(get_portdir)/profiles"
	
	[ "${SCOPE}" == "global" ] && echo "global use flags (searching: ${args})"
	[ "${SCOPE}" == "local" ] && echo "local use flags (searching: ${args})"
	echo "************************************************************"
	
	if [ "${args}" == "*" ]; then
		args="$(get_useflaglist | sort -u)"
	fi
	
	set ${args}
	
	foundone=0
	while [ -n "${1}" ]; do
		if [ "${SCOPE}" == "global" ]; then
			if grep "^${1}  *-" "${descdir}/use.desc" > /dev/null; then
				get_flagstatus "${1}"
				foundone=1
			fi
			grep "^${1}  *-" "${descdir}/use.desc"
		fi
		# local flags are a bit more complicated as there can be multiple 
		# entries per flag and we can't pipe into printf
		if [ "${SCOPE}" == "local" ]; then
			if grep ":${1}  *-" "${descdir}/use.local.desc" > /dev/null; then
				foundone=1
			fi
			grep ":${1}  *-" "${descdir}/use.local.desc" \
				| sed -e "s/^\([^:]\+\):\(${1}\) *- *\(.\+\)/\1|\2|\3/g" \
				| while read line; do
					pkg="$(echo $line | cut -d\| -f 1)"
					flag="$(echo $line | cut -d\| -f 2)"
					desc="$(echo $line | cut -d\| -f 3)"
					get_flagstatus "${flag}"
					printf "%s (%s):\n%s\n\n" "${flag}" "${pkg}" "${desc}"
				done
		fi
		shift
	done
	
	if [ ${foundone} == 0 ]; then
		echo "no matching entries found"
	fi
}

# Works like showdesc() but displays only descriptions of which the appropriate
# ebuild is installed and prints the name of those packages.
showinstdesc() {
	local descdir
	local current_desc
	local args
	local -i foundone=0
	local IFS

	args=("${@:-*}")

	case "${SCOPE}" in
		"global") echo "global use flags (searching: ${args})";;
		 "local") echo "local use flags (searching: ${args})";;
		       *) SCOPE="global" showinstdesc "${args[@]}"
		          echo
		          SCOPE="local" showinstdesc "${args[@]}"
		          return;;
	esac

	descdir="$(get_portdir)/profiles"
	echo "************************************************************"
	
	if [ "${args}" = "*" ]; then
		args="$(get_useflaglist | sort -u)"
	fi
	
	set "${args[@]}"
	
	while [ -n "${1}" ]; do
		case "${SCOPE}" in
			"global")
				if desc=$(grep "^${1}  *-" "${descdir}/use.desc"); then
					get_flagstatus "${1}"
					echo "$desc"
					# get list of installed packages matching this USE flag.
					IFS=$'\n'
					packages=($(equery -q -C hasuse -i "${1}" | awk '{ print $(NF-1) }'))
					foundone+=${#packages[@]}
					printf "\nInstalled packages matching this USE flag: "
					if [ ${foundone} -gt 0 ]; then
						echo $'\n'"${packages[*]}"
					else
						echo "none"
					fi
				fi
			;;
			"local")
				# local flags are a bit more complicated as there can be multiple
				# entries per flag and we can't pipe into printf
				IFS=': ' # Use a space instead of a dash because dashes occur in cat/pkg
				while read pkg flag desc; do
					# print name only if package is installed
					# NOTE: If we implement bug #114086 's enhancement we can just use the
					#       exit status of equery instead of a subshell and pipe to wc -l
					if [ $(equery -q -C list -i -e "${pkg}" | wc -l) -gt 0 ]; then
						foundone=1
						get_flagstatus "${flag}"
						printf "%s (%s):\n%s\n\n" "${flag}" "${pkg}" "${desc#- }"
					fi
				done < <(grep ":${1}  *-" "${descdir}/use.local.desc")
			;;
		esac
		shift
	done
	
	if [ ${foundone} -lt 1 ]; then
		echo "no matching entries found"
	fi
}

# show a list of all currently active flags and where they are activated
showflags() {
	local args

	get_useflags
	
	args="${*:-*}"
	
	if [ "${args}" == "*" ]; then
		args="$(get_useflaglist | sort -u)"
	fi
	
	set ${args}
	
	while [ -n "${1}" ]; do
		if echo " ${ACTIVE_FLAGS[9]} " | grep " ${1} " > /dev/null; then
			printf "%-20s" ${1}
			get_flagstatus ${1}
			echo
		fi
		shift
	done
}

# two small helpers to add or remove a flag from a USE string
add_flag() {
	NEW_MAKE_CONF_USE="${NEW_MAKE_CONF_USE} ${1}"
}

remove_flag() {
	NEW_MAKE_CONF_USE="${NEW_MAKE_CONF_USE// ${1} / }"
}

# USE flag modification function. Mainly a loop with calls to add_flag and 
# remove_flag to create a new USE string which is then inserted into make.conf.
modify() {
	if [ -z "${*}" ]; then
		if [ "${ACTION}" != "prune" ]; then
			echo "WARNING: no USE flags listed for modification, do you really"
			echo "         want to ${ACTION} *all* known USE flags?"
			echo "         If you don't please press Ctrl-C NOW!!!"
			sleep 5
			set $(get_useflaglist | sort -u)
		fi
	fi
	
	get_useflags
	
	NEW_MAKE_CONF_USE=" ${ACTIVE_FLAGS[1]} "
	
	while [ -n "${1}" ]; do
		if [ "${ACTION}" == "add" ]; then
			if echo " ${NEW_MAKE_CONF_USE} " | grep " ${1} " > /dev/null; then
				shift
			elif echo " ${NEW_MAKE_CONF_USE} " | grep " -${1} " > /dev/null; then
				remove_flag "-${1}"
			else
				add_flag "${1}"
				shift
			fi
		elif [ "${ACTION}" == "remove" ]; then
			if echo " ${NEW_MAKE_CONF_USE} " | grep " -${1} " > /dev/null; then
				shift
			elif echo " ${NEW_MAKE_CONF_USE} " | grep " ${1} " > /dev/null; then
				remove_flag "${1}"
			else
				add_flag "-${1}"
				shift
			fi
		elif [ "${ACTION}" == "prune" ]; then
			if echo " ${NEW_MAKE_CONF_USE} " | grep " ${1} " > /dev/null; then
				remove_flag "${1}"
			elif echo " ${NEW_MAKE_CONF_USE} " | grep " -${1} " > /dev/null; then
				remove_flag "-${1}"
			fi
			shift
		fi
	done
	
	#echo "old flags:"
	#echo ${ACTIVE_FLAGS[1]}
	#echo
	#echo "new flags:"
	#echo ${NEW_MAKE_CONF_USE}
	
	# a little loop to add linebreaks so we don't end with one ultra-long line
	NEW_MAKE_CONF_USE_2=""
	for x in ${NEW_MAKE_CONF_USE}; do
		if [ $(((${#NEW_MAKE_CONF_USE_2}%70)+${#x}+2)) -gt 70 ]; then
			NEW_MAKE_CONF_USE_2="${NEW_MAKE_CONF_USE_2}\\ \\n     $x "
		else
			NEW_MAKE_CONF_USE_2="${NEW_MAKE_CONF_USE_2}${x} "
		fi
	done

	# make a backup just in case the user doesn't like the new make.conf
	cp -p "${MAKE_CONF_PATH}" "${MAKE_CONF_BACKUP_PATH}"
	
	# as sed doesn't really work with multi-line patterns we have to replace USE
	# on our own here. Basically just skip everything between USE=" and the 
	# closing ", printing our new USE line there instead.
	inuse=0
	had_use=0
	x=0
	(while [ "$x" -eq "0" ]; do
		read -r line
		x="$?"
		[ "${line:0:4}" == "USE=" ] && inuse=1
		[ "${inuse}" == "0" ] && echo -E "${line}"
		if [ "${inuse}" == "1" ] && echo "${line}" | egrep '" *(#.*)?$' > /dev/null; then
			echo -n 'USE="'
			echo -ne "${NEW_MAKE_CONF_USE_2%% }"
			echo '"'
		 	inuse=0
			had_use=1
		fi
	done
	if [ ${had_use} -eq 0 ]; then
		echo -n 'USE="'
		echo -ne "${NEW_MAKE_CONF_USE_2%% }"
		echo '"'
	fi ) < "${MAKE_CONF_BACKUP_PATH}" | sed -e 's:\\ $:\\:' > "${MAKE_CONF_PATH}"
	
	echo "${MAKE_CONF_PATH} was modified, a backup copy has been placed at ${MAKE_CONF_BACKUP_PATH}"
}

##### main program comes now #####

# disable globbing as it fucks up with args=*
set -f
parse_arguments "$@"
check_sanity

eval ${MODE} ${ARGUMENTS}
set +f