forked from DrewThomasson/ebook2audiobook
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathebook2audiobook.command
More file actions
executable file
·1120 lines (1068 loc) · 37.7 KB
/
Copy pathebook2audiobook.command
File metadata and controls
executable file
·1120 lines (1068 loc) · 37.7 KB
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
: "${HOME:=$PWD}"
CURRENT_PYVENV=""
SWITCHED_TO_ZSH="${SWITCHED_TO_ZSH:-0}"
if [[ "${OSTYPE:-}" == darwin* && "$SWITCHED_TO_ZSH" -eq 0 && "$(ps -p $$ -o comm= 2>/dev/null || true)" != "zsh" ]]; then
export SWITCHED_TO_ZSH=1
exec env zsh "$0" "$@"
fi
if [[ -n "${BASH_SOURCE:-}" ]]; then
script_path="${BASH_SOURCE[0]}"
elif [[ -n "${ZSH_VERSION:-}" ]]; then
script_path="${(%):-%x}"
else
script_path="$0"
fi
case "$(uname -m)" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*) ARCH="$(uname -m)" ;;
esac
export ARCH
export DOCKER_DEFAULT_PLATFORM="linux/${ARCH}"
export BASHRCSOURCED="1"
export SCRIPT_DIR="$(cd "$(dirname "$script_path")" >/dev/null 2>&1 && pwd -P)"
export PYTHONUTF8="1"
export PYTHONIOENCODING="utf-8"
export TTS_CACHE="$SCRIPT_DIR/models"
export TESSDATA_PREFIX="$SCRIPT_DIR/models/tessdata"
export TMPDIR="$SCRIPT_DIR/run"
export APP_VERSION=$(<"$SCRIPT_DIR/VERSION.txt")
export DEVICE_TAG="${DEVICE_TAG:-}"
export CONDA_HOME="$HOME/Miniforge3"
export CONDA_BIN_PATH="$CONDA_HOME/bin"
export CONDA_ENV="$CONDA_HOME/etc/profile.d/conda.sh"
export PATH="$CONDA_BIN_PATH:${PATH-}"
export PODMAN_DESKTOP="0"
export DOCKER_DESKTOP="0"
export DOCKER_DEVICE_STR=""
export RENDER_GID="${RENDER_GID:-}"
export VIDEO_GID="${VIDEO_GID:-}"
export DEVICE_INFO_STR=""
export HOMEBREW_NO_ENV_HINTS="1"
export SUDO="sudo"
export SETVARS_CALL=""
export ETVARS_ARGS=""
export SETVARS_COMPLETED=""
NATIVE="native"
BUILD_DOCKER="build_docker"
FULL_DOCKER="full_docker"
MIN_PYTHON_VERSION="3.10"
MAX_PYTHON_VERSION="3.12"
PYTHON_VERSION="$MAX_PYTHON_VERSION"
PYTHON_ENV="python_env"
SCRIPT_MODE="$NATIVE"
APP_NAME="ebook2audiobook"
OS_LANG=$(echo "${LANG:-en}" | cut -d_ -f1 | tr '[:upper:]' '[:lower:]')
HOST_PROGRAMS=("cmake" "curl" "pkg-config" "xcb-util-cursor" "calibre" "ffmpeg" "mediainfo" "nodejs" "espeak-ng" "cargo" "rust" "sox" "tesseract")
DOCKER_PROGRAMS=("curl" "ffmpeg" "mediainfo" "nodejs" "espeak-ng" "sox" "tesseract-ocr") # tesseract-ocr-[lang] and calibre are hardcoded in Dockerfile
DOCKER_MODE=""
DOCKER_IMG_NAME="athomasson2/$APP_NAME"
CALIBRE_INSTALLER_URL="https://download.calibre-ebook.com/linux-installer.sh"
BREW_INSTALLER_URL="https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"
MINIFORGE_MACOSX_INSTALLER_URL="https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-$(uname -m).sh"
MINIFORGE_LINUX_INSTALLER_URL="https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
RUST_INSTALLER_URL="https://sh.rustup.rs"
INSTALLED_LOG="$SCRIPT_DIR/.installed"
UNINSTALLER="$SCRIPT_DIR/uninstall.sh"
WGET="$(command -v wget 2>/dev/null || true)"
typeset -A arguments=() # associative array
typeset -a programs_missing=() # indexed array
PACK_MGR=""
PACK_MGR_OPTIONS=""
BUILD_NAME=""
ISO3_LANG="eng"
# Validate command arguments against conf.py
if [ $# -gt 0 ]; then
VALID_ARGS=$(python3 -c 'from lib.conf import cli_options; print(" ".join(cli_options))')
for arg in "$@"; do
if [ "${arg:0:2}" = "--" ]; then
if ! echo " $VALID_ARGS " | grep -q " $arg "; then
echo "ERROR: Unknown option \"$arg\""
exit 1
fi
fi
done
fi
ARGS=("$@")
# Parse arguments
while (( $# > 0 )); do
case "$1" in
--*)
key="${1#--}"
if (( $# > 1 )) && [[ "$2" != --* ]]; then
arguments[$key]=$2
shift 2
continue
else
arguments[$key]=true
shift
continue
fi
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
if [[ -n "${arguments[script_mode]+exists}" ]]; then
if [[ "${arguments[script_mode]}" == "$BUILD_DOCKER" || "${arguments[script_mode]}" == "$FULL_DOCKER" || "${arguments[script_mode]}" == "$NATIVE" ]]; then
SCRIPT_MODE="${arguments[script_mode]}"
else
echo "Error: Invalid script mode argument: ${arguments[script_mode]}"
exit 1
fi
fi
if [[ "$SCRIPT_MODE" == "$BUILD_DOCKER" ]]; then
if [[ -n "${ZSH_VERSION:-}" ]]; then
for key in ${(k)arguments}; do
if [[ "$key" != "script_mode" && "$key" != "docker_device" && "$key" != "docker_mode" ]]; then
echo "Error: when --script_mode is $BUILD_DOCKER, only --docker_device or --docker_mode are allowed. Invalid: --$key"
exit 1
fi
done
else
for key in "${!arguments[@]}"; do
if [[ "$key" != "script_mode" && "$key" != "docker_device" && "$key" != "docker_mode" ]]; then
echo "Error: when --script_mode is $BUILD_DOCKER, only --docker_device or --docker_mode are allowed. Invalid: --$key"
exit 1
fi
done
fi
if [[ -n "${arguments[docker_mode]+exists}" ]]; then
DOCKER_MODE="${arguments[docker_mode]}"
if [[ "$DOCKER_MODE" != "podman" && "$DOCKER_MODE" != "compose" ]]; then
if [[ "$DOCKER_MODE" == "" ]]; then
echo "Error: --docker_mode has no value!"
else
echo "Error: --docker_mode accepts only podman or compose as value"
fi
exit 1
fi
fi
if [[ -n "${arguments[docker_device]+exists}" ]]; then
DOCKER_DEVICE_STR="${arguments[docker_device]}"
if [[ "$DOCKER_DEVICE_STR" == "" ]]; then
echo "Error: --docker_device has no value!"
exit 1
fi
fi
fi
[[ "${OSTYPE-}" != darwin* && "$SCRIPT_MODE" != "$BUILD_DOCKER" ]] && SUDO="sudo" || SUDO=""
[[ "${OSTYPE-}" == darwin* ]] && SHELL_NAME="zsh" || SHELL_NAME="bash"
cd "$SCRIPT_DIR"
if [[ "$SCRIPT_MODE" == "$FULL_DOCKER" ]]; then
USER="${USER:-root}"
HOME="${HOME:-/root}"
SUDO=""
fi
if [[ ! -f "$INSTALLED_LOG" && "$SCRIPT_MODE" != "$BUILD_DOCKER" ]]; then
touch "$INSTALLED_LOG"
fi
######## check if the user is part of the read/write group
if [[ -n "${arguments[headless]+exists}" && ! -n "${arguments[script_mode]+exists}" ]]; then
PUBLIC_DIRS=("$SCRIPT_DIR/tmp" "$SCRIPT_DIR/models" "$SCRIPT_DIR/audiobooks")
if [[ "$OSTYPE" == "darwin"* ]]; then
APP_GROUP=$(stat -f '%Sg' "$SCRIPT_DIR")
else
APP_GROUP=$(stat -c '%G' "$SCRIPT_DIR")
fi
user_in_group() {
if [[ -n "${USER:-}" ]]; then
id -nG "$USER" 2>/dev/null | tr ' ' '\n' | grep -qx "$1"
else
return 1
fi
}
if [[ -n "${USER:-}" ]] && ! user_in_group "$APP_GROUP"; then
echo "Adding $USER to group $APP_GROUP (requires sudo)..."
if [[ "$OSTYPE" == "darwin"* ]]; then
sudo dseditgroup -o edit -a "$USER" -t user "$APP_GROUP"
echo "Group added. Please restart your terminal and re-run:"
echo " $0 $*"
exit 0
else
sudo usermod -aG "$APP_GROUP" "$USER"
exec sg "$APP_GROUP" -c "\"$0\" $*"
fi
fi
fi
if [[ -n "${arguments[version]+exists}" ]]; then
echo "v${APP_VERSION}"
exit 0
fi
############### FUNCTIONS ##############
###### DESKTOP APP
function has_no_display {
if [[ "${OSTYPE:-}" == darwin* ]]; then
if pgrep -x WindowServer >/dev/null 2>&1 &&
[[ "$(launchctl managername 2>/dev/null)" == "Aqua" ]]; then
return 0 # macOS GUI
else
return 1 # SSH or console mode
fi
else
if [[ -n "${SSH_CONNECTION-}" || -n "${SSH_CLIENT-}" || -n "${SSH_TTY-}" ]]; then
return 1
fi
if [[ -z "${DISPLAY-}" && -z "${WAYLAND_DISPLAY-}" ]]; then
return 1 # No display server → headless
fi
if pgrep -x vncserver >/dev/null 2>&1 || \
pgrep -x Xvnc >/dev/null 2>&1 || \
pgrep -x x11vnc >/dev/null 2>&1 || \
pgrep -x Xtightvnc >/dev/null 2>&1 || \
pgrep -x Xtigervnc >/dev/null 2>&1 || \
pgrep -x Xrealvnc >/dev/null 2>&1; then
return 0
fi
if pgrep -x gnome-shell >/dev/null 2>&1 || \
pgrep -x plasmashell >/dev/null 2>&1 || \
pgrep -x xfce4-session >/dev/null 2>&1 || \
pgrep -x cinnamon >/dev/null 2>&1 || \
pgrep -x mate-session >/dev/null 2>&1 || \
pgrep -x lxsession >/dev/null 2>&1 || \
pgrep -x openbox >/dev/null 2>&1 || \
pgrep -x i3 >/dev/null 2>&1 || \
pgrep -x sway >/dev/null 2>&1 || \
pgrep -x hyprland >/dev/null 2>&1 || \
pgrep -x wayfire >/dev/null 2>&1 || \
pgrep -x river >/dev/null 2>&1 || \
pgrep -x fluxbox >/dev/null 2>&1; then
return 0 # Desktop environment detected
fi
return 1
fi
}
function open_desktop_app {
(
host=127.0.0.1
port=7860
url="http://$host:$port/"
timeout=120
start_time=$(date +%s)
while ! nc -z "$host" "$port" >/dev/null 2>&1; do
sleep 1
elapsed=$(( $(date +%s) - start_time ))
if [[ "$elapsed" -ge "$timeout" ]]; then
exit 0
fi
done
if [[ "${OSTYPE-}" == darwin* ]]; then
open "$url" >/dev/null 2>&1 &
elif command -v xdg-open >/dev/null 2>&1; then
xdg-open "$url" >/dev/null 2>&1 &
elif command -v gio >/dev/null 2>&1; then
gio open "$url" >/dev/null 2>&1 &
elif command -v x-www-browser >/dev/null 2>&1; then
x-www-browser "$url" >/dev/null 2>&1 &
else
echo "No method found to open the default web browser." >&2
fi
exit 0
) &
}
function mac_app {
local APP_BUNDLE="$HOME/Applications/$APP_NAME.app"
local CONTENTS="$APP_BUNDLE/Contents"
local MACOS="$CONTENTS/MacOS"
local RESOURCES="$CONTENTS/Resources"
local DESKTOP_DIR="$(osascript -e 'POSIX path of (path to desktop folder)' 2>/dev/null | sed 's:/$::')"
local DESKTOP_SHORTCUT="$DESKTOP_DIR/$APP_NAME"
local ICON_PATH="$SCRIPT_DIR/tools/icons/mac/appIcon.icns"
local OPEN_DESKTOP_APP_DEF=$(typeset -f open_desktop_app)
local ESCAPED_APP_ROOT=$(printf '%q' "$SCRIPT_DIR") # Escape SCRIPT_DIR safely for AppleScript
if [[ -d "$APP_BUNDLE" ]]; then
open_desktop_app
return 0
fi
[[ -d "$HOME/Applications" ]] || mkdir "$HOME/Applications"
if [[ ! -d "$MACOS" || ! -d "$RESOURCES" ]]; then
mkdir -p "$MACOS" "$RESOURCES"
fi
cat > "$MACOS/$APP_NAME" << EOF
#!/bin/zsh
$OPEN_DESKTOP_APP_DEF
open_desktop_app
# TODO: replace osascript when log will be available in gradio with
#
# cd "$SCRIPT_DIR"
# ./ebook2audiobook.sh
osascript -e '
tell application "Terminal"
do script "cd \"${ESCAPED_APP_ROOT}\" && ./ebook2audiobook.sh"
activate
end tell
'
EOF
chmod +x "$MACOS/$APP_NAME"
cp "$ICON_PATH" "$RESOURCES/AppIcon.icns"
cat > "$CONTENTS/Info.plist" << 'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>ebook2audiobook</string>
<key>CFBundleIdentifier</key>
<string>com.local.ebook2audiobook</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>ebook2audiobook</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>10.9</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
</dict>
</plist>
PLIST
ln -sf "$APP_BUNDLE" "$DESKTOP_SHORTCUT"
echo -e "Next launch in GUI mode you just need to double click on the desktop shortcut or go to the launchpad and click on ebook2audiobook icon."
open_desktop_app
}
function linux_app {
local MENU_ENTRY="$HOME/.local/share/applications/$APP_NAME.desktop"
local DESKTOP_DIR="$(xdg-user-dir DESKTOP 2>/dev/null || echo "$HOME/Desktop")"
local DESKTOP_SHORTCUT="$DESKTOP_DIR/$APP_NAME.desktop"
local ICON_PATH="$SCRIPT_DIR/tools/icons/linux/appIcon"
if [[ -f "$MENU_ENTRY" ]]; then
open_desktop_app
return 0
fi
mkdir -p "$HOME/.local/share/applications"
cat > "$MENU_ENTRY" <<EOF
[Desktop Entry]
Type=Application
Name=ebook2audiobook
Exec=$SCRIPT_DIR/ebook2audiobook.sh
Icon=$ICON_PATH
Terminal=true
Categories=Utility;
EOF
chmod +x "$MENU_ENTRY"
mkdir -p "$HOME/Desktop" 2>&1 > /dev/null
cp "$MENU_ENTRY" "$DESKTOP_SHORTCUT"
chmod +x "$DESKTOP_SHORTCUT"
if command -v update-desktop-database >/dev/null 2>&1; then
update-desktop-database ~/.local/share/applications >/dev/null 2>&1
fi
echo -e "Next launch in GUI mode you just need to double click on the desktop shortcut or go to menu entry and click on ebook2audiobook icon."
open_desktop_app
}
function check_desktop_app {
if [[ " ${ARGS[*]} " == *" --headless "* ]] || ! has_no_display; then
return 0
fi
if [[ "${OSTYPE-}" == darwin* ]]; then
mac_app
elif [[ "${OSTYPE-}" == linux* ]]; then
linux_app
fi
return 0
}
#################
function get_iso3_lang {
case "$1" in
en) echo "eng" ;;
fr) echo "fra" ;;
de) echo "deu" ;;
it) echo "ita" ;;
es) echo "spa" ;;
pt) echo "por" ;;
ar) echo "ara" ;;
tr) echo "tur" ;;
ru) echo "rus" ;;
bn) echo "ben" ;;
zh) echo "chi_sim" ;;
fa) echo "fas" ;;
hi) echo "hin" ;;
hu) echo "hun" ;;
id) echo "ind" ;;
jv) echo "jav" ;;
ja) echo "jpn" ;;
ko) echo "kor" ;;
pl) echo "pol" ;;
ta) echo "tam" ;;
te) echo "tel" ;;
yo) echo "yor" ;;
*) echo "eng" ;;
esac
}
function check_python {
if ! command -v python3 &>/dev/null; then
echo 'Python is not installed.'
return 1
fi
local installed_version
installed_version=$(python3 --version 2>&1 | awk '{print $2}')
local IFS='.'
read -r ins_major ins_minor ins_patch <<< "$installed_version"
read -r req_major req_minor req_patch <<< "$MIN_PYTHON_VERSION"
ins_patch="${ins_patch%%[!0-9]*}"
ins_patch="${ins_patch:-0}"
req_patch="${req_patch%%[!0-9]*}"
req_patch="${req_patch:-0}"
if [ "$ins_major" -lt "$req_major" ] ||
[ "$ins_major" -eq "$req_major" -a "$ins_minor" -lt "$req_minor" ] ||
[ "$ins_major" -eq "$req_major" -a "$ins_minor" -eq "$req_minor" -a "$ins_patch" -lt "$req_patch" ]; then
echo "Python $installed_version found but $MIN_PYTHON_VERSION or higher is required."
return 1
fi
return 0
}
function check_required_programs {
local programs=("$@")
programs_missing=()
for program in "${programs[@]}"; do
local pkg="$program"
local bin="$program"
# Normalize special binaries
[[ "$program" == "nodejs" ]] && bin="node"
[[ "$program" == "rust" ]] && bin="rustc"
# Special case: tesseract OCR
if [[ "$program" == "tesseract" || "$program" == "tesseract-ocr" ]]; then
bin="tesseract"
if command -v zypper >/dev/null 2>&1 || command -v apt-get >/dev/null 2>&1 || command -v apk >/dev/null 2>&1; then
pkg="tesseract-ocr"
else
pkg="$program"
fi
elif [[ "$program" == "xcb-util-cursor" ]]; then
bin=""
if [[ "${OSTYPE-}" != darwin* ]]; then
if command -v apt-get >/dev/null 2>&1 || command -v zypper >/dev/null 2>&1; then
pkg="libxcb-cursor0"
elif command -v apk >/dev/null 2>&1; then
pkg="xcb-util-cursor"
else
pkg="$program"
fi
check_xcb=$(ldconfig -p 2>/dev/null | grep libxcb-cursor)
if [[ "$check_xcb" == "" ]]; then
programs_missing+=("$pkg")
fi
fi
fi
if [[ "$bin" != "" ]]; then
if ! command -v "$bin" &>/dev/null; then
echo -e "\e[33m$pkg is not installed.\e[0m"
programs_missing+=("$pkg")
fi
fi
done
(( ${#programs_missing[@]} == 0 ))
}
function install_programs {
if [[ "${OSTYPE-}" == darwin* ]]; then
echo -e "\e[33mInstalling required programs…\e[0m"
PACK_MGR="brew install --force"
if ! command -v brew &> /dev/null; then
echo -e "\e[33mHomebrew is not installed. Installing Homebrew…\e[0m"
/usr/bin/env bash -c "$(curl -fsSL $BREW_INSTALLER_URL)"
echo >> $HOME/.zprofile
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> $HOME/.zprofile
eval "$(/usr/local/bin/brew shellenv)"
if ! grep -iqFx "homebrew" "$INSTALLED_LOG"; then
echo "homebrew" >> "$INSTALLED_LOG"
fi
fi
if ! brew list --versions llvm@15 >/dev/null 2>&1; then
echo "Installing llvm@15 (required for numba/llvmlite on macOS)"
brew install llvm@15
export LLVM_DIR="$(brew --prefix llvm@15)/lib/cmake/llvm"
export PATH="$(brew --prefix llvm@15)/bin:$PATH"
fi
else
if [[ "$SUDO" == "sudo" ]]; then
echo -e "\e[33mInstalling required programs. NOTE: you must have 'sudo' priviliges to install ebook2audiobook.\e[0m"
fi
local PACK_MGR_OPTIONS=""
if command -v emerge &> /dev/null; then
PACK_MGR="emerge"
elif command -v dnf &> /dev/null; then
PACK_MGR="dnf install"
PACK_MGR_OPTIONS="-y"
elif command -v yum &> /dev/null; then
PACK_MGR="yum install"
PACK_MGR_OPTIONS="-y"
elif command -v zypper &> /dev/null; then
PACK_MGR="zypper install"
PACK_MGR_OPTIONS="-y"
elif command -v pacman &> /dev/null; then
PACK_MGR="pacman -Sy --noconfirm"
elif command -v apt-get &> /dev/null; then
$SUDO apt-get update
PACK_MGR="apt-get install"
PACK_MGR_OPTIONS="-y"
elif [[ -f /etc/unraid-version ]] || command -v installplg &>/dev/null; then
if ! command -v un-get &>/dev/null; then
echo " → Installing un-get plugin…"
installplg ./ext/app/un-get.plg
# Add the two best repos for Unraid 7 (current as of Dec 2025)
mkdir -p /boot/config/plugins/un-get
cat > /boot/config/plugins/un-get/sources.list <<EOF
https://slackware.uk/slackware/slackware64-current/
https://slackware.uk/people/shinji257/unraid7/
EOF
sleep 8
fi
PACK_MGR="un-get install"
elif command -v apk &>/dev/null; then
PACK_MGR="apk add"
else
echo "Cannot recognize your applications package manager. Please install the required applications manually."
return 1
fi
fi
if [[ -z "$WGET" ]]; then
echo -e "\e[33m wget is missing! trying to install it… \e[0m"
result=$(eval "$PACK_MGR wget $PACK_MGR_OPTIONS" 2>&1)
result_code=$?
if [[ $result_code -eq 0 ]]; then
WGET="$(command -v wget 2>/dev/null || true)"
else
echo "Cannot 'wget'. Please install 'wget' manually."
return 1
fi
fi
for program in "${programs_missing[@]}"; do
if [[ "$program" == "calibre" ]]; then
if command -v $program >/dev/null 2>&1; then
echo -e "\e[32m=============== Calibre OK! ===============\e[0m"
else
# avoid conflict with calibre builtin lxml
python3 -m pip uninstall -y lxml 2>/dev/null || true
echo -e "\e[33mInstalling Calibre…\e[0m"
if [[ "${OSTYPE-}" == darwin* ]]; then
eval "$PACK_MGR --cask calibre"
else
tmp="$(mktemp)"
$WGET -nv -O "$tmp" "$CALIBRE_INSTALLER_URL" || return 1
if [[ "$SUDO" == "sudo" ]]; then
$SUDO sh "$tmp"
else
sh "$tmp"
fi
rm -f "$tmp"
fi
eval "$SUDO $PACK_MGR $program $PACK_MGR_OPTIONS"
if command -v $program >/dev/null 2>&1; then
echo -e "\e[32m=============== $program OK! ===============\e[0m"
else
echo -e "\e[31m=============== $program failed.\e[0m"
fi
fi
elif [[ "$program" == "rust" || "$program" == "rustc" ]]; then
RUSTUP_TMP="$(mktemp)"
curl -fL "$RUST_INSTALLER_URL" -o "$RUSTUP_TMP" || return 1
sh "$RUSTUP_TMP" -y
rm -f "$RUSTUP_TMP"
if [[ -f "$HOME/.cargo/env" ]]; then
source "$HOME/.cargo/env"
fi
if command -v $program &>/dev/null; then
echo -e "\e[32m=============== $program OK! ===============\e[0m"
else
echo -e "\e[31m=============== $program failed.\e[0m"
fi
elif [[ "$program" == "tesseract" || "$program" == "tesseract-ocr" ]]; then
eval "$SUDO $PACK_MGR $program $PACK_MGR_OPTIONS"
if command -v $program >/dev/null 2>&1; then
echo -e "\e[32m=============== $program OK! ===============\e[0m"
ISO3_LANG="$(get_iso3_lang "${OS_LANG:-en}")"
echo "Detected system language: $OS_LANG → installing Tesseract OCR language: $ISO3_LANG"
langpack=""
if command -v brew &> /dev/null; then
langpack="tesseract-lang-$ISO3_LANG"
elif command -v apt-get &>/dev/null; then
langpack="tesseract-ocr-$ISO3_LANG"
elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then
langpack="tesseract-langpack-$ISO3_LANG"
elif command -v zypper &>/dev/null; then
langpack="tesseract-ocr-$ISO3_LANG"
elif command -v pacman &>/dev/null; then
langpack="tesseract-data-$ISO3_LANG"
elif command -v apk &>/dev/null; then
langpack="tesseract-ocr-$ISO3_LANG"
else
echo "Cannot recognize your applications package manager. Please install the required applications manually."
return 1
fi
if [[ -n "$langpack" ]]; then
eval "$SUDO $PACK_MGR $langpack $PACK_MGR_OPTIONS"
if tesseract --list-langs | grep -q "$ISO3_LANG"; then
echo "Tesseract OCR language '$ISO3_LANG' successfully installed."
else
echo "Tesseract OCR language '$ISO3_LANG' not installed properly."
fi
fi
else
echo -e "\e[31m=============== $program failed.\e[0m"
fi
elif [[ "$program" == "nodejs" ]]; then
eval "$SUDO $PACK_MGR $program $PACK_MGR_OPTIONS"
if command -v node >/dev/null 2>&1; then
echo -e "\e[32m=============== $program OK! ===============\e[0m"
else
echo -e "\e[31m=============== $program failed.\e[0m"
fi
else
eval "$SUDO $PACK_MGR $program $PACK_MGR_OPTIONS"
if command -v $program >/dev/null 2>&1; then
echo -e "\e[32m=============== $program OK! ===============\e[0m"
else
echo -e "\e[31m=============== $program failed.\e[0m"
fi
fi
done
if check_required_programs "${HOST_PROGRAMS[@]}"; then
return 0
else
echo "Some programs didn't install successfuly, please report the log to the support"
fi
}
function check_conda {
function compare_versions {
local ver1=$1
local ver2=$2
IFS='.' read -r v1_major v1_minor <<<"$ver1"
IFS='.' read -r v2_major v2_minor <<<"$ver2"
((v1_major < v2_major)) && return 1
((v1_major > v2_major)) && return 2
((v1_minor < v2_minor)) && return 1
((v1_minor > v2_minor)) && return 2
return 0
}
local conda_owned=0
if ! command -v conda &>/dev/null; then
local installer_url
local installer_path="/tmp/Miniforge3.sh"
local config_path
echo -e "\e[33mDownloading Miniforge3 installer…\e[0m"
if [[ "${OSTYPE-}" == darwin* ]]; then
config_path="$HOME/.zshrc"
curl -fsSLo "$installer_path" "$MINIFORGE_MACOSX_INSTALLER_URL"
else
config_path="$HOME/.bashrc"
wget -O "$installer_path" "$MINIFORGE_LINUX_INSTALLER_URL"
fi
if [[ ! -f "$installer_path" ]]; then
echo -e "\e[31m=============== Miniforge3 installer not found!\e[0m"
return 1
fi
echo -e "\e[33mInstalling Miniforge3…\e[0m"
bash "$installer_path" -b -u -p "$CONDA_HOME"
rm -f "$installer_path"
if [[ ! -f "$CONDA_HOME/bin/conda" ]]; then
echo -e "\e[31m=============== Miniforge3 failed.\e[0m"
return 1
fi
if [[ ! -f "$HOME/.condarc" ]]; then
"$CONDA_HOME/bin/conda" config --set auto_activate_base false
fi
[[ -f "$config_path" ]] || touch "$config_path"
if ! grep -qxF 'export PATH="$HOME/Miniforge3/bin:$PATH"' "$config_path"; then
echo 'export PATH="$HOME/Miniforge3/bin:$PATH"' >> "$config_path"
fi
case ":$PATH:" in
*":$HOME/Miniforge3/bin:"*) ;;
*) export PATH="$HOME/Miniforge3/bin:$PATH" ;;
esac
echo -e "\e[32m=============== Miniforge3 OK! ===============\e[0m"
if ! grep -iqFx "Miniforge3" "$INSTALLED_LOG"; then
echo "Miniforge3" >> "$INSTALLED_LOG"
fi
conda_owned=1
fi
local detected_base
detected_base="$(conda info --base 2>/dev/null || true)"
if [[ -z "$detected_base" ]]; then
echo -e "\e[31m=============== Failed to query 'conda info --base'.\e[0m"
return 1
fi
export CONDA_HOME="$detected_base"
export CONDA_BIN_PATH="$CONDA_HOME/bin"
export CONDA_ENV="$CONDA_HOME/etc/profile.d/conda.sh"
case ":$PATH:" in
*":$CONDA_BIN_PATH:"*) ;;
*) export PATH="$CONDA_BIN_PATH:$PATH" ;;
esac
if [[ ! -f "$CONDA_ENV" ]]; then
echo -e "\e[31m=============== conda.sh not found at $CONDA_ENV.\e[0m"
return 1
fi
if [[ ! -f "$SCRIPT_DIR/$PYTHON_ENV/.provisioned" ]]; then
if [[ -d "$SCRIPT_DIR/$PYTHON_ENV" ]]; then
echo -e "\e[33mDetected incomplete $PYTHON_ENV — removing and recreating…\e[0m"
rm -rf "$SCRIPT_DIR/$PYTHON_ENV"
fi
local model="other"
if [[ "${OSTYPE-}" == darwin* && "$ARCH" == "x86_64" ]]; then
PYTHON_VERSION="3.11"
else
if [[ -r /proc/device-tree/model ]]; then
model="$(tr -d '\0' </proc/device-tree/model 2>/dev/null | tr 'A-Z' 'a-z' || true)"
if [[ "$model" == *jetson* ]]; then
PYTHON_VERSION="$MIN_PYTHON_VERSION"
fi
else
compare_versions "$PYTHON_VERSION" "$MIN_PYTHON_VERSION"
case $? in 1) PYTHON_VERSION="$MIN_PYTHON_VERSION" ;; esac
compare_versions "$PYTHON_VERSION" "$MAX_PYTHON_VERSION"
case $? in 2) PYTHON_VERSION="$MAX_PYTHON_VERSION" ;; esac
fi
fi
echo -e "\e[33mCreating ./$PYTHON_ENV with python $PYTHON_VERSION…\e[0m"
chmod -R 775 "$SCRIPT_DIR/audiobooks" "$SCRIPT_DIR/tmp" "$SCRIPT_DIR/models" 2>/dev/null || true
chmod g+s "$SCRIPT_DIR/audiobooks" "$SCRIPT_DIR/tmp" "$SCRIPT_DIR/models" 2>/dev/null || true
source "$CONDA_ENV" || return 1
if (( conda_owned == 1 )); then
conda update -n base -c conda-forge conda -y
conda update --all -y
conda clean --index-cache -y
conda clean --packages --tarballs -y
fi
conda create --prefix "$SCRIPT_DIR/$PYTHON_ENV" -c conda-forge python=$PYTHON_VERSION pip -y || return 1
set +u
conda activate "$SCRIPT_DIR/$PYTHON_ENV" || return 1
set -u
if [[ "${OSTYPE-}" != darwin* && "$model" == *jetson* ]]; then
# gfortran needed to compile scipy from pip on Jetson
conda install -c conda-forge gfortran -y || return 1
fi
DEVICE_INFO_STR="$(check_device_info "$SCRIPT_MODE")"
if [[ -z "$DEVICE_INFO_STR" ]]; then
echo "check_device_info() error: result is empty"
return 1
fi
install_device_packages "$DEVICE_INFO_STR" || return 1
install_python_packages || return 1
echo "$APP_VERSION" > "$SCRIPT_DIR/$PYTHON_ENV/.provisioned"
conda deactivate &>/dev/null || true
conda deactivate &>/dev/null || true
fi
return 0
}
function check_docker {
if [[ "$DOCKER_MODE" == "podman" ]]; then
if command -v podman-compose &> /dev/null; then
PODMAN_DESKTOP="1"
return 0
fi
echo -e "\e[31m=============== Podman is not installed.\e[0m"
return 1
fi
if command -v docker &> /dev/null; then
DOCKER_DESKTOP="1"
return 0
fi
echo -e "\e[31m=============== Docker is not installed.\e[0m"
return 1
}
function install_python_packages {
echo "Installing python dependencies…"
PYTHONPATH="$SCRIPT_DIR" python3 -c "import sys; from lib.classes.device_installer import DeviceInstaller; device = DeviceInstaller(); sys.exit(device.install_python_packages())"
return $?
}
function check_device_info {
local ARG="$1"
python3 - << EOF
from lib.classes.device_installer import DeviceInstaller
device = DeviceInstaller()
result = device.check_device_info("$ARG")
if result:
print(result)
raise SystemExit(0)
raise SystemExit(1)
EOF
}
function json_get {
local key="$1"
echo "$DEVICE_INFO_STR" | python3 -c "
import sys, json
data = json.load(sys.stdin)
print(data['$key'])
"
}
function install_device_packages {
local ARG="$1"
python3 - "$ARG" << 'EOF'
import sys,json
from lib.classes.device_installer import DeviceInstaller
device = DeviceInstaller()
data = sys.argv[1]
exit_code = device.install_device_packages(data)
sys.exit(exit_code)
EOF
}
function check_sitecustomized {
local src_pyfile="$SCRIPT_DIR/components/sitecustomize.py"
local site_packages_path=$(python3 -c "import sysconfig;print(sysconfig.get_paths()['purelib'])")
local dst_pyfile="$site_packages_path/sitecustomize.py"
if [ ! -f "$dst_pyfile" ] || [ "$src_pyfile" -nt "$dst_pyfile" ]; then
if cp -p "$src_pyfile" "$dst_pyfile"; then
echo "Installed sitecustomize.py hook in $dst_pyfile"
else
echo -e "\e[31m=============== sitecustomize.py hook error: copy failed.\e[0m" >&2
exit 1
fi
fi
return 0
}
function get_dri_gids {
local node
RENDER_GID=""
VIDEO_GID=""
if [[ "${OSTYPE-}" == linux* ]]; then
for node in /dev/dri/renderD*; do
if [[ ! -c "$node" ]]; then
continue
fi
RENDER_GID="$(stat -c '%g' "$node" 2>/dev/null || true)"
if [[ -n "$RENDER_GID" ]]; then
break
fi
done
for node in /dev/dri/card*; do
if [[ ! -c "$node" ]]; then
continue
fi
VIDEO_GID="$(stat -c '%g' "$node" 2>/dev/null || true)"
if [[ -n "$VIDEO_GID" ]]; then
break
fi
done
fi
export RENDER_GID VIDEO_GID
{
if [[ -n "$RENDER_GID" ]]; then
printf 'RENDER_GID=%s\n' "$RENDER_GID"
fi
if [[ -n "$VIDEO_GID" ]]; then
printf 'VIDEO_GID=%s\n' "$VIDEO_GID"
fi
} > "$SCRIPT_DIR/.env"
return 0
}
function build_docker_image {
local ARG="$1"
if [[ "$ARG" == "" ]]; then
echo "build_docker_image() error: ARG is empty"
return 1
fi
local cmd_options=""
local py_vers
# Base-image Python MUST match the wheel ABI in the device profile: derive it from
# pyvenv in $ARG (the single source of truth), not from $DEVICE_TAG.
py_vers="$(printf '%s' "$ARG" | python3 -c 'import json,sys; v=json.load(sys.stdin).get("pyvenv"); print(f"{v[0]}.{v[1]}" if v else "")' 2>/dev/null)"
[[ -z "$py_vers" ]] && py_vers="$PYTHON_VERSION"
# Compose reads build args ONLY from the compose file build.args, resolved from the
# environment -- it ignores PODMAN_BUILD_ARGS and loose --build-arg flags. Export every
# value the Dockerfile ARGs consume so the device profile + matching Python reach the
# image instead of the Dockerfile defaults (a generic cu130 / python3.12).
export PYTHON_VERSION="$py_vers"
export DOCKER_DEVICE_STR="$ARG"
export DOCKER_PROGRAMS_STR="${DOCKER_PROGRAMS[*]}"
export CALIBRE_INSTALLER_URL ISO3_LANG
case "$DEVICE_TAG" in
cpu) cmd_options="";;
cu*) cmd_options="--gpus all" ;;
rocm*) cmd_options="--device=/dev/kfd --device=/dev/dri" ;;
jetson*) cmd_options="--runtime nvidia --gpus all" ;;
xpu) cmd_options="--device=/dev/dri" ;;
esac
ISO3_LANG="$(get_iso3_lang "${OS_LANG:-en}")"
DOCKER_IMG_NAME="${DOCKER_IMG_NAME}:${DEVICE_TAG}"
case "$DEVICE_TAG" in
cpu|mps) COMPOSE_PROFILES=cpu ;;
cu*) COMPOSE_PROFILES=cuda ;;
rocm*) COMPOSE_PROFILES=rocm ;;
jetson*) COMPOSE_PROFILES=jetson ;;
xpu) COMPOSE_PROFILES=xpu ;;
*) COMPOSE_PROFILES=cpu ;;
esac
export COMPOSE_PROFILES
get_dri_gids
SERVICE="ebook2audiobook-${COMPOSE_PROFILES}"
if [[ "$DOCKER_MODE" == "podman" ]]; then
if ! command -v podman &>/dev/null; then
echo "ERROR: podman is not installed"
return 1
fi
# podman-compose is only needed to RUN the image, not to build it
if ! command -v podman-compose &>/dev/null || ! podman-compose -f podman-compose.yml config &>/dev/null; then
echo "WARNING: podman-compose is missing or podman-compose.yml is not valid -- the image will still build, but you will not be able to run it with podman-compose"
fi
elif [[ "$DOCKER_MODE" == "compose" ]]; then
if ! docker compose config --services 2>/dev/null | grep -q .; then
echo "ERROR: docker compose found no services or yml file is not valid."
return 1
fi
fi
if [[ "$DOCKER_MODE" == "podman" ]]; then
echo "--> Using podman build"
podman \
build \
--network=host \
--no-cache \
-t "$DOCKER_IMG_NAME" \
-f Dockerfile \
--build-arg PYTHON_VERSION="$py_vers" \
--build-arg APP_VERSION="$APP_VERSION" \
--build-arg DEVICE_TAG="$DEVICE_TAG" \
--build-arg DOCKER_DEVICE_STR="$ARG" \
--build-arg DOCKER_PROGRAMS_STR="${DOCKER_PROGRAMS[*]}" \
--build-arg CALIBRE_INSTALLER_URL="$CALIBRE_INSTALLER_URL" \
--build-arg ISO3_LANG="$ISO3_LANG" \
. || return 1
echo "Docker image ready! to run your docker: "
echo "Podman Compose:"
echo " GUI mode:"
echo " DEVICE_TAG=$DEVICE_TAG podman-compose -f podman-compose.yml --profile $COMPOSE_PROFILES up"
echo " Headless mode:"
echo " DEVICE_TAG=$DEVICE_TAG podman-compose -f podman-compose.yml --profile $COMPOSE_PROFILES run --rm -v \"/mnt/c/Users/myname/whatever/custom_voice:/app/custom_voice\" $SERVICE --headless --ebook \"/app/ebooks/tests/test_eng.txt\" --tts_engine yourtts --language eng --voice \"/app/Desktop/myvoice.wav\" [etc.]"
elif [[ "$DOCKER_MODE" == "compose" ]]; then
echo "--> Using docker compose"
BUILD_NAME="$DOCKER_IMG_NAME" docker compose \
-f docker-compose.yml \
build \
--no-cache \
--build-arg PYTHON_VERSION="$py_vers" \
--build-arg APP_VERSION="$APP_VERSION" \
--build-arg DEVICE_TAG="$DEVICE_TAG" \
--build-arg DOCKER_DEVICE_STR="$ARG" \
--build-arg DOCKER_PROGRAMS_STR="${DOCKER_PROGRAMS[*]}" \
--build-arg CALIBRE_INSTALLER_URL="$CALIBRE_INSTALLER_URL" \
--build-arg ISO3_LANG="$ISO3_LANG" \
|| return 1
echo "Docker image ready! to run your docker: "