Skip to content

Commit 72a0625

Browse files
authored
big refactor
1 parent 6a47b5f commit 72a0625

File tree

1 file changed

+47
-85
lines changed

1 file changed

+47
-85
lines changed

AppDir/AppRun

Lines changed: 47 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,59 @@
1-
#!/usr/bin/env sh
2-
CURRENTDIR="$(dirname "$(readlink -f "$0")")"
3-
UDEVNOTICE='No android udev rules detected, use "--getudev" to install'
4-
UDEVREPO="https://github.com/M0Rf30/android-udev-rules.git"
5-
cat /etc/udev/rules.d/*droid.rules >/dev/null 2>&1 && UDEVNOTICE=""
6-
cat /usr/lib/udev/rules.d/*droid.rules >/dev/null 2>&1 && UDEVNOTICE=""
7-
BIN="${ARGV0#./}"
8-
unset ARGV0
9-
export PATH="$CURRENTDIR/bin:$PATH"
1+
#!/bin/sh
102

11-
_get_udev_rules() {
12-
if cat /etc/udev/rules.d/*droid.rules >/dev/null 2>&1 \
13-
|| cat /usr/lib/udev/rules.d/*droid.rules >/dev/null 2>&1; then
14-
echo "ERROR: udev rules are already installed!"
15-
echo "Errors persisting with installed udev rules may be due to missing"
16-
echo "udev rules for device or lack of permissions from device"
17-
exit 1
18-
fi
19-
if ! command -v git >/dev/null 2>&1; then
20-
echo "ERROR: you need git to use this function"
21-
exit 1
22-
fi
23-
if command -v sudo >/dev/null 2>&1; then
24-
SUDOCMD="sudo"
25-
elif command -v doas >/dev/null 2>&1; then
26-
SUDOCMD="doas"
27-
else
28-
echo "ERROR: You need sudo or doas to use this function"
29-
exit 1
30-
fi
31-
printf '%s' "udev rules installer from $UDEVREPO, run installer? (y/N): "
32-
read -r yn
33-
if echo "$yn" | grep -i '^y' >/dev/null 2>&1; then
34-
tmpudev=".udev_rules_tmp.dir"
35-
git clone "$UDEVREPO" "$tmpudev" && cd "$tmpudev" || exit 1
36-
chmod +x ./install.sh && "$SUDOCMD" ./install.sh
37-
cat /etc/udev/rules.d/*droid.rules >/dev/null 2>&1 || exit 1
38-
cd .. && rm -rf "$tmpudev" || exit 1
39-
echo "udev rules installed successfully!"
40-
else
41-
echo "Aborting..."
42-
exit 1
43-
fi
44-
}
3+
set -e
4+
5+
CURRENTDIR="$(cd "${0%/*}" && echo "$PWD")"
6+
BINDIR="${XDG_BIN_HOME:-$HOME/.local/bin}"
7+
BIN="${ARGV0:-$0}"
8+
BIN="${BIN##*/}"
9+
unset ARGV0
4510

4611
_get_symlinks() {
47-
BINDIR="${XDG_BIN_HOME:-$HOME/.local/bin}"
48-
links="$(find "$CURRENTDIR"/shared/bin -maxdepth 1 -exec file {} \; \
49-
| awk -F":" '/ELF/ {print $1}' | xargs -I {} basename {} 2>/dev/null)"
12+
set -- "$CURRENTDIR"/bin/*
5013
echo ""
5114
echo "This function will make wrapper symlinks in $BINDIR"
52-
echo "that will point to $APPIMAGE with the names:"
53-
echo "$links" | tr ' ' '\n'
15+
echo "that will point to $APPIMAGE, this way when you run that symlink"
16+
echo "it automatically launches that binary without extra arguments"
5417
echo ""
55-
echo "Make sure there are not existing files $BINDIR with those names"
56-
printf '\n%s' "Proceed with the symlink creation? (Y/n): "
57-
read -r yn
58-
if echo "$yn" | grep -i '^n' >/dev/null 2>&1; then
59-
echo "Aborting..."
60-
exit 1
61-
fi
62-
mkdir -p "$BINDIR" || exit 1
63-
for link in $links; do
64-
ln -s "$APPIMAGE" "$BINDIR/$link" 2>/dev/null \
65-
&& echo "\"$link\" symlink successfully created in \"$BINDIR\""
18+
printf '%s' "Proceed with the symlink creation? (Y/n): "; read -r yn
19+
case "$yn" in
20+
n*|N*) >&2 echo "Aborting..."; exit 1;;
21+
esac
22+
mkdir -p "$BINDIR"
23+
for link do
24+
if [ ! -x "$link" ] || [ ! -f "$link" ]; then
25+
continue
26+
fi
27+
link="${link##*/}"
28+
ln -s "$APPIMAGE" "$BINDIR"/"$link" 2>/dev/null \
29+
&& echo "'$link' successfully created in '$BINDIR'"
6630
done
6731
}
6832

69-
# logic
70-
if [ -n "$BIN" ] && [ -e "$CURRENTDIR/bin/$BIN" ]; then
71-
"$CURRENTDIR/bin/$BIN" "$@" || echo "$UDEVNOTICE"
72-
elif [ -n "$1" ] && [ -e "$CURRENTDIR/bin/$1" ]; then
73-
option="$1"
33+
# additional scripts can be placed in the top level bin dir
34+
# those with a name that ends up .hook will be executed in teh current shell
35+
# while those that end with bg.hook will be executed in the background
36+
for hook in "$CURRENTDIR"/bin/*.hook; do
37+
[ -x "$hook" ] || continue
38+
case "$hook" in
39+
*.bg.hook) >&2 echo "exec bg hook: $hook"; "$hook" &;;
40+
*.hook) >&2 echo "exec hook: $hook"; "$hook" ;;
41+
esac
42+
done
43+
44+
if [ "$1" = '--getlinks' ]; then
45+
_getlinks
46+
fi
47+
48+
# Check if ARGV0 matches any bundled binary, fallback to $1, then main bin
49+
if [ -f "$CURRENTDIR"/bin/"$BIN" ]; then
50+
exec "$CURRENTDIR"/bin/"$BIN" "$@"
51+
elif [ -f "$CURRENTDIR"/bin/"$1" ]; then
52+
BIN="$1"
7453
shift
75-
"$CURRENTDIR/bin/$option" "$@" || echo "$UDEVNOTICE"
54+
exec "$CURRENTDIR"/bin/"$BIN" "$@"
7655
else
77-
case "$1" in
78-
'--getudev')
79-
_get_udev_rules
80-
;;
81-
'--getlinks')
82-
_get_symlinks
83-
;;
84-
*)
85-
echo ""
86-
echo "USAGE: \"${APPIMAGE##*/} [ARGUMENT]\""
87-
echo "EXAMPLE: \"${APPIMAGE##*/} adb shell\" to enter adb shell"
88-
echo ""
89-
echo "You can also make a symlink to $APPIMAGE named adb"
90-
echo "and run the symlink to enter adb without typing ${APPIMAGE##*/}"
91-
echo ""
92-
echo 'use "--getlinks" if you want to make the symlinks automatically'
93-
echo ""
94-
exit 1
95-
;;
96-
esac
56+
>&2 echo "Using default binary adb..."
57+
>&2 echo "Run ${APPIMAGE##*/} --getlinks for more info"
58+
exec "$CURRENTDIR"/bin/adb "$@"
9759
fi

0 commit comments

Comments
 (0)