summaryrefslogtreecommitdiff
blob: a34e581ed91eb6f92f70ce81dba40d2b158de572 (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
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: mysql.eselect,v 1.6 2006/02/01 15:32:32 vivo Exp $

DESCRIPTION="Manage the /usr/lib/*mysql* links"
MAINTAINER="vivo@gentoo.org"
SVN_DATE='$Date: 2006/02/01 15:32:32 $'
VERSION=$(svn_date_to_version "${SVN_DATE}" )
MYSQL_SYMLINK_CHECKDIR="${ROOT}/usr/sbin"
ERR_NOT_FOUND="(not-found)"
# "unset" is grepped by the MySQL ebuild so don't touch it
ERR_UNSET="(unset)"

# return a human oriented version
#
# $1 - version to transform
function version4human() {
	local major=${1:0:1}
	local minor=${1:1:2}
	[[ ${minor:0:1} == "0" ]] && minor=${minor:1:1}
	echo "${major}.${minor}.x"
}

# return a integer version
#
# $1 - version to transform

function version4bash() {
	local minor=${1#*.}
	minor=${minor%%.x*}
	[[ ${#minor} -eq 1 ]] && minor="0${minor}"
	echo "${1%%.*}${minor}"
}

# Return the versions currently installed
find_versions() {
	local versions list
	# "eselect mysql set 1" will always set the best version
	list=${MYSQL_SYMLINK_CHECKDIR}/mysqld-*
	[[ ${list##/} == mysqld-* ]] && return
	for x in ${MYSQL_SYMLINK_CHECKDIR}/mysqld-* ; do
		versions=( $(version4human ${x##*-}) ${versions[@]} )
	done
	echo ${versions[@]}
}

# Check if a symlink could be created for a specific file
#
# $1 - original file name
# $2 - MySQL version
check_symlink() {
	local filename=${1}
	local version=${2}
	local linkname=${filename/"-${version}"/}
	local ret=0

	[[ -z ${filename} ]] || [[ -z ${version} ]] && return 1

	# check link source
	[[ -f ${filename} ]] || [[ -d ${filename} ]] || ret=$(( ${ret} + 1 ))

	# check link targe
	# not checking for directory (want to keep "/etc/mysql")
	[[ -a ${linkname} ]] && [[ ! -L ${linkname} ]] && [[ ! -d ${linkname} ]] \
		&& ret=$(( ${ret} +2 ))

	return ${ret}
}

# another one inherited from versionator.eclass (version_sort)
# THERE IS A COPY OF THIS ONE IN MYSQL_FX.ECLASS, keep the two synced
mysql_make_file_list() {
	local items= left=0
	items=$( ls -d ${1}-[[:digit:]][[:digit:]][[:digit:]] )

	while [[ ${left} -lt ${#items[@]} ]] ; do
		local lowest_idx=${left}
		local idx=$(( ${lowest_idx} + 1 ))
		while [[ ${idx} -lt ${#items[@]} ]] ; do
			[[ "${items[${lowest_idx}]}" > "${items[${idx}]}" ]] \
				&& lowest_idx=${idx}
			idx=$(( ${idx} + 1 ))
		done
		local tmp=${items[${lowest_idx}]}
		items[${lowest_idx}]=${items[${left}]}
		items[${left}]=${tmp}
		left=$(( ${left} + 1 ))
	done
	echo ${items[@]}
}

# THERE IS A COPY OF THIS ONE IN ESELECT-MYSQL, keep the two synced
mysql_choose_better_version() {
	local items= better="" i
	items="$( ls -d ${1}-[[:digit:]][[:digit:]][[:digit:]] )"
	for i in ${items} ; do
		if [[ "${i}" > "${better}" ]] ; then
			better="${i}"
		fi
	done
	echo "${better}"
}

# void mysql_lib_symlinks()
#
# To be called on the live filesystem, reassign symlinks to each mysql
# library to the best version avaiable
# 2005-12-30 <vivo at gentoo.org>
# THERE IS A COPY OF THIS ONE IN MYSQL_FX.ECLASS, keep the two synced
mysql_lib_symlinks() {
	local d dirlist maxdots soname sonameln other better
	pushd "${ROOT}/usr/lib" &> /dev/null
		# dirlist must contain the less significative directory left
		dirlist="mysql $( mysql_make_file_list mysql )"

		# waste some time in removing and recreating symlinks
		for d in $dirlist ; do
			for soname in $(find "${d}" -name "*.so*" -and -not -type "l") ; do
				# maxdot is a limit versus infinite loop
				maxdots=0
				sonameln=${soname##*/}
				# loop in version of the library to link it, similar to the
				# libtool work
				while [[ ${sonameln:0-3} != '.so' ]] && [[ ${maxdots} -lt 6 ]]
				do
					rm -f "${sonameln}"
					ln -s "${soname}" "${sonameln}"
					(( ++maxdots ))
					sonameln="${sonameln%.*}"
				done
				rm -f "${sonameln}"
				ln -s "${soname}" "${sonameln}"
			done
		done
	popd &> /dev/null

	# "include"s and "mysql_config", needed to compile other sw
	for other in "/usr/lib/mysql" "/usr/include/mysql" "/usr/bin/mysql_config" ; do
		pushd "${ROOT}${other%/*}" &> /dev/null
		better=$( mysql_choose_better_version "${other##*/}" )
		if ! [[ -d "${other##*/}" ]] ; then
			[[ -L "${other##*/}" ]] && rm -f "${other##*/}"
			! [[ -f "${other##*/}" ]] && ln -sf "${better}" "${other##*/}"
		else
			[[ -L "${other##*/}" ]] && rm -f "${other##*/}"
			! [[ -d "${other##*/}" ]] && ln -s "${better}" "${other##*/}"
		fi
		popd &> /dev/null
	done
}


# work out which MYSQL version the user has chosen from the list
#
# $1: the list id that the user has chosen
get_version_fromlist() {
	local version=${1}
	local -a versions=( $(find_versions) )
	
	if is_number ${version} ; then
		if [[ ${version} -gt 0 ]] && [[ ${version} -le ${#versions[@]} ]] ; then
			version=${versions[(( ${version} - 1 ))]}
			echo "${version}"
			return
		fi
	fi

	for (( i = 0 ; i < ${#versions[@]} ; i = i + 1 )) ; do
		[[ ${versions[$i]} == ${version} ]] || continue
			echo ${version}
			return
	done

	echo "${ERR_NOT_FOUND}"
}

get_current_version() {
	link=$(readlink ${MYSQL_SYMLINK_CHECKDIR}/mysqld)
	version=${link##*-}

	if ! is_number "${version}" || [[ "${version}" == "" ]] ; then
		version="${ERR_UNSET}"
		echo "${version}"
	else
		echo $(version4human "${version}")
	fi
}

set_current_version() {

	local version=${1}
	is_number "${version}" || die -q "No usable version: ${version}"
	local CWD=$(pwd)
	local lastdir dir linkname
	local list=$(< $ROOT/var/lib/eselect/mysql/mysql-${version}.filelist )

	# perform a check on the entire file lis
	for filename in $list ; do
		dir="${ROOT}/$(dirname "${filename}")"
		if [[ "${dir}" != "${lastdir}" ]] ; then
			cd "${dir}"
			lastdir="${dir}"
		fi
		check_symlink "$(basename "${filename}")" "${version}"
		ret=$?
		if [[ $ret -ne 0 ]] ; then
			cd "${CWD}"
			die -q \
			"File \"${ROOT}/${filename}\" is not suitable for symlink (${ret})"
		fi
	done

	# it's ok to do the effective work now
	for filename in $list ; do
		dir="${ROOT}/$(dirname "${filename}")"
		if [[ "${dir}" != "${lastdir}" ]] ; then
			cd "${dir}"
			lastdir="${dir}"
		fi
		linkname="$(basename "${filename}")"
		if [[ -a "${linkname}" ]] ; then
			# rm needed for directory
			rm -f "${linkname/"-${version}"/}" &> /dev/null
			ln -sf "${linkname}" "${linkname/"-${version}"/}"
			[[ ! -L "${linkname/"-${version}"/}" ]] \
			&& write_warning_msg "FAILED: cd \"${dir}\" && ln -sf \"${linkname}\" \"${linkname/"-${version}"/}\""
		fi
	done

	cd "${CWD}"
}

remove_current_version() {

	local version=${1}
	is_number "${version}" || die -q "No usable version: ${version}"
	local CWD=$(pwd)
	local lastdir dir linkname
	local list=$(< $ROOT/var/lib/eselect/mysql/mysql-${version}.filelist )
	
	for filename in $list ; do
		linkname="${ROOT}/${filename/"-${version}"/}"
		[[ -L "${linkname}" ]] && rm -f "${linkname}"
	done

	cd "${CWD}"
}

### show action ###

describe_show() {
	echo "Show the current default version for MySQL"
}

do_show() {
	write_list_start "Current default MySQL version:"
	version=$(get_current_version)
	write_kv_list_entry "$version" ""
}

### list action ###

describe_list() {
	echo "List available MySQL versions to link to"
}

do_list() {
	versions=( $(find_versions) )
	current=$(get_current_version)
	
	write_list_start "Available MySQL versions:"

	if [[ -n "${versions[@]}" ]] ; then
		local i
		for (( i = 0 ; i < ${#versions[@]} ; i = i + 1 )) ; do
			[[ ${versions[${i}]} == ${current} ]] && \
				versions[${i}]="${versions[${i}]} $(highlight '*' )"
		done
		write_numbered_list "${versions[@]}"
	else
		write_kv_list_entry "(none found)" ""
	fi
}

### set action ###

describe_set() {
	echo "Set MYSQL version used"
}

do_set() {
	[[ ${#@} -lt 1 ]] &&
		die -q "Wrong count of paramters."
	local version
	version=$(get_version_fromlist ${1})

	if [[ ${version} == "${ERR_NOT_FOUND}" ]] ; then
		die -q "I don't recognise that version of MYSQL"
	fi

	set_current_version "$(version4bash "${version}")"
	mysql_lib_symlinks
}

### remove action ###

describe_remove() {
	echo "Remove symlinks of the default server"
}

do_remove() {

	local version=$(readlink "${MYSQL_SYMLINK_CHECKDIR}/mysqld")
	version=$(version4human "${version##*-}")

	if [[ -z "${version}" ]] ; then
		die -q "No symlinks found"
	fi

	remove_current_version "$(version4bash "${version}")"
	mysql_lib_symlinks
}

# vim: set ft=eselect :