aboutsummaryrefslogtreecommitdiff
blob: 8b7bdd99caf8f9de4b30c8d1fdeeda65c19287de (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
# -*-eselect-*-  vim: ft=eselect
# Copyright 2005-2014 Gentoo Foundation
# Distributed under the terms of the GNU GPL version 2 or later

inherit multilib tests

DESCRIPTION="Manage active wine version"
MAINTAINER=""

VARIANTS=(staging)

PREFIXDIR=/usr/lib
BINDIR=/usr/bin
EXECUTABLES=(msiexec notepad regedit regsvr32 widl wine wine64 wine64-preloader
wineboot winebuild winecfg wineconsole winecpp winedbg winefile wineg++ winegcc
winemine winepath wine-preloader wineserver wmc wrc)
MANDIR=/usr/share/man
MANPAGES=(man1/widl.1 man1/winebuild.1 man1/winegcc.1 man1/wineg++.1
man1/winecpp.1 man1/wineserver.1 man1/wine.1 man1/wmc.1 man1/wrc.1
man1/msiexec.1 man1/notepad.1 man1/regedit.1 man1/regsvr32.1 man1/wineboot.1
man1/winecfg.1 man1/wineconsole.1 man1/winedbg.1 man1/winefile.1
man1/winemine.1 man1/winepath.1)
DESKTOPFILES=(wine.desktop)
DESKTOPDIR=/usr/share/applications

# sort function for versions, to be used in a pipe
sort_versions() {
	local vsort="sort --version-sort"
	# Test if our sort supports the --version-sort option (posix does)
	${vsort} </dev/null &>/dev/null || vsort=sort

	# We sort versions as follows:
	# 1. Run sed to prepend the version string by the numeric version
	#      1.7.21 wine-1.7.21
	#      1.7.14 wine-compholio-1.7.14
	# 2. sort --version-sort
	# 3. Run sed again to remove the prepended keys from step 1.
	sed -e 's/^\(.\+-\)\([[:digit:].]*\)$/\2 &/' \
		| LC_ALL=C ${vsort} | sed 's/.* //'
}

# find a list of available wine versions
find_targets() {
	local f
	for f in "${EROOT}${PREFIXDIR}"/wine-*; do
		[[ -f ${f}/bin/wine ]] && echo "${f##*/}"
	done | sort_versions
}

find_variant_targets() {
	local variant=$1
	[[ -n $variant ]] || die "\$variant is empty :("
	local f
	for f in "${EROOT}${PREFIXDIR}/wine-${variant}-"*; do
		[[ -f ${f}/bin/wine ]] && echo "${f##*/}"
	done | sort_versions
}

# lifted from fdo-mime.eclass
desktop_database_update() {
	if [[ -x "${EPREFIX}/usr/bin/update-desktop-database" ]]; then
		echo "Updating desktop mime database ..."
		"${EPREFIX}/usr/bin/update-desktop-database" -q "${EROOT}usr/share/applications"
	fi
}

# remove wine symlinks
remove_symlinks() {
	for exe in "${EXECUTABLES[@]}"; do
		if [[ -L ${EROOT}${BINDIR}/${exe} ]]; then
			rm "${EROOT}${BINDIR}/${exe}" || return 1
		fi
	done

	for page in "${MANPAGES[@]}"; do
		if [[ -L ${EROOT}/${MANDIR}/${page} ]]; then
			rm "${EROOT}/${MANDIR}/${page}" || return 1
		fi
	done
	for d in "${DESKTOPFILES[@]}"; do
		if [[ -L ${EROOT}/${DESKTOPDIR}/${d} ]]; then
			rm "${EROOT}/${DESKTOPDIR}/${d}" || return 1
		fi
	done
	if [[ -L ${EROOT}/usr/include/wine ]]; then
		rm "${EROOT}/usr/include/wine" || return 1
	fi
	for d in $(list_libdirs); do
		local ld=/usr/$d
		[[ -L $ld ]] && continue
		for f in "$ld"/libwine.so*; do
			rm -f "$f" || return 1
		done
		if [[ -L ${ld}/wine ]]; then
			rm -f "${ld}/wine" || return 1
		fi
	done

	desktop_database_update

	return 0
}

# remove variant wine symlinks
# usage: remove_variant_symlinks <variant>
remove_variant_symlinks() {
	local variant=$1
	[[ -n $variant ]] || die "\$variant is empty :("

	for exe in "${EXECUTABLES[@]}"; do
		if [[ -L ${EROOT}${BINDIR}/${exe}-${variant} ]]; then
			rm "${EROOT}${BINDIR}/${exe}-${variant}" || return 1
		fi
	done

	return 0
}

# set wine symlinks
# Usage: set_symlinks <name|number>
set_symlinks() {
	local target=$1

	if is_number "${target}"; then
		local targets=( $(find_targets) )
		target=${targets[target-1]}
	fi

	if [[ -z ${target} ]]; then
		die "Target \"$target\" doesn't appear to be valid!"
	elif [[ -f ${EROOT}${PREFIXDIR}/${target}/bin/wine ]]; then

		for exe in "${EXECUTABLES[@]}"; do
			local tgtexe="${exe}"-"${target#wine-}"
			if [[ -f ${EROOT}${BINDIR}/${tgtexe} ]] || [[ -L ${EROOT}${BINDIR}/${tgtexe} ]]; then
				ln -s "${tgtexe}" "${EROOT}${BINDIR}/${exe}" || return 1
			fi
		done

		for page in "${MANPAGES[@]}"; do
			if [[ -f ${EROOT}${PREFIXDIR}/${target}/man/${page} ]]; then
				ln -s "${EROOT}${PREFIXDIR}/${target}/man/${page}" "${EROOT}/${MANDIR}/${page}" || return 1
			fi
		done

		for d in "${DESKTOPFILES[@]}"; do
			if [[ -f ${EROOT}${PREFIXDIR}/${target}/applications/${d} ]]; then
				ln -s "${EROOT}${PREFIXDIR}/${target}/applications/${d}" "${EROOT}/${DESKTOPDIR}/${d}" || return 1
			fi
		done

		if [[ -d ${EROOT}${PREFIXDIR}/${target}/include/wine ]]; then
			ln -s "${EROOT}${PREFIXDIR}/${target}/include/wine" "${EROOT}/usr/include/wine" || return 1
		fi

		for d in $(list_libdirs); do
			local ld=/usr/$d
			[[ -L $ld ]] && continue
			local wined=${EROOT}${PREFIXDIR}/${target}/${d}
			for f in "${wined}"/libwine.so*; do
				ln -s "$f" "$ld"/ || return 1
			done
			if [[ -d ${wined}/wine ]]; then
				ln -s "${wined}/wine" "$ld"/wine || return 1
			fi
		done

	else
		die "Target \"$target\" doesn't appear to be valid!"
	fi

	desktop_database_update

	return 0
}

# set variant wine symlinks
# usage: set_variant_symlinks <variant> <name|number>
set_variant_symlinks() {
	local variant=$1
	[[ -n $variant ]] || die "\$variant is empty :("

	local target=$2

	if is_number "${target}"; then
		local targets=( $(find_variant_targets "$variant") )
		target=${targets[target-1]}
	fi

	if [[ -z ${target} ]]; then
		die "Target \"$target\" doesn't appear to be valid!"
	elif [[ -f ${EROOT}${PREFIXDIR}/${target}/bin/wine ]]; then

		for exe in "${EXECUTABLES[@]}"; do
			local tgtexe="${exe}"-"${target#wine-}"
			if [[ -f ${EROOT}${BINDIR}/${tgtexe} || -L ${EROOT}${BINDIR}/${tgtexe} ]]; then
				ln -s "${tgtexe}" "${EROOT}${BINDIR}/${exe}-${variant}" || return 1
			fi
		done

	else
		die "Target \"$target\" doesn't appear to be valid!"
	fi

	return 0
}

# print currently selected wine
get_current() {
	if [[ -L ${EROOT}${BINDIR}/wine ]]; then
		local winefull=$(canonicalise "${EROOT}${BINDIR}/wine")
		local wine="${winefull##*/}"
		[[ -n $wine ]] || return 1
		echo "${wine}"
	fi
}

# print currently selected variant wine
get_current_variant() {
	local variant=$1
	[[ -n $variant ]] || die "\$variant is empty :("

	if [[ -L ${EROOT}${BINDIR}/wine-${variant} ]]; then
		local winefull=$(canonicalise "${EROOT}${BINDIR}/wine-${variant}")
		local wine="${winefull##*/}"
		[[ -n $wine ]] || return 1
		echo "${wine}"
	fi
}

### show action ###

describe_show() {
	echo "Show the active wine version"
}

describe_show_options() {
	for v in ${VARIANTS[@]}; do
		echo "--$v : Show the active 'wine-$v'"
	done
}

do_show() {
	local variant=""
	if has "${1#--}" "${VARIANTS[@]}" ; then
		variant=${1#--}
	fi

	local wine
	if [[ -n $variant ]]; then
		write_list_start "Active wine-${variant} version:"
		wine=$(get_current_variant "$variant")
	else
		write_list_start "Active wine version:"
		wine=$(get_current)
	fi
	if [[ -n ${wine} ]]; then
		write_kv_list_entry "${wine}" ""
		local winedir=${EROOT}${PREFIXDIR}/${wine##*/}
		[[ -f ${winedir}/bin/wine ]] \
			|| write_warning_msg "Symlink target doesn't appear to be valid!"
	else
		write_kv_list_entry "(unset)" ""
	fi
}

### list action ###

describe_list() {
	echo "List available wine versions"
}

describe_list_options() {
	for v in ${VARIANTS[@]}; do
		echo "--$v : List the available 'wine-$v'"
	done
}

do_list() {
	local i
	local variant=""
	local targets
	if has "${1#--}" "${VARIANTS[@]}" ; then
		variant=${1#--}
	fi

	if [[ -n $variant ]]; then
		targets=( $(find_variant_targets "$variant") )
		write_list_start "Available wine-$variant versions:"
	else
		targets=( $(find_targets) )
		write_list_start "Available wine versions:"
	fi

	for (( i = 0; i < ${#targets[@]}; i++ )); do
		[[ ${targets[i]} = \
			$(basename "$(canonicalise "${EROOT}${BINDIR}/wine")") ]] \
			&& targets[i]=$(highlight_marker "${targets[i]}")
	done
	write_numbered_list -m "(none found)" "${targets[@]}"
}

### unset action ###

describe_unset() {
	echo "Remove wine symlinks"
}

describe_unset_options() {
	for v in ${VARIANTS[@]}; do
		echo "--$v : Remove the 'wine-$v' symlinks"
	done
}

do_unset() {
	local variant=""
	if has "${1#--}" "${VARIANTS[@]}" ; then
		variant=${1#--}
	fi
	if [[ -n $variant ]]; then
		remove_variant_symlinks "$variant" || die -q "Couldn't remove wine-${variant} symlinks"
	else
		remove_symlinks || die -q "Couldn't remove wine symlinks"
	fi
}

### set action ###

describe_set() {
	echo "Set a new wine version"
}

describe_set_parameters() {
	echo "<target>"
}

describe_set_options() {
	for v in ${VARIANTS[@]}; do
		echo "--$v : Set only the 'wine-$v' symlinks"
	done
	echo "target : Target name or number (from 'list' action)"
}

do_set() {
	local variant=""
	if has "${1#--}" "${VARIANTS[@]}" ; then
		variant=${1#--}
		shift
	fi
	[[ -z $1 ]] && die -q "You didn't tell me what version to symlink"
	[[ $# -gt 1 ]] && die "Too many parameters"

	local suffix=""
	[[ -n $variant ]] && suffix=-$variant
	for exe in "${EXECUTABLES[@]}"; do
		if [[ -L ${EROOT}${BINDIR}/${exe}${suffix} ]]; then
			continue
		elif [[ -e ${EROOT}${BINDIR}/${exe}${suffix} ]]; then
			# we have something strange
			die "${EROOT}${BINDIR}/${exe}${suffix} exists but is not a symlink"
		fi
	done

	if [[ -n $variant ]]; then
		remove_variant_symlinks "$variant"
		set_variant_symlinks "$variant" "$1"
	else
		remove_symlinks || die -q "Couldn't remove existing symlinks"
		set_symlinks "$1" || die -q "Couldn't set a new symlinks"
	fi
}

### update action ###

describe_update() {
	echo "Set highest version installed active"
}

describe_update_options() {
	for v in ${VARIANTS[@]}; do
		echo "--$v : Update the 'wine-$v' symlinks"
	done
	echo "--all : Update main active wine and all variants"
	echo "--if-unset : Reuse currently selected version if it appears valid"
}

do_update() {
	local variant=""
	if has --all "$@"; then
		if has --if-unset "$@"; then
			do_update --if-unset
		else
			do_update
		fi
		for variant in "$VARIANTS"; do
			if has --if-unset "$@"; then
				do_update "--${variant}" --if-unset
			else
				do_update "--${variant}"
			fi
		done
		return 0
	fi

	local v=0
	local arg
	for arg in "$@"; do
		if has "${arg#--}" "${VARIANTS[@]}" ; then
			variant=${arg#--}
			break
		fi
	done
	if has --if-unset "$@"; then
		local wine
		if [[ -n $variant ]]; then
			wine=$(get_current_variant "$variant")
		else
			wine=$(get_current)
		fi
		if [[ -n ${wine} ]]; then
			v="${wine}"
		else
			echo "Current wine is not valid, will update to newest"
		fi
	fi

	if [[ -n $variant ]]; then
		do_set "--$variant" "$v" || die -q "Couldn't set a new wine-$variant symlinks"
	else
		do_set "$v" || die -q "Couldn't set a new wine symlinks"
	fi
}