Skip to content

Commit da21e4d

Browse files
author
zenobit
committed
run portable or not
1 parent 57261c2 commit da21e4d

File tree

1 file changed

+107
-112
lines changed

1 file changed

+107
-112
lines changed

dh

Lines changed: 107 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,38 @@ some_bugs() {
55
echo "Done
66
PS: You saw some bugs? Let me know on project page:
77
https://github.com/oSoWoSo/DistroHopper
8-
Happy distro hopping... zenobit"
8+
Flawless distro hopping... zenobit"
9+
}
10+
11+
show_help() {
12+
echo "
13+
Possible arguments:
14+
d Set where VMs are stored
15+
i Install DistroHopper
16+
s Update supported VMs
17+
t Run TUI instead of GUI
18+
r Update ready to run VMs
19+
a Add new distro to quickget
20+
h Show this help
21+
o Copy all ISOs to target dir (for Ventoy)
22+
p Push changed quickget to quickemu project
23+
---------------------------------------------------------"
924
}
1025

1126
# installation
1227
check_gui_dependencies() {
13-
if ! command -v yad >/dev/null 2>&1; then
14-
echo "You are missing yad...
15-
It's needed for GUI!" >&2
16-
fi
28+
[ -f "$PREFIX/yad" ] && echo "yad is here" || echo "You are missing yad...
29+
It's needed for GUI!"
1730
}
1831

1932
check_tui_dependencies() {
20-
if ! command -v fzf >/dev/null 2>&1; then
21-
echo "You are missing fzf...
22-
It's needed for TUI!" >&2
23-
fi
24-
}
25-
26-
check_quickemu_dependencies() {
27-
if ! command -v yad >/dev/null 2>&1; then
28-
echo "You are missing some quickemu dependencies......
29-
They are needed for running VMs!" >&2
30-
fi
33+
[ -f "$PREFIX/fzf" ] && echo "fzf is here" || echo "You are missing fzf...
34+
It's needed for TUI!"
3135
}
3236

