-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-arch-wiki
executable file
·96 lines (82 loc) · 3.27 KB
/
local-arch-wiki
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
#!/usr/bin/env bash
checkConfigVariable() {
local variable="$1"
if [ -z "${!variable}" ] || [[ "${!variable}" =~ ^[[:space:]]*$ ]]; then
echo "$variable is not defined or is configured to be empty in the config file."
notify-send "Error!" "Undefined or empty $variable in the config file."
exit 1
fi
}
checkDependencies() {
if [[ ! -d "$wikidir" ]]; then
echo "Dependency error, please install arch-wiki-docs or configure wiki dir accordingly."
notify-send "Dependency error!" "Please install arch-wiki-docs or configure wiki directory accordingly in the config file."
exit 1
else
wikidocs="$(find ${wikidir} -iname "*.html")"
fi
menu_command=$(echo "$menu" | awk '{print $1}')
if ! type -P "$menu_command" &>/dev/null; then
echo "Error: ${menu_command} not found. Please install it or configure accordingly in config."
notify-send "Error: ${menu_command} not found." "Please install it or configure accordingly in the config file."
exit 1
fi
if ! type -P "$browser" &>/dev/null; then
echo "Error: ${browser} not found. Please install it or configure accordingly in config."
notify-send "Error: ${browser} not found." "Please install it or configure accordingly in the config file."
exit 1
fi
}
main(){
choice=$(printf '%s\n' "${wikidocs[@]}" | \
cut -d '/' -f8- | \
sed -e 's/_/ /g' -e 's/.html//g' | \
sort | ${menu} 'Search Wiki: ' "$@") || exit 1
if [ "$choice" ]; then
article=$(printf '%s\n' "${wikidir}${choice}.html" | sed 's/ /_/g')
"${browser}" "${article}"
else
echo "Program terminated." && exit 0
fi
}
# Determine the config directory
if [ -n "$XDG_CONFIG_HOME" ]; then
config_file_dir="$XDG_CONFIG_HOME"/local-arch-wiki
else
config_file_dir="$HOME"/.config/local-arch-wiki
fi
config_file=${config_file_dir}/config
wikidir="/usr/share/doc/arch-wiki/html/en/" #Overridable in the config file.
menu="tofi --prompt-text" #Overridable in the config file.
browser="firefox" #Overridable in the config file.
variables_to_check=("browser" "wikidir" "menu")
if [[ -f "$config_file" ]]; then
# shellcheck source=/dev/null
source "${config_file}"
echo "Config: Sourced user config. (${config_file})"
for variable in "${variables_to_check[@]}"; do
checkConfigVariable "$variable"
done
else
mkdir -p "$config_file_dir"
touch "$config_file"
cat <<EOF > "$config_file"
## Dependencies: arch-wiki-docs, a menu such as dmenu, rofi, tofi,... and a web browser.
## This is the directory where arch wiki's offline html files from package arch-wiki-docs exists.
## By default, english wiki page is selected. However other languages are available.
## Check directory /usr/share/doc/arch-wiki/html/ for other languages and configure accordingly.
wikidir="/usr/share/doc/arch-wiki/html/en/"
## Your favourite web browser to open wiki pages.
browser="firefox"
## Your menu of choice
# menu="dmenu -i -l 10 -p"
# menu="dmenu -l 10 -p" #For dmenu with case-insensitive patch
# menu="rofi -dmenu -p"
menu="tofi --prompt-text"
EOF
# shellcheck source=/dev/null
source "${config_file}"
echo "Config: Created and Sourced user config. (${config_file})"
fi
checkDependencies
main "$@"