aboutsummaryrefslogtreecommitdiff
blob: dd81f744840c920023ea0dfa9ce6ea33a7a69d26 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# -*-eselect-*-  vim: ft=eselect
# Copyright 2005-2017 Gentoo Foundation
# Distributed under the terms of the GNU GPL version 2 or later

inherit config multilib tests

DESCRIPTION="Manage active wine version"
MAINTAINER="NP-Hardass@gentoo.org"

CONFIG_DIR="/etc/eselect/wine"

VARIANTS=(
	vanilla
	staging
	d3d9
	any
)

################################################################################
#                               Eselect Wine                                   #
################################################################################

# Verify that files exist for specified target
# Input: ${P}
is_valid_target(){
	if [[ $# -ne 1 ]]; then
		die "Invalid number of parameters"
	fi
	
	local tgt="$1"

	set_dir_env	

	if [[ ! -d "${wine_bin}" ]] || \
		[[ ! "$(ls -A ${wine_bin})" ]] || \
		[[ ! -e "${wine_bin}/wine" ]]; then
		return 1
	elif [[ ! -e "${EPREFIX}/usr/bin/${tgt}" ]]; then
		return 1
	else
		return 0
	fi

	unset_dir_env
}

# Verify that symlinks are valid
are_symlinks_valid() {
	if [[ $# -gt 1 ]]; then
		die "Invalid number of parameters"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die "Invalid variant"
			fi
		else
			die "Invalid parameter"
		fi
		shift
	done

	local active=$(load_config "${CONFIG_DIR}/active" ${v})
	if [[ "${active}" == "(unset)" ]]; then
		write_error_message "Invalid active config"
		return 1
	fi

	set_dir_env

	#TODO check links manually and by config
	unset_dir_env
}


# print currently selected wine
get_active() {
	if [[ $# -gt 1 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die -q "Invalid variant"
			fi
		else
			[[ -z $1 ]] || die -q "Invalid parameter"
		fi
		shift
	done

	local active=$(load_config "${CONFIG_DIR}/active" ${v})

	if [[ -z "${active}" ]]; then
		echo "(unset)"
	else
		echo "${active}"
	fi
}

get_installed(){
	if [[ $# -gt 1 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die -q "Invalid variant"
			fi
		else
			die -q "Invalid parameter"
		fi
		shift
	done

	local installed=$(load_config "${CONFIG_DIR}/installed" ${v})

	echo $(sort_list ${installed[@]})
}

### Registration action ##
describe_register(){
	echo "Register a new package with the eselect module (Internal use only)"
}

describe_register() {
	echo "<target>"
}

describe_register_options() {
	for v in ${VARIANTS[@]}; do
		echo "--${v} : Register a new package with ${v}"
	done
	echo "target : A fully qualified variant e.g."
	echo " :	wine-1.9.23, wine-vanilla-2.0, or wine-staging-2.1.1" 
}

do_register(){
	if [[ $# -lt 1 ]] || [[ $# -gt 3 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die -q "Invalid flag or variant"
			fi
		else
			local tgt="$1"
		fi
		shift
	done

	if [[ -z "${tgt}" ]]; then
		die -q "Target not specified"
	fi

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	if is_number "${tgt}"; then
		local tgts=($(get_installed ${v_arg}))
		tgt=${tgts[${tgt}-1]}
	fi

	if ! is_valid_target "${tgt}"; then
		die -q "Target does not exist"
	fi

	local list=$(get_installed ${v_arg})

	if ! has ${tgt} ${list}; then
		list=($(sort_list "${list} ${tgt}"))
		store_config "${CONFIG_DIR}/installed" ${v} "${list[@]}"
	fi
}

describe_deregister(){
	echo "Deregister a package with the eselect module (Internal use only)"
}

describe_deregister() {
	echo "<target>"
}

describe_deregister_options() {
	for v in ${VARIANTS[@]}; do
		echo "--${v} : Deregister a package with ${v}"
	done
	echo "--force : Forcibly remove a package"
	echo "target : A fully qualified variant e.g."
	echo " :	wine-1.9.23, wine-vanilla-2.0, or wine-staging-2.1.1" 
}

do_deregister(){
	if [[ $# -lt 1 ]] || [[ $# -gt 3 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"
	local force=false

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			elif [[ "${flag}" == "force" ]]; then
				force=true
			else
				die -q "Invalid variant"
			fi
		else
			local tgt="$1"
		fi
		shift
	done

	if [[ -z "${tgt}" ]]; then
		die -q "Target not specified"
	fi

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	if is_number "${tgt}"; then
		local tgts=($(get_installed ${v_arg}))
		tgt=${tgts[${tgt}-1]}
	fi

	if ! is_valid_target "${tgt}" && [[ ${force} == "false" ]]; then
		die -q "Target does not exist"
	fi

	local list=$(get_installed ${v_arg})

	if ! has ${tgt} ${list}; then
		die -q "${tgt} is not registered for ${v}"
	fi

	if [[ "${tgt}" == "$(get_active ${v_arg})" ]]; then
		do_unset ${v_arg}
	fi

	list=$(list_rm_item ${tgt} "${list}")
	store_config "${CONFIG_DIR}/installed" ${v} "${list}"
}

### 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() {
	if [[ $# -gt 1 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die -q "Invalid variant"
			fi
		else
			die -q "Invalid parameter"
		fi
		shift
	done

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	write_list_start "Available wine${v_arg#-} versions:"
	local installed=($(get_installed ${v_arg}))
	local active=$(get_active ${v_arg})

	if [[ ${#installed} -eq 0 ]]; then
		unset installed
	else
		for (( i = 0; i < ${#installed[@]}; i++ )); do
			if [[ ${installed[i]} = ${active} ]]; then
				installed[i]=$(highlight_marker "${installed[i]}")
			fi
		done
	fi

	write_numbered_list -m "(none found)" "${installed[@]}"
}

### 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() {
	if [[ $# -gt 1 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die -q "Invalid variant"
			fi
		else
			die -q "Invalid parameter"
		fi
		shift
	done

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	write_list_start "Active wine${v_arg#-} version:"
	local active=$(get_active ${v_arg})

	write_kv_list_entry "${active}" ""
	if [[ "${active}" != "(unset)" ]]; then
		are_symlinks_valid ${v_arg}
	fi
}

### unset action ###

describe_unset() {
	echo "Remove wine symlinks"
}

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

do_unset() {
	if [[ $# -gt 1 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"
	local all=false

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			elif [[ "${flag}" == "all" ]]; then
				all=true
			else
				die -q "Invalid variant"
			fi
		else
			die -q "Invalid parameter"
		fi
		shift
	done

	# Run do_update for all variants
	if ${all}; then
		do_unset
		for v in ${VARIANTS[@]}; do
			do_unset "--${v}"
		done

		return 0
	fi

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	remove_symlinks ${v_arg} || die -q "Couldn't remove wine${v_arg#-} symlinks"

	store_config "${CONFIG_DIR}/active" "${v}" ""
}

### 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() {
	if [[ $# -lt 1 ]] || [[ $# -gt 2 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die -q "Invalid flag or variant"
			fi
		else
			local tgt="$1"
		fi
		shift
	done

	if [[ -z "${tgt}" ]]; then
		die -q "Target not specified"
	fi

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	if is_number "${tgt}"; then
		local tgts=($(get_installed ${v_arg}))
		tgt=${tgts[${tgt}-1]}
	fi

	if ! is_valid_target "${tgt}"; then
		die -q "Target does not exist"
	fi

	do_unset ${v_arg} || die -q "Failed to unset active target"
	set_symlinks ${v_arg} "${tgt}" || die -q "Couldn't set wine-${v_arg#-} symlinks"

	store_config "${CONFIG_DIR}/active" "${v}" "${tgt}"
}

### 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() {
	if [[ $# -gt 3 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"
	local if_unset=false
	local all=false

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			elif [[ "${flag}" == "if-unset" ]]; then
				if_unset=true
			elif [[ "${flag}" == "all" ]]; then
				all=true
			else
				die -q "Invalid flag or variant"
			fi
		else
			die -q "Invalid parameter"
		fi
		shift
	done

	if [[ "${v}" != "wine" ]] && ${all}; then
		die -q "Either specify a specific variant, or all, but not both"
	fi

	# Run do_update for all variants
	if ${all}; then
		local unset_arg
		if ${if_unset}; then
			unset_arg="--if-unset"
		fi

		do_update ${unset_arg}
		for v in ${VARIANTS[@]}; do
			do_update "--${v}" ${unset_arg}
		done

		return 0
	fi

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	# Determine if set and valid
	# If so, return
	if ${if_unset}; then
		local active=$(get_active ${v_arg})
		if is_valid_target "${active}" && are_symlinks_valid ${v_arg}; then
			return 0
		fi
	fi

	local installed=($(get_installed ${v_arg}))
	if [[ ${#installed[@]} -eq 0 ]]; then
		write_warning_msg "Skipping wine${v_arg#-}. No registered targets found."
		return 0
	fi
	local tgt=${installed[-1]}

	do_set ${v_arg} "${tgt}" || die -q "Couldn't set new wine${v_arg#-} symlinks"
}


# 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 "${EPREFIX}/usr/share/applications" || die
	fi
}

# 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/.* //'
}

# Takes a list of items and returns a sorted version
sort_list() {
	list=($@)
	if [[ ! -z "${list}" ]]; then
		for i in ${list[@]}; do
			echo "${i}"
		done | sort_versions
	fi
}

# Returns a list excluding the specified item
# list_rm_item [rm_this] [list]
list_rm_item() {
	if [[ $# -lt 2 ]]; then
		die "Not enough arguments"
	fi

	rm_this=$1
	shift
	list=($@)

	local i
	for i in ${!list[@]};do
	    if [ "${list[$i]}" == "${rm_this}" ]; then
	        unset list[$i]
	    fi 
	done

	echo ${list[@]}
}

remove_symlinks() {
	if [[ $# -gt 1 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die -q "Invalid flag or variant"
			fi
		else
			die -q "Invalid parameter"
		fi
		shift
	done

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	local active=$(get_active ${v_arg})
	if [[ "${active}" == "(unset)" ]]; then
		return 0
		#die -q "No active links to remove"
	fi

	if ! are_symlinks_valid ${v_arg}; then
		die -q "Currently configured symlinks are invalid"
	fi

	local symlink_vars=$(load_config "${CONFIG_DIR}/links/${v}" symlink_dirs)
	if [[ -z "${symlink_vars}" ]]; then
		die -q "Missing listing of configured symlinks"
	fi
	local var
	for var in ${symlink_vars[@]}; do
		local symlinks=$(load_config "${CONFIG_DIR}/links/${v}" ${var})
		local symlink
		if [[ -z "${symlinks}" ]]; then
			die -q "Missing listing of ${var} symlinks"
		fi
		for symlink in ${symlinks[@]}; do
			if [[ ! -L "${symlink}" ]]; then
				die -q "${symlink} is not a symlink."
			fi
			rm "${symlink}" || die "Failed to rm ${symlink}"
			
			# remove entry from config file
			symlinks=$(list_rm_item "${symlink}" "${symlinks}")

			store_config "${CONFIG_DIR}/links/${v}" ${var} "${symlinks}"
		done

		# remove set from config file
		symlink_vars=$(list_rm_item "${var}" "${symlink_vars}")

		store_config "${CONFIG_DIR}/links/${v}" symlink_dirs "${symlink_vars}"
	done

#	desktop_database_update || die "Failed to update xorg desktop database"
}

set_symlinks() {
	if [[ $# -lt 1 ]] || [[ $# -gt 2 ]]; then
		die -q "Invalid number of parameters specified"
	fi

	local v="wine"

	while [[ $# -gt 0 ]]; do
		if [[ $1 == --* ]]; then
			local flag="${1#--}"
			if has "${flag}" ${VARIANTS[@]}; then
				v=${flag}
			else
				die -q "Invalid flag or variant"
			fi
		else
			local tgt="$1"
		fi
		shift
	done

	if [[ -z "${tgt}" ]]; then
		die -q "Target not specified"
	fi

	local v_arg
	if [[ ${v} != "wine" ]]; then
		v_arg="--${v}"
	fi

	if is_number "${tgt}"; then
		local tgts=($(get_installed ${v_arg}))
		tgt=${tgts[${tgt}-1]}
	fi

	if ! is_valid_target "${tgt}"; then
		die -q "Target does not exist"
	fi

	set_dir_env

	local link

	store_config "${CONFIG_DIR}/links/${v}" symlink_dirs "bin"
	store_config "${CONFIG_DIR}/links/${v}" "bin" ""
	for b in "${wine_bin}"/*; do
		#TODO detect errors and clean up
		link="${PREFIX}/usr/bin/${b##*/}${v_arg#-}"
		ln -s "${b}" "${link}"
		append_config "${CONFIG_DIR}/links/${v}" "bin" "${link}"
	done

	append_config "${CONFIG_DIR}/links/${v}" symlink_dirs "man"
	store_config "${CONFIG_DIR}/links/${v}" "man" ""
	for md in "${wine_man}"/*; do
		for m in "${md}"/*; do
			#TODO detect errors and clean up
			link="${PREFIX}/usr/share/man/${md##*/}/${m##*/}${v_arg#-}"
			ln -s "${m}" "${link}"
			append_config "${CONFIG_DIR}/links/${v}" "man" "${link}"
		done
	done

	unset_dir_env

#	desktop_database_update || die "Failed to update xorg desktop database"
}

# set env vars for us to work with
set_dir_env() {
	wine_bin="${EPREFIX}/usr/lib/${tgt}/bin"
	wine_include="${EPREFIX}/usr/include/${tgt}/wine"
	wine_share="${EPREFIX}/usr/share/${tgt}"
	wine_app="${wine_share}/applications"
	wine_man="${wine_share}/man"
}

# unset our env vars
unset_dir_env() {
	unset wine_{bin,include,share,app,man}
}