summaryrefslogtreecommitdiff
blob: c258c220ecad232ba3d5df39dd2cc5aff948d61f (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
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: mysql.eselect,v 1.3 2006/01/07 22:40:38 vivo Exp $

DESCRIPTION="Manage the /usr/lib/*mysql* links"
MAINTAINER="vivo@gentoo.org"
SVN_DATE='$Date: 2006/01/07 22:40:38 $'
VERSION=$(svn_date_to_version "${SVN_DATE}" )
MYSQL_SYMLINK_CHECKDIR="${ROOT}/usr/sbin"
ERR_NOT_FOUND="(not-found)"
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 lis
	# "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} ]] || [[ -f "${filename}.gz" ]] \
		|| [[ -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}
}

# 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 linkname2
	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}")"
		for linkname2 in "${linkname}" "${linkname}.gz" ; do
			if [[ -a "${linkname2}" ]] ; then
				# rm needed for directory
				rm -f "${linkname/"-${version}"/}" &> /dev/null
				ln -sf "${linkname2}" "${linkname2/"-${version}"/}"
				[[ ! -L "${linkname2/"-${version}"/}" ]] \
				&& write_warning_msg "FAILED: cd \"${dir}\" && ln -sf \"${linkname2}\" \"${linkname2/"-${version}"/}\""
			fi
		done
	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}")"
}

# vim: set ft=eselect :