forked from clayrisser/dotstow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotstow.sh
810 lines (776 loc) · 23 KB
/
dotstow.sh
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#!/bin/sh
export GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
export DEBIAN_FRONTEND=${DEBIAN_FRONTEND:-gnome}
export _TMP_PATH="${XDG_RUNTIME_DIR:-$([ -d "/run/user/$(id -u $USER)" ] && echo "/run/user/$(id -u $USER)" || echo ${TMP:-${TEMP:-/tmp}})}/cody/wizard/$$"
export _STATE_PATH="${XDG_STATE_HOME:-$HOME/.local/state}/dotstow"
export _STOWED_PATH="$_STATE_PATH/stowed"
if [ "$DOTFILES_PATH" = "" ]; then
export DOTFILES_PATH="$_STATE_PATH/dotfiles"
fi
export ARCH=unknown
export FLAVOR=unknown
export PKG_MANAGER=unknown
export PLATFORM=unknown
if [ "$OS" = "Windows_NT" ]; then
export HOME="${HOMEDRIVE}${HOMEPATH}"
PLATFORM=win32
FLAVOR=win64
ARCH="$PROCESSOR_ARCHITECTURE"
PKG_MANAGER=choco
if [ "$ARCH" = "AMD64" ]; then
ARCH=amd64
elif [ "$ARCH" = "ARM64" ]; then
ARCH=arm64
fi
if [ "$PROCESSOR_ARCHITECTURE" = "x86" ]; then
ARCH=amd64
if [ "$PROCESSOR_ARCHITEW6432" = "" ]; then
ARCH=x86
FLAVOR=win32
fi
fi
else
PLATFORM=$(uname 2>/dev/null | tr '[:upper:]' '[:lower:]' 2>/dev/null)
ARCH=$( (dpkg --print-architecture 2>/dev/null || uname -m 2>/dev/null || arch 2>/dev/null || echo unknown) |
tr '[:upper:]' '[:lower:]' 2>/dev/null)
if [ "$ARCH" = "i386" ] || [ "$ARCH" = "i686" ]; then
ARCH=386
elif [ "$ARCH" = "x86_64" ]; then
ARCH=amd64
fi
if [ "$PLATFORM" = "linux" ]; then
if [ -f /system/bin/adb ]; then
if [ "$(getprop --help >/dev/null 2>/dev/null && echo 1 || echo 0)" = "1" ]; then
PLATFORM=android
fi
fi
if [ "$PLATFORM" = "linux" ]; then
FLAVOR=$(lsb_release -si 2>/dev/null | tr '[:upper:]' '[:lower:]' 2>/dev/null)
if [ "$FLAVOR" = "" ]; then
FLAVOR=unknown
if [ -f /etc/redhat-release ]; then
FLAVOR=rhel
elif [ -f /etc/SuSE-release ]; then
FLAVOR=suse
elif [ -f /etc/debian_version ]; then
FLAVOR=debian
elif (cat /etc/os-release 2>/dev/null | grep -qE '^ID=alpine$'); then
FLAVOR=alpine
fi
fi
if [ "$FLAVOR" = "rhel" ]; then
PKG_MANAGER=$(which microdnf >/dev/null 2>&1 && echo microdnf ||
echo $(which dnf >/dev/null 2>&1 && echo dnf || echo yum))
elif [ "$FLAVOR" = "suse" ]; then
PKG_MANAGER=zypper
elif [ "$FLAVOR" = "debian" ]; then
PKG_MANAGER=apt-get
elif [ "$FLAVOR" = "ubuntu" ]; then
PKG_MANAGER=apt-get
elif [ "$FLAVOR" = "alpine" ]; then
PKG_MANAGER=apk
fi
fi
elif [ "$PLATFORM" = "darwin" ]; then
PKG_MANAGER=brew
else
if (echo "$PLATFORM" | grep -q 'MSYS'); then
PLATFORM=win32
FLAVOR=msys
PKG_MANAGER=pacman
elif (echo "$PLATFORM" | grep -q 'MINGW'); then
PLATFORM=win32
FLAVOR=msys
PKG_MANAGER=mingw-get
elif (echo "$PLATFORM" | grep -q 'CYGWIN'); then
PLATFORM=win32
FLAVOR=cygwin
fi
fi
fi
if [ "$FLAVOR" = "unknown" ]; then
FLAVOR="$PLATFORM"
fi
get_package_dir() {
_PACKAGE=$1
cd $DOTFILES_PATH
_PACKAGE_GROUP=$( ( (ls $FLAVOR 2>/dev/null || true) | grep -qE "^${_PACKAGE}$") && echo $FLAVOR ||
( ( (ls $PLATFORM 2>/dev/null || true) | grep -qE "^${_PACKAGE}$") && echo $PLATFORM ||
( ( (ls global 2>/dev/null || true) | grep -qE "^${_PACKAGE}$") && echo global || true)))
if [ "$_PACKAGE_GROUP" != "" ] && [ -d "$_PACKAGE_GROUP" ]; then
cd $_PACKAGE_GROUP
pwd
fi
}
main() {
_prepare
if [ "$_COMMAND" = "init" ]; then
_init $@
elif [ "$_COMMAND" = "stow" ]; then
_stow $@
elif [ "$_COMMAND" = "unstow" ]; then
_unstow $@
elif [ "$_COMMAND" = "available" ]; then
_available $@
elif [ "$_COMMAND" = "sync" ]; then
_sync $@
elif [ "$_COMMAND" = "stowed" ]; then
_stowed $@
elif [ "$_COMMAND" = "wizard" ]; then
_wizard $@
elif [ "$_COMMAND" = "path" ]; then
_path $@
elif [ "$_COMMAND" = "reset" ]; then
_reset $@
elif [ "$_COMMAND" = "status" ]; then
_status $@
fi
}
_prepare() {
if ! which stow 2>&1 >/dev/null; then
if [ "$PKG_MANAGER" = "apt-get" ]; then
sudo apt-get install -y stow
elif [ "$PKG_MANAGER" = "brew" ]; then
brew install stow
else
echo "please install the stow command
https://www.gnu.org/software/stow" >&2
exit 1
fi
fi
if [ ! -d "$_STATE_PATH" ]; then
mkdir -p "$_STATE_PATH"
fi
if [ ! -d "$DOTFILES_PATH" ]; then
GIT_REPO=$(kwyzod string -d "[email protected]:$USER/dotfiles" "select the git repository that contains your dotfiles")
rm -rf $_TMP_PATH
_init "$GIT_REPO"
fi
}
_init() {
_REPO=$1
if [ "$_REPO" = "" ]; then
echo "no repo specified" >&2
exit 1
fi
if [ -d "$DOTFILES_PATH" ]; then
echo "dotfiles already initialized" >&2
exit 1
fi
echo '$ git clone '"$_REPO $DOTFILES_PATH"
git clone $_REPO "$DOTFILES_PATH"
if [ ! -L "$HOME/.dotfiles" ] && [ ! -d "$HOME/.dotfiles" ] && [ ! -f "$HOME/.dotfiles" ]; then
ln -s "$DOTFILES_PATH" "$HOME/.dotfiles"
fi
}
_stow() {
_PACKAGE=$1
shift
_PACKAGE_DIR=$(get_package_dir $_PACKAGE)
echo "stowing $_PACKAGE package for $FLAVOR $PLATFORM"
if [ "$_PACKAGE_DIR" = "" ]; then
echo "package $_PACKAGE not found" >&2
exit 1
fi
_RM_FILES=$(echo $(for f in $(cd $_PACKAGE_DIR/$_PACKAGE && (find . -type f | sed "s|^./|$HOME/|g")); do
if [ -f $f ]; then echo $f; fi
done))
if [ "$_RM_FILES" != "" ]; then
echo '$ rm -f '"$_RM_FILES"
rm -f $_RM_FILES
fi
echo '$ '"stow -t $HOME -d $_PACKAGE_DIR $@ $_PACKAGE" | sed 's| \+| |g'
stow --override '/.*/s' -t $HOME -d $_PACKAGE_DIR $@ $_PACKAGE
mkdir -p "$_STOWED_PATH/$_PACKAGE"
}
_unstow() {
_PACKAGE=$1
shift
_PACKAGE_DIR=$(get_package_dir $_PACKAGE)
echo "unstowing $_PACKAGE package for $FLAVOR $PLATFORM"
if [ "$_PACKAGE_DIR" = "" ]; then
echo "package $_PACKAGE not found" >&2
exit 1
fi
echo '$ '"stow -t $HOME -d $_PACKAGE_DIR -D $@ $_PACKAGE" | sed 's| \+| |g'
stow -t $HOME -d $_PACKAGE_DIR -D $@ $_PACKAGE
rm -rf "$_STOWED_PATH/$_PACKAGE"
}
_available() {
( (ls $DOTFILES_PATH/global 2>/dev/null || true) &&
(ls $DOTFILES_PATH/$FLAVOR 2>/dev/null || true) &&
(ls $DOTFILES_PATH/$PLATFORM 2>/dev/null || true)) | sort | uniq
}
_stowed() {
(ls $_STOWED_PATH 2>/dev/null || true) | sort | uniq
}
_sync() {
if [ ! -d "$DOTFILES_PATH" ]; then
echo "dotfiles not initialized" >&2
exit 1
fi
cd $DOTFILES_PATH
echo '$ git add -A'
git add -A
echo '$ git commit -m "Updated '"$(git status | grep 'modified: ' | sed 's|^.*modified:\s*||g' | head -n1)"'"'
git commit -m "Updated $(git status | grep 'modified: ' | sed 's|^.*modified:\s*||g' | head -n1)"
echo '$ git pull'
git pull
echo '$ git push'
(echo y) | git push
}
_wizard() {
STOWED=$(dotstow stowed)
NOT_STOWED=$( (dotstow stowed && dotstow available) | sort | uniq -u)
if [ "$STOWED" != "" ]; then
PACKAGES_UNSTOW=$(kwyzod enum -m "select the packages you wish to unstow" $STOWED)
if [ "$(kwyzod boolean "UNSTOW PACKAGES\n\n$PACKAGES_UNSTOW")" != "1" ]; then
exit 1
fi
fi
if [ "$NOT_STOWED" != "" ]; then
PACKAGES_STOW=$(kwyzod enum -m "select the packages you wish to stow" $NOT_STOWED)
if [ "$(kwyzod boolean "STOW PACKAGES\n\n$PACKAGES_STOW")" != "1" ]; then
exit 1
fi
fi
for t in $PACKAGES_UNSTOW; do
echo '$' dotstow unstow $t
dotstow unstow $t
done
for l in $PACKAGES_STOW; do
echo '$' dotstow stow $l
dotstow stow $l
done
}
_path() {
echo "$DOTFILES_PATH"
}
_reset() {
cd "$DOTFILES_PATH"
git add -A
git reset --hard
}
_status() {
cd "$DOTFILES_PATH"
git status
}
if ! test $# -gt 0; then
set -- "-h"
fi
while test $# -gt 0; do
case "$1" in
-h | --help)
echo "dotstow - manage dotfiles with git and stow"
echo " "
echo "dotstow [options] command <PACKAGE>"
echo " "
echo "options:"
echo " -h, --help show brief help"
echo " "
echo "commands:"
echo " init <REPO> initialize dotstow"
echo " s, stow <PACKAGE> stow a package"
echo " u, unstow <PACKAGE> unstow a package"
echo " w, wizard dotfiles wizard"
echo " a, available available packages"
echo " stowed stowed packages"
echo " sync sync dotfiles"
echo " status dotfiles git status"
echo " reset reset dotfiles"
echo " path get dotfiles path"
exit 0
;;
-*)
echo "invalid option $1" 1>&2
exit 1
;;
*)
break
;;
esac
done
case "$1" in
init)
shift
if test $# -gt 0; then
export _COMMAND=init
elif [ -d "$DOTFILES_PATH" ]; then
echo "no repo specified" 1>&2
exit 1
fi
;;
s | stow)
shift
if test $# -gt 0; then
export _COMMAND=stow
else
echo "no package specified" 1>&2
exit 1
fi
;;
u | unstow)
shift
if test $# -gt 0; then
export _COMMAND=unstow
else
echo "no package specified" 1>&2
exit 1
fi
;;
w | wizard)
shift
export _COMMAND=wizard
;;
a | available)
shift
export _COMMAND=available
;;
stowed)
shift
export _COMMAND=stowed
;;
sync)
shift
export _COMMAND=sync
;;
path)
shift
export _COMMAND=path
;;
reset)
shift
export _COMMAND=reset
;;
status)
shift
export _COMMAND=status
;;
*)
echo "invalid command $1" 1>&2
exit 1
;;
esac
kwyzod() {
while [ $# -gt 0 ]; do
case "$1" in
--dialogs)
KWYZOD_DIALOGS="$2"
shift 2
;;
--default | -d)
KWYZOD_DEFAULT="$2"
shift 2
;;
--multiple | -m)
_ENUM_MULTIPLE="1"
shift
;;
*)
break
;;
esac
done
_TYPE="$1"
shift
while [ $# -gt 0 ]; do
case "$1" in
--default | -d)
KWYZOD_DEFAULT="$2"
shift 2
;;
--multiple | -m)
_ENUM_MULTIPLE="1"
shift
;;
*)
break
;;
esac
done
case "$_TYPE" in
dialogs)
_kwyzod_dialogs "$@"
;;
boolean)
_kwyzod_boolean "$KWYZOD_DEFAULT" "$@"
;;
string)
_kwyzod_string "$KWYZOD_DEFAULT" "$@"
;;
integer)
_kwyzod_integer "$KWYZOD_DEFAULT" "$@"
;;
enum)
if [ -z "$_ENUM_MULTIPLE" ]; then
_kwyzod_select "$KWYZOD_DEFAULT" "$@"
else
_kwyzod_multiselect "$KWYZOD_DEFAULT" "$@"
fi
;;
*)
_kwyzod_help
;;
esac
}
_kwyzod_help() {
cat <<EOF
Usage: kwyzod <OPTIONS> [COMMAND] <TYPE_OPTIONS> <PROMPT>
[COMMAND]:
dialogs list installed dialogs
boolean prompt for a boolean value
string prompt for a string value
integer prompt for an integer value
enum [...options] prompt for an enum value
<OPTIONS>:
--default, -d set the default value
--dialogs comma separated list of ordered dialogs to use (eg: "zenity,dialog")
--help, -h display this help
--multiple, -m allow multiple selections (only for enum)
--version, -v display version
<TYPE_OPTIONS>:
--default, -d set the default value
--multiple, -m allow multiple selections (only for enum)
<PROMPT>: prompt displayed in the dialog box
EOF
}
_error() {
echo "$@" >&2
}
_kwyzod_detect_dialogs() {
_DIALOGS=""
if [ -z "$KWYZOD_DIALOGS" ]; then
if [ "$(uname)" = "Darwin" ]; then
KWYZOD_DIALOGS="osascript zenity yad kdialog"
elif [ "$(uname)" = "Linux" ]; then
if [ "$XDG_CURRENT_DESKTOP" = "KDE" ]; then
KWYZOD_DIALOGS="kdialog zenity yad"
else
KWYZOD_DIALOGS="zenity yad kdialog"
fi
fi
KWYZOD_DIALOGS="$KWYZOD_DIALOGS dialog whiptail"
else
KWYZOD_DIALOGS="$(echo "$KWYZOD_DIALOGS" | tr ',' ' ')"
fi
for _DIALOG in $KWYZOD_DIALOGS; do
if command -v "$_DIALOG" >/dev/null 2>&1; then
_DIALOGS="$_DIALOGS $_DIALOG"
fi
done
echo "$_DIALOGS"
}
_kwyzod_get_dialog() {
_SUPPORTED_DIALOGS="$@"
_INSTALLED_DIALOGS="$(_kwyzod_detect_dialogs)"
_DIALOG=""
for d in $_INSTALLED_DIALOGS; do
if echo " $_SUPPORTED_DIALOGS " | grep -q " $d "; then
_DIALOG="$d"
break
fi
done
echo "$_DIALOG"
}
_kwyzod_dialogs() {
_kwyzod_detect_dialogs | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | tr ' ' '\n'
}
_kwyzod_boolean() {
_DEFAULT="$1"
_PROMPT="$2"
_RESULT=""
_DIALOG=$(_kwyzod_get_dialog "osascript" "zenity" "kdialog" "yad" "dialog" "whiptail")
if [ -z "$_DIALOG" ]; then
printf "%s [Y|n]: " "$_PROMPT"
read -r _ANSWER
case "$_ANSWER" in
[Nn]*)
_RESULT="0"
;;
*)
_RESULT="1"
;;
esac
else
case "$_DIALOG" in
osascript)
_OSASCRIPT_OUTPUT="$(osascript -e 'tell application "System Events" to display dialog '"\"$_PROMPT\""' buttons {"Yes", "No"} default button "Yes" giving up after 86400' -e 'button returned of result')"
_RESULT="$([ "$_OSASCRIPT_OUTPUT" = "Yes" ] && echo "1" || echo "0")"
;;
zenity)
_RESULT="$(zenity --question --text "$_PROMPT" && echo "1" || echo "0")"
;;
kdialog)
_RESULT="$(kdialog --yesno "$_PROMPT" && echo "1" || echo "0")"
;;
yad)
_RESULT="$(yad --question --text "$_PROMPT" && echo "1" || echo "0")"
;;
dialog)
dialog --yesno "$_PROMPT" 10 40 2>&1 >/dev/tty && _RESULT="1" || _RESULT="0"
;;
whiptail)
whiptail --yesno "$_PROMPT" 10 40 --yes-button "Yes" --no-button "No" && _RESULT="1" || _RESULT="0"
;;
esac
fi
echo "$_RESULT"
}
_kwyzod_string() {
_DEFAULT="$1"
_PROMPT="$2"
_RESULT=""
_DIALOG=$(_kwyzod_get_dialog "osascript" "zenity" "kdialog" "yad" "dialog" "whiptail")
if [ -z "$_DIALOG" ]; then
if [ -z "$_DEFAULT" ]; then
printf "%s: " "$_PROMPT"
else
printf "%s (%s): " "$_PROMPT" "$_DEFAULT"
fi
read -r _RESULT
if [ -z "$_RESULT" ]; then
_RESULT="$_DEFAULT"
fi
else
case "$_DIALOG" in
osascript)
_RESULT=$(
osascript <<EOF
text returned of (display dialog "$_PROMPT" default answer "$_DEFAULT" buttons {"OK"} default button 1)
EOF
)
;;
zenity)
_RESULT="$(zenity --entry --text "$_PROMPT" --entry-text="$_DEFAULT")"
;;
kdialog)
_RESULT="$(kdialog --inputbox "$_PROMPT" "$_DEFAULT")"
;;
yad)
_RESULT="$(yad --entry --text="$_PROMPT" --entry-text="$_DEFAULT")"
;;
dialog)
_RESULT="$(dialog --inputbox "$_PROMPT" 10 40 "$_DEFAULT" 3>&1 1>&2 2>&3)"
;;
whiptail)
_RESULT="$(whiptail --inputbox "$_PROMPT" 10 40 "$_DEFAULT" 3>&1 1>&2 2>&3)"
;;
esac
fi
echo "$_RESULT"
}
_kwyzod_integer() {
_DEFAULT="$1"
_PROMPT="$2"
_RESULT=""
_DIALOG="$(_kwyzod_get_dialog "osascript" "zenity" "kdialog" "yad" "dialog" "whiptail")"
if [ -z "$_DIALOG" ]; then
if [ -z "$_DEFAULT" ]; then
printf "%s: " "$_PROMPT"
else
printf "%s (%s): " "$_PROMPT" "$_DEFAULT"
fi
read -r _RESULT
if [ -z "$_RESULT" ]; then
_RESULT="$_DEFAULT"
fi
else
case "$_DIALOG" in
osascript)
_RESULT=$(
osascript <<EOF
text returned of (display dialog "$_PROMPT" default answer "$_DEFAULT" buttons {"OK"} default button 1)
EOF
)
;;
zenity)
_RESULT="$(zenity --entry --text "$_PROMPT" --entry-text="$_DEFAULT")"
;;
kdialog)
_RESULT="$(kdialog --inputbox "$_PROMPT" "$_DEFAULT")"
;;
yad)
_RESULT="$(yad --form --text="$_PROMPT" --field="":NUM "$_DEFAULT" | sed 's/|[[:space:]]*$//'))"
;;
dialog)
_RESULT="$(dialog --inputbox "$_PROMPT" 10 40 "$_DEFAULT" 3>&1 1>&2 2>&3)"
;;
whiptail)
_RESULT="$(whiptail --inputbox "$_PROMPT" 10 40 "$_DEFAULT" 3>&1 1>&2 2>&3)"
;;
esac
fi
_RESULT="$(echo "$_RESULT" | cut -d'.' -f1)"
_RESULT="$(echo "$_RESULT" | tr -d -c 0-9)"
if [ -z "$_RESULT" ]; then
_RESULT="0"
fi
echo "$_RESULT"
}
_kwyzod_select() {
_DEFAULT="$1"
_PROMPT="$2"
shift 2
set -- "$@"
_RESULT=""
_DIALOG=$(_kwyzod_get_dialog "osascript" "zenity" "kdialog" "yad" "dialog" "whiptail")
if [ -z "$_DIALOG" ]; then
_OPTIONS=""
for _OPTION in "$@"; do
if [ "$_OPTION" != "$_DEFAULT" ]; then
_OPTIONS="$_OPTIONS \"$_OPTION\""
fi
done
eval set -- $_OPTIONS
if [ -n "$_DEFAULT" ]; then
set -- "$_DEFAULT" "$@"
fi
i=1
for _OPTION; do
if [ "$_OPTION" == "$_DEFAULT" ]; then
printf "*%s) %s\n" "$i" "$_OPTION"
else
if [ -z "$_DEFAULT" ]; then
printf "%s) %s\n" "$i" "$_OPTION"
else
printf " %s) %s\n" "$i" "$_OPTION"
fi
fi
i="$((i + 1))"
done
printf "%s: " "$_PROMPT"
read -r _RESULT
if [ -z "$_RESULT" ]; then
_RESULT="$_DEFAULT"
else
_RESULT="$(eval "echo \$$((_RESULT))")"
fi
else
case "$_DIALOG" in
osascript)
if [ -n "$_DEFAULT" ]; then
_OPTIONS="\"$_DEFAULT\""
fi
for _OPTION; do
if [ "$_OPTION" != "$_DEFAULT" ]; then
_OPTIONS="$_OPTIONS, \"$_OPTION\""
fi
done
_RESULT="$(osascript -e "choose from list {$_OPTIONS} with prompt \"$_PROMPT\"")"
;;
zenity)
if [ -n "$_DEFAULT" ]; then
_OPTIONS="\"$_DEFAULT\""
fi
for _OPTION in "$@"; do
if [ "$_OPTION" != "$_DEFAULT" ]; then
_OPTIONS="$_OPTIONS \"$_OPTION\""
fi
done
_RESULT="$(eval zenity --list --text=\"$_PROMPT\" --column=\"Options\" $_OPTIONS)"
;;
kdialog)
_RESULT="$(kdialog --radiolist "$_PROMPT" "$_DEFAULT" "$@")"
;;
yad)
_OPTIONS="$_DEFAULT"
for _OPTION; do
if [ "$_OPTION" != "$_DEFAULT" ]; then
_OPTIONS="$_OPTIONS\!$_OPTION"
fi
done
_RESULT="$(yad --form --text="$_PROMPT" --field="":CB "$_OPTIONS" | sed 's/|[[:space:]]*$//')"
;;
dialog)
if [ -n "$_DEFAULT" ]; then
_OPTIONS="\"$_DEFAULT\" \"1\" \"on\""
_COUNT=2
else
_COUNT=1
fi
i=1
while [ $i -le $# ]; do
_OPTION="$(eval "echo \$$i")"
if [ "$_OPTION" != "$_DEFAULT" ]; then
_OPTIONS="$_OPTIONS \"$_OPTION\" \"$_COUNT\" \"off\""
_COUNT="$((_COUNT + 1))"
fi
i="$((i + 1))"
done
_RESULT="$(eval dialog --radiolist \"$_PROMPT\" 15 40 10 $_OPTIONS 3>&1 1>&2 2>&3)"
;;
whiptail)
_RESULT="$(whiptail --menu "$_PROMPT" 15 40 10 "$_DEFAULT" "$@" 3>&1 1>&2 2>&3)"
;;
esac
fi
echo "$_RESULT"
}
_kwyzod_multiselect() {
_DEFAULT="$1"
_PROMPT="$2"
shift 2
set -- "$@"
_RESULT=""
_DIALOG=$(_kwyzod_get_dialog "osascript" "zenity" "kdialog" "yad" "dialog" "whiptail")
if [ -z "$_DIALOG" ]; then
i=1
for _OPTION; do
printf "%s) %s\n" "$i" "$_OPTION"
i="$((i + 1))"
done
printf "%s: " "$_PROMPT"
read -r _RESULTS
_RESULT=""
for _INDEX in $_RESULTS; do
_RESULT+="$(eval "echo \$$_INDEX")\n"
done
_RESULT="${_RESULT%\\n}"
else
case "$_DIALOG" in
osascript)
_OPTIONS="$(printf ", \"%s\"" "$@")"
_OPTIONS="$(echo "$_OPTIONS" | cut -c 3-)"
_RESULT="$(osascript -e "choose from list {$_OPTIONS} with prompt \"$_PROMPT\" with multiple selections allowed")"
_RESULT="$(echo "$_RESULT" | sed 's/, /\n/g')"
;;
zenity)
_RESULT="$(zenity --list --multiple --text="$_PROMPT" --column="Options" "$@" | sed 's/|/\n/g')"
;;
kdialog)
_RESULT="$(kdialog --checklist "$_PROMPT" "$@")"
;;
yad)
_OPTIONS=""
for _OPTION; do
_OPTIONS="$_OPTIONS --field=\"$_OPTION\":CHK"
done
_RESULT="$(eval yad --form --text=\"$_PROMPT\" $_OPTIONS)"
_RESULT="$(echo "$_RESULT" | tr '|' '\n')"
_INDEX=1
for _VALUE in $_RESULT; do
if [ "$_VALUE" = "TRUE" ]; then
_RESULT+="$(eval "echo \$$_INDEX")|"
fi
_INDEX="$((_INDEX + 1))"
done
_RESULT="$(echo "${_RESULT%?}" | sed 's/|/\n/g')"
;;
dialog)
_OPTIONS=""
i=1
while [ $i -le $# ]; do
_OPTIONS="$_OPTIONS \"$(eval "echo \$$i")\" \"$i\" \"off\""
i="$((i + 1))"
done
_RESULT="$(eval dialog --checklist \"$_PROMPT\" 15 40 10 $_OPTIONS 3>&1 1>&2 2>&3)"
_RESULT="$(echo "$_RESULT" | awk 'BEGIN{RS="\"";ORS="\n"}{if(NR%2==0){print $0}else{gsub(/ /,"\n");print $0}}' | awk 'NF')"
;;
whiptail)
_RESULT="$(whiptail --checklist "$_PROMPT" 15 40 10 "$@" 3>&1 1>&2 2>&3)"
;;
esac
fi
echo "$_RESULT"
}
main $@