3337
set_variables() {
34-
progname="${progname:="${0##*/}"}"
38+
#progname="${progname:="${0##*/}"}"
39+
progname="DistroHopper"
3540
version="0.5"
3641
#GTK_THEME="alt-dialog"
3742
DH_CONFIG_DIR="$HOME/.config/distrohopper"
@@ -40,34 +45,13 @@ set_variables() {
4045
PREFIX="/usr/bin/"
4146
TERMINAL=sakura
4247
replace='"!"'
43-
source "$DH_CONFIG" >/dev/null 2>&1 \
44-
&& cd "$VMS_DIR" \
45-
&& export "DH_CONFIG_DIR" \
46-
"DH_CONFIG" \
47-
"DH_ICON_DIR" \
48-
"PREFIX" \
49-
"TERMINAL" \
50-
"replace" \
51-
|| echo "Not installed yet!" \
52-
&& VMS_DIR="$(pwd)" \
53-
&& DH_CONFIG_DIR="$(pwd)" \
54-
&& DH_ICON_DIR="$(pwd)"/icons \
55-
&& export "VMS_DIR" \
56-
"DH_CONFIG_DIR" \
57-
"DH_ICON_DIR" \
58-
"TERMINAL" \
59-
"replace" \
60-
&& echo "Using current: $VMS_DIR directory!"
61-
echo "DistroHopper v$version"
62-
echo "quickemu v$(quickemu --version)
63-
"
48+
export "DH_CONFIG_DIR" "DH_CONFIG" "replace" "DH_ICON_DIR" "PREFIX" "TERMINAL"
49+
portable
6450
# Set traps to catch the signals and exit gracefully
6551
trap "exit" INT
6652
trap "exit" EXIT
67-
}
68-
69-
run_once() {
70-
source config/distrohopper.conf
53+
echo "DistroHopper v. $version"
54+
echo "quickemu v. $("$prefix"quickemu --version)"
7155
}
7256

7357
create_structure() {
@@ -79,10 +63,6 @@ create_structure() {
7963
mkdir -p "$DH_CONFIG_DIR/supported"
8064
echo "creating icons dir as root..."
8165
sudo mkdir -p "$DH_ICON_DIR"
82-
echo "copying to config dir..."
83-
cp -r * "$DH_CONFIG_DIR/"
84-
echo "copying icons to root..."
85-
sudo cp "icons/"* "$DH_ICON_DIR/"
8666
}
8767

8868
set_dir() {
@@ -98,37 +78,93 @@ install_prereq() {
9878
sudo xbps-install -S qemu bash coreutils grep jq procps-ng python3 util-linux sed spice-gtk swtpm usbutils wget xdg-user-dirs xrandr unzip zsync socat
9979
}
10080

101-
dh_to_bin() {
81+
portable() {
82+
[ -f "$DH_CONFIG" ] && mode_installed || mode_portable
83+
}
84+
85+
mode_portable() {
86+
VMS_DIR="$(pwd)"
87+
DH_CONFIG_DIR="$(pwd)"
88+
DH_ICON_DIR="$(pwd)/icons"
89+
prefix=./
90+
export "VMS_DIR" "DH_CONFIG_DIR" "DH_ICON_DIR" "TERMINAL" "replace" "prefix"
91+
}
92+
93+
mode_installed() {
94+
source "$DH_CONFIG"
95+
prefix=
96+
export "prefix"
97+
}
98+
99+
install_dh() {
100+
source distrohopper.conf
102101
sudo cp dh quickget quickemu macrecovery windowskey "$PREFIX"
103102
# quickget also to config directory for adding new distros...
104103
cp quickget "$DH_CONFIG_DIR"
104+
echo "copying icons to root..."
105+
sudo cp "icons/"* "$DH_ICON_DIR/"
106+
echo "copying to config dir..."
107+
cp -r * "$DH_CONFIG_DIR/"
105108
}
106109

107110
install_process() {
108111
check_tui_dependencies
109112
check_gui_dependencies
110113
check_quickemu_dependencies
111-
echo "Sourcing config..." \
112-
&& run_once \
113-
&& echo "Creating directory structure..." \
114+
echo "Creating directory structure..." \
114115
&& create_structure \
115116
&& echo "Setting up directory..." \
116117
&& set_dir \
117118
&& echo "Installing needed..." \
118119
&& echo "For now voidlinux only" \
119120
&& install_prereq \
120121
&& echo "Installing DistroHopper to bin..." \
121-
&& dh_to_bin
122+
&& install_dh
122123
}
123124

124125
# basic
126+
renew_ready() {
127+
cd "$VMS_DIR" || exit 1
128+
rm "$DH_CONFIG_DIR"/ready/*.desktop
129+
if [ ! -e *.conf ]; then
130+
echo "No .conf files found"
131+
return
132+
fi
133+
for vm_conf in *.conf; do
134+
if [ "$vm_conf" == "distrohopper.conf" ]; then
135+
continue # skip processing distrohopper.conf
136+
fi
137+
vm_desktop=$(basename "$VMS_DIR/$vm_conf" .conf)
138+
# Use fuzzy matching to find the best matching icon file (ready to run VMs)
139+
icon_name=$(basename "$VMS_DIR/$vm_conf" .conf | cut -d'-' -f -2)
140+
icon_file=$(find "$DH_ICON_DIR" -type f -iname "${icon_name// /}.*")
141+
# If no icon was found, try shorter name (ready to run VMs)
142+
if [ -z "$icon_file" ]; then
143+
icon_name=$(basename "$VMS_DIR/$vm_conf" .conf | cut -d'-' -f1)
144+
icon_file=$(find "$DH_ICON_DIR" -type f -iname "${icon_name// /}.*")
145+
elif [ -z "$icon_file" ]; then
146+
icon_file="$DH_ICON_DIR/tux.svg"
147+
fi
148+
# content of desktop files (ready to run VMs)
149+
desktop_file_content="[Desktop Entry]
150+
Type=Application
151+
Name=$vm_desktop
152+
Exec=sh -c 'cd \"$VMS_DIR\" && "$prefix"quickemu -vm $vm_conf'
153+
Icon=$icon_file
154+
Categories=System;Virtualization;"
155+
# create desktop files (ready to run VMs)
156+
echo "$desktop_file_content" > "$DH_CONFIG_DIR"/ready/"$vm_desktop".desktop
157+
done
158+
}
159+
125160
renew_supported() {
161+
rm "$DH_CONFIG_DIR"/supported/*.desktop
126162
# get supported VMs
127-
quickget | sed 1d | cut -d':' -f2 | grep -o '[^ ]*' > "$DH_CONFIG_DIR/supported.md"
163+
"$prefix"quickget | sed 1d | cut -d':' -f2 | grep -o '[^ ]*' > "$DH_CONFIG_DIR/supported.md"
128164
while read -r get_name; do
129165
vm_desktop=$(echo "$get_name" | tr ' ' '_')
130-
releases=$(quickget "$vm_desktop" | grep 'Releases' | cut -d':' -f2 | sed 's/^ //')
131-
editions=$(quickget "$vm_desktop" | grep 'Editions' | cut -d':' -f2 | sed 's/^ //')
166+
releases=$("$prefix"quickget "$vm_desktop" | grep 'Releases' | cut -d':' -f2 | sed 's/^ //')
167+
editions=$("$prefix"quickget "$vm_desktop" | grep 'Editions' | cut -d':' -f2 | sed 's/^ //')
132168
icon_name="$DH_ICON_DIR/$get_name"
133169
if [ -f "$icon_name.svg" ]; then
134170
icon_file="$icon_name.svg"
@@ -145,7 +181,7 @@ Type=Application
145181
Name=$get_name
146182
releases=$releases
147183
replace=$replace
148-
Exec=sh -c 'cd \"$VMS_DIR\" && yad --form --field=\"Release:CB\" \"${releases// /$replace}\" | cut -d\"|\" -f1 | xargs -I{} sh -c \"quickget $get_name {}\"'
184+
Exec=sh -c 'cd \"$VMS_DIR\" && yad --form --field=\"Release:CB\" \"${releases// /$replace}\" | cut -d\"|\" -f1 | xargs -I{} sh -c "$prefix"quickget $get_name {}'
149185
Icon=$icon_file
150186
Categories=System;Virtualization;"
151187
echo "$desktop_file_content" > "$DH_CONFIG_DIR"/supported/"$vm_desktop".desktop
@@ -157,59 +193,25 @@ Name=$get_name
157193
releases=$releases
158194
editions=$editions
159195
replace=$replace
160-
Exec=sh -c 'cd \"$VMS_DIR\" && yad --form --separator=\" \" --field=\"Release:CB\" \"${releases// /$replace}\" --field=\"Edition:CB\" \"${editions// /$replace}\" | xargs -I{} sh -c \"quickget $get_name {}\"'
196+
Exec=sh -c 'cd \"$VMS_DIR\" && yad --form --separator=\" \" --field=\"Release:CB\" \"${releases// /$replace}\" --field=\"Edition:CB\" \"${editions// /$replace}\" | xargs -I{} sh -c \"$prefixquickget $get_name {}\"'
161197
Icon=$icon_file
162198
Categories=System;Virtualization;"
163199
echo "$desktop_file_content" > "$DH_CONFIG_DIR"/supported/"$vm_desktop".desktop
164200
fi
165201
done < "$DH_CONFIG_DIR"/supported.md
166202
}
167203

168-
renew_ready() {
169-
cd "$VMS_DIR" || exit 1
170-
# check for VMs .conf files (ready to run VMs)
171-
if [ ! -e *.conf ]; then
172-
echo "No .conf files found"
173-
return
174-
fi
175-
for vm_conf in *.conf; do
176-
vm_desktop=$(basename "$VMS_DIR/$vm_conf" .conf)
177-
# Use fuzzy matching to find the best matching icon file (ready to run VMs)
178-
icon_name=$(basename "$VMS_DIR/$vm_conf" .conf | cut -d'-' -f -2)
179-
icon_file=$(find "$DH_ICON_DIR" -type f -iname "${icon_name// /}.*")
180-
# If no icon was found, try shorter name (ready to run VMs)
181-
if [ -z "$icon_file" ]; then
182-
icon_name=$(basename "$VMS_DIR/$vm_conf" .conf | cut -d'-' -f1)
183-
icon_file=$(find "$DH_ICON_DIR" -type f -iname "${icon_name// /}.*")
184-
elif [ -z "$icon_file" ]; then
185-
icon_file="$DH_ICON_DIR/tux.svg"
186-
fi
187-
# content of desktop files (ready to run VMs)
188-
desktop_file_content="[Desktop Entry]
189-
Type=Application
190-
Name=$vm_desktop
191-
Exec=sh -c 'cd \"$VMS_DIR\" && quickemu -vm \"$vm_conf\"'
192-
Icon=$icon_file
193-
Categories=System;Virtualization;"
194-
# create desktop files (ready to run VMs)
195-
echo "$desktop_file_content" > "$DH_CONFIG_DIR"/ready/"$vm_desktop".desktop
196-
done
197-
}
198-
199204
run_gui() {
205+
check_gui_dependencies
200206
key=$((RANDOM % 9000 + 1000))
201207
yad --plug="$key" --tabnum=1 --monitor --icons --listen --read-dir="$DH_CONFIG_DIR"/ready --sort-by-name --no-buttons --borders=0 --icon-size=46 --item-width=76 &
202208
yad --plug="$key" --tabnum=2 --monitor --icons --listen --read-dir="$DH_CONFIG_DIR"/supported --sort-by-name --no-buttons --borders=0 --icon-size=46 --item-width=76 &
203-
yad --dynamic --notebook --key="$key" --monitor --listen --window-icon="$DH_ICON_DIR"/hop.svg --width=900 --height=900 --title="DistroHopper" --tab="run VM" --tab="download VM" --tab="settings" --button "test"
209+
yad --dynamic --notebook --key="$key" --monitor --listen --window-icon="$DH_ICON_DIR"/hop.svg --width=900 --height=900 --title="DistroHopper" --tab="run VM" --tab="download VM"
204210
}
205211

206212
run_tui() {
207-
printf '%s: v.%s\nquickemu: v.%s\n\n VMs dir: %s\n\n' "$progname" "$version" "$(quickemu --version)" "$(pwd)"
213+
check_tui_dependencies
208214
vms=(*.conf)
209-
# Dependency check: check if fzf is installed and can be executed
210-
if ! command -v fzf >/dev/null 2>&1; then
211-
echo "You are missing fzf..." && exit 255
212-
fi
213215
printf ' Prepared VMs:\n-------------\n\n'
214216
# Check if there are any VMs
215217
if [ ${#vms[@]} -eq 0 ]; then
@@ -230,7 +232,7 @@ run_tui() {
230232
esac
231233
# If the user chose to create a new VM
232234
if [ "$todo" = "create" ]; then
233-
os=$(quickget | sed 1d | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose OS to download
235+
os=$("$prefix"quickget | sed 1d | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose OS to download
234236
or CTRL-c or ESC to quit')
235237
# If the OS is Windows
236238
if [ "$os" = windows ]; then
@@ -242,24 +244,24 @@ Choose other language" | fzf --cycle)
242244
10
243245
11" | fzf --cycle)
244246
# get window language list
245-
wlend=$(($(cat quickget | sed '/Arabic/,$!d' | grep -n '}' | cut -d':' -f1 | head -n 1) - 1))
247+
wlend=$(($(cat "$prefix"quickget | sed '/Arabic/,$!d' | grep -n '}' | cut -d':' -f1 | head -n 1) - 1))
246248
# get windows language
247-
wlang=$(cat quickget | sed '/Arabic/,$!d' | head -n $wlend | cut -d'=' -f2 | tail -c +2 | head -c -2 | sed 's/^[ \t]*//' | fzf --cycle --header='Choose Language
249+
wlang=$(cat "$prefix"quickget | sed '/Arabic/,$!d' | head -n $wlend | cut -d'=' -f2 | tail -c +2 | head -c -2 | sed 's/^[ \t]*//' | fzf --cycle --header='Choose Language
248250
or CTRL-c or ESC to quit')
249251
# downloading windows
250252
printf '\n Trying to download Windows %s %s...\n\n' "$wrelease" "$wlang"
251-
quickget "windows" "$wrelease" "$wlang"
253+
"$prefix"quickget "windows" "$wrelease" "$wlang"
252254
fi
253255
fi
254256
# Get the release and edition to download, if necessary
255-
choices=$(quickget "$os" | sed 1d)
257+
choices=$("$prefix"quickget "$os" | sed 1d)
256258
if [ "$(echo "$choices" | wc -l)" = 1 ]; then
257259
# get release
258260
release=$(echo "$choices" | grep 'Releases' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Release
259261
or CTRL-c or ESC to quit')
260262
# downloading
261263
printf '\n Trying to download %s %s...\n\n' "$os" "$release"
262-
quickget "$os" "$release"
264+
"$prefix"quickget "$os" "$release"
263265
else
264266
# get release
265267
release=$(echo "$choices" | grep 'Releases' | cut -d':' -f2 | grep -o '[^ ]*' | fzf --cycle --header='Choose Release
@@ -269,14 +271,14 @@ Choose other language" | fzf --cycle)
269271
or CTRL-c or ESC to quit')
270272
# downloading
271273
printf '\n Trying to download %s %s %s...\n\n' "$os" "$release" "$edition"
272-
quickget "$os" "$release" "$edition"
274+
"$prefix"quickget "$os" "$release" "$edition"
273275
fi
274276
# choose VM to run
275277
choosed=$(echo "$(ls ./***.conf 2>/dev/null | sed 's/\.conf$//')" | fzf --cycle --header='Choose VM to run
276278
or CTRL-c or ESC to quit')
277279
# Run choosed VM
278280
printf '\n Starting %s...\n\n' "$choosed"
279-
quickemu -vm "$choosed.conf"
281+
"$prefix"quickemu -vm "$choosed.conf"
280282
fi
281283
}
282284

@@ -319,27 +321,20 @@ add_distro() {
319321
}
320322

321323
# run
324+
322325
set_variables
326+
323327
case "$1" in
324328
d) set_dir;;
325329
i) echo "Starting installation..." && install_process;;
326330
s) echo "Updating supported VMs..." && renew_supported;;
327331
t) echo "You are master! Running TUI..." && run_tui;;
328332
r) echo "Updating ready VMs..." && renew_ready;;
329333
a) echo "Adding new distro started..." && add_distro;;
330-
h) echo "Possible arguments are:
331-
d Set where VMs are stored
332-
i Install DistroHopper
333-
s Update supported VMs
334-
t Run TUI instead of GUI
335-
r Update ready to run VMs
336-
a Add new distro to quickget
337-
h Show this help
338-
o Copy all ISOs to target dir (for Ventoy)
339-
p Push changed quickget to quickemu project";;
334+
h) show_help;;
340335
o) echo "Copying ISOs to dir. It will take some time..." && isos_to_dir;;
341336
p) echo "Nothing here yet!";;
342-
*) echo "You can show help if you run 'dh h'
337+
*) echo "Run 'dh h' for help
343338
Starting DistroHopper GUI..." && run_gui;;
344339
esac
345340

0 commit comments

Comments
 (0)