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

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

PREFIXDIR=/usr/share
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)

# TODO:
# sort function for kernel versions, to be used in a pipe
sort_kernel_versions() {
	local vsort="sort --version-sort"
	# Test if our sort supports the --version-sort option
	# (should be GNU sort, since the kernel module is GNU/Linux specific)
	${vsort} </dev/null &>/dev/null || vsort=sort

	# We sort kernel versions as follows:
	# 1. Run sed to prepend the version string by the numeric version
	#    and an additional rank indicator that is 0 for release candidates
	#    or 1 otherwise. After this step we have, for example:
	#      2.6.29 1 linux-2.6.29
	#      2.6.29 0 linux-2.6.29-rc8
	# 2. sort --version-sort
	# 3. Run sed again to remove the prepended keys from step 1.
	sed -e 's/^\(linux-\)\?\([[:digit:].]\+\)[-_]rc/\2 0 &/' \
		-e 't;s/^\(linux-\)\?\([[:digit:].]\+\)/\2 1 &/' \
		| LC_ALL=C ${vsort} | sed 's/.* //'
}

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

# 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

	return 0
}

# set wine symlinks
set_symlinks() {
	local target=$1

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

	if [[ -z ${target} ]]; then
		die -q "Target \"$1\" 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

	else
		die -q "Target \"$1\" doesn't appear to be valid!"
	fi

	return 0
}

### show action ###

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

do_show() {
	write_list_start "Active wine version:"
	if [[ -L ${EROOT}${BINDIR}/wine ]]; then
		local wine=$(canonicalise "${EROOT}${BINDIR}/wine")
		winedir=${EROOT}${PREFIXDIR}/${wine##*/}
		write_kv_list_entry "${winedir%/}" ""
		[[ -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"
}

do_list() {
	local i targets=( $(find_targets) )

	write_list_start "Available wine versions:"
	write_numbered_list -m "(none found)" "${targets[@]}"
}

### remove action ###

describe_remove() {
	echo "Remove wine symlinks"
}

do_remove() {
	remove_symlinks || die -q "Couldn't remove existing symlinks"
}

### set action ###

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

describe_set_parameters() {
	echo "<target>"
}

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

do_set() {
	[[ -z $1 ]] && die -q "You didn't tell me what version to symlink"
	[[ $# -gt 1 ]] && die -q "Too many parameters"

	for exe in "${EXECUTABLES[@]}"; do
		if [[ -L ${EROOT}/usr/bin/${exe} ]]; then
			continue
		elif [[ -e ${EROOT}/usr/bin/${exe} ]]; then
			# we have something strange
			die -q "${EROOT}/usr/bin/${exe} exists but is not a symlink"
		fi
	done

	remove_symlinks || die -q "Couldn't remove existing symlinks"
	set_symlinks "$1" || die -q "Couldn't set a new symlinks"
}