summaryrefslogtreecommitdiff
blob: 967be72913b651238db9ca4d6c6f672d48ab77db (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
# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit output path-manipulation

DESCRIPTION="Manage /usr/bin/yacc symlink"
MAINTAINER="base-system@gentoo.org"
VERSION="0.1"
YACC_SYMLINK=${ROOT}/usr/bin/yacc

# find_targets
# returns available targets for symlink
# Note: first match is best match for do_update command
find_targets() {
	local f
	for f in "${ROOT}/usr/bin/"{yacc.bison,yacc.yacc} ; do
		[[ -x $f ]] && echo "${f}"
	done
}

# is_active $target
# returns true if ${YACC_SYMLINK} points to $target
is_active() {
	[[ ${#@} -eq 1 ]] || die "Need exactly 1 argument!"
	[[ -L ${YACC_SYMLINK} && $(canonicalise ${YACC_SYMLINK}) -ef ${1} ]]
}

# is_updateable
# returns true if ${YACC_SYMLINK} could be updated
# That means it does either is symlink or does not exist
is_updateable() {
	[[ -L ${YACC_SYMLINK} || ! -a ${YACC_SYMLINK} ]]
}

# safe_set_symlink $target
# does some safety checks and points $YACC_SYMLINK to $target
safe_set_symlink() {
	[[ ${#@} -eq 1 ]] || die "Need exactly 1 argument!"
	[[ -x ${1} ]] || die "${1} is not executable!"
	is_updateable || die "${YACC_SYMLINK} exists but is not symlink!"
	ln -sf "${1}" "${YACC_SYMLINK}" || \
		die "Failed to set symlink! How could this happen? :("
}

### list action ###

describe_list() {
	echo "List available targets for ${YACC_SYMLINK} symlink can point to"
}

do_list() {
	local targets i=0 active

	targets=( $(find_targets) )
	
	write_list_start "Available possibilities for ${YACC_SYMLINK} symlink can point to:"
	write_list_start "( $(highlight '*') - designates where symlink points now)"
	if [[ -n ${targets[@]} ]] ; then
		for (( i = 0 ; i < ${#targets[@]} ; i = i + 1 )) ; do
			active=''
			is_active "${targets[${i}]}" && active=' *'
			write_numbered_list_entry ${i} "${targets[${i}]}$(highlight "${active}")"
		done
	else
		write_kv_list_entry "(none found)" ""
	fi

	return 0
}

### show action ###

describe_show() {
	echo "Show where ${YACC_SYMLINK} currently points to"
}

do_show() {
	if [[ -L ${YACC_SYMLINK} ]]; then
		echo $(canonicalise "${YACC_SYMLINK}")
	else
		write_warning_msg "${YACC_SYMLINK} is not a symlink!"
	fi
}

### set action ###

describe_set() {
	echo "Set ${YACC_SYMLINK} symlink"
}

describe_set_parameters() {
	echo "<target>"
}

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

do_set() {
	[[ ${#@} == 1 ]] || \
		die -q "Please specify exactly one version to activate!"

	local target targets i=0

	targets=( $(find_targets) )
	
	if is_number "${1}"; then
		target=${targets[${1}]}
	else
		for (( i = 0 ; i < ${#targets[@]} ; i = i + 1 )) ; do
			[[ ${targets[${i}]} == ${1} ]] && \
				target=${1}
		done
	fi

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

	safe_set_symlink ${target}
}

### update action ###

describe_update() {
	echo "Atomaticaly detect providers for ${YACC_SYMLINK} symlink and set it"
}

do_update() {
	local target targets i=0

	targets=( $(find_targets) )
	if [[ -n ${targets[@]} ]] ; then
		for (( i = 0 ; i < ${#targets[@]} ; i = i + 1 )) ; do
			[[ ${targets[${i}]} -ef $(canonicalise "${YACC_SYMLINK}") ]] && \
				return 0
		done
		safe_set_symlink ${targets[0]}
	else
		write_warning_msg "No targets exist to update symlink. Removing ${YACC_SYMLINK} if exist."
		write_warning_msg "Ignore this warning if you removed all yacc providers."
		is_updateable || \
			die -q "${YACC_SYMLINK} is not symlink. I'd better not remove it!"
		[[ -L ${YACC_SYMLINK} ]] && rm -r ${YACC_SYMLINK}
	fi
}

# vim: set ft=eselect :