summaryrefslogtreecommitdiff
blob: 5d3ef8a1463c58d8baa6e47e94c97ef168ccbf7d (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
# -*-eselect-*-  vim: ft=eselect
# Copyright 2009-2022 Gentoo Authors
# Distributed under the terms of the GNU GPL version 2 or later

DESCRIPTION="Manage {,/usr}/bin/awk implementations"
MAINTAINER="base-system@gentoo.org"
VERSION="0.3"

## Functions ##

# find a list of awk symlink targets, best first
find_targets() {
	local t
	for t in \
			"${EROOT}/usr/bin/gawk" \
			"${EROOT}/usr/bin/mawk" \
			"${EROOT}/usr/bin/nawk" \
			"${EROOT}/bin/busybox" \
		; do
		if [[ -x ${t} ]]; then
			echo ${t##*/}
		fi
	done
}

# set the awk symlink
set_symlinks() {
	local target=${1} targets extension dir i rel_target

	[[ ! -L ${EROOT}/bin/awk && -e ${EROOT}/bin/awk ]] && \
		die -q "/bin/awk is not a symlink!"

	[[ ! -L ${EROOT}/usr/bin/awk && -e ${EROOT}/usr/bin/awk ]] && \
		die -q "/usr/bin/awk is not a symlink!"

	if is_number "${target}" && [[ ${target} -ge 1 ]]; then
		targets=( $(find_targets) )
		target=${targets[target-1]}
	fi
	
	if [[ ${target} == "busybox" ]]; then
		dir="${EROOT}/bin"
	else
		dir="${EROOT}/usr/bin"
	fi

	[[ -x ${dir}/${target} ]] || die -q "Target '${target}' doesn't appear to be valid!"
	
	for i in /bin /usr/bin; do
		rel_target=$(relative_name "${dir}/${target}" "${EROOT}${i}")
		ln -sf "${rel_target}" "${EROOT}${i}/awk" || \
			die -q "Couldn't set ${target} ${i}/awk symlink"
	done

	rm -f "${EROOT}"/usr/share/man/man1/awk.1{,.bz2,.gz,.lzma,.xz,.lz,.zst}
	for x in .1{,.gz,.bz2,.lzma,.xz,.lz,.zst}; do
		if [[ -e ${EROOT}/usr/share/man/man1/${target}${x} ]] ; then
			extension="${x}"
			break
		fi
	done

	#busybox has no man page
	if [[ -z "${extension}" ]] ; then
		echo "Couldn't find a man page for ${target}; skipping." 1>&2
		return 1
	fi

	if ! ln -s "${target}${extension}" "${EROOT}/usr/share/man/man1/awk${extension}"; then
		echo "Couldn't create man page symlink for ${target}; skipping." 1>&2
                return 1
        fi
}

### show action ###

describe_show() {
	echo "Show the current awk implementation"
}

do_show() {
	[[ -z ${@} ]] || die -q "Too many parameters"

	write_list_start "Current awk implementation:"
	if [[ -L ${EROOT}/usr/bin/awk ]]; then
		write_kv_list_entry "$(basename $(readlink ${ROOT}/usr/bin/awk))" ""
	elif [[ -e ${EROOT}/usr/bin/awk ]]; then
		write_kv_list_entry "(not a symlink)" ""
	else
		write_kv_list_entry "(unset)" ""
	fi
}

### list action ###

describe_list() {
	echo "List available awk implementations"
}

do_list() {
	[[ -z ${@} ]] || die -q "Too many parameters"

	local i targets

	targets=( $(find_targets) )
	for (( i = 0; i < ${#targets[@]}; i++ )) ; do
		[[ ${targets[${i}]} == $(basename $(readlink "${EROOT}"/usr/bin/awk)) ]] && \
			targets[${i}]=$(highlight_marker "${targets[${i}]}")
	done
	write_list_start "Available awk implementations:"
	write_numbered_list -m '(none found)' "${targets[@]}"
}

### set action ###

describe_set() {
	echo "Set a new awk implementation"
}

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

describe_set_parameters() {
	echo "<target>"
}

do_set() {
	if [[ -z ${1} ]]; then
		die -q "Not enough parameters"
	elif [[ -n ${2} ]]; then
		die -q "Too many parameters"
	else
		set_symlinks "${1}"
	fi
}

### update action ###

describe_update() {
	echo "Automatically update the awk implementation"
}

describe_update_options() {
	echo "ifunset : Do not override existing implementation"
}

do_update() {
	[[ -z ${1} || ( -z ${2} && ( ${1} == ifunset || ${1} == '--if-unset' ) ) ]] || \
		die -q "Usage error"

	[[ ( ${1} == ifunset || ${1} == '--if-unset' ) \
			&& -L ${EROOT}/usr/bin/awk && -x ${EROOT}/usr/bin/awk ]] && \
		return

	set_symlinks 1
}

# vim: set syn=sh :