Skip to content

Commit b21388c

Browse files
committed
termux-wallpaper: rewrite
- allow spaces in the option -f - fix shellcheck errors fixes termux/termux-api#298
1 parent 97d7297 commit b21388c

1 file changed

Lines changed: 26 additions & 45 deletions

File tree

scripts/termux-wallpaper

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,50 @@
33
set -e
44

55
SCRIPTNAME=termux-wallpaper
6-
76
show_usage () {
87
echo "Change wallpaper on your device"
98
echo
10-
echo "Usage: $SCRIPTNAME cmd [args]"
9+
echo "Usage: $SCRIPTNAME [options]"
1110
echo "-h show this help"
1211
echo "-f <file> set wallpaper from file"
1312
echo "-u <url> set wallpaper from url resource"
1413
echo "-l set wallpaper for lockscreen (Nougat and later)"
1514
exit 1
1615
}
1716

18-
call_api() {
19-
/data/data/com.termux/files/usr/libexec/termux-api Wallpaper "$@"
20-
}
21-
22-
usage_error () {
23-
echo "ERROR: $@"
24-
show_usage
25-
}
26-
27-
LOCKSCREEN_FLAG=1
28-
RESOURCE_FLAG=2
29-
30-
FLAGS=0
31-
32-
set_single () {
33-
if [ $((FLAGS & $1)) -ne 0 ]; then
34-
usage_error "Option already set"
35-
fi
36-
FLAGS=$((FLAGS | $1))
37-
PARAMS="$PARAMS $2"
38-
}
39-
40-
set_resource () {
41-
if [ $((FLAGS & $RESOURCE_FLAG)) -ne 0 ]; then
42-
usage_error "More than one image resource specified!"
43-
fi
44-
set_single $RESOURCE_FLAG "$1"
45-
}
46-
47-
set_file () {
48-
if [ ! -f $1 ]; then
49-
usage_error "'$1' is not a file!"
50-
fi
51-
set_resource "--es file "$(realpath $1)""
52-
}
17+
OPT_LS=""
18+
OPT_FILE=""
19+
OPT_URL=""
5320

5421
while getopts :h,:l,f:,u: option
5522
do
5623
case "$option" in
5724
h) show_usage ;;
58-
l) set_single $LOCKSCREEN_FLAG "--ez lockscreen true" ;;
59-
f) set_file $OPTARG ;;
60-
u) set_resource "--es url $OPTARG" ;;
25+
l) OPT_LS="true" ;;
26+
f) path="$(realpath "$OPTARG")"
27+
if [[ ! -f "$path" ]]; then
28+
echo "$SCRIPTNAME: $path is not a file!"
29+
exit 1
30+
fi
31+
OPT_FILE="$path" ;;
32+
u) OPT_URL="$OPTARG" ;;
6133
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1 ;;
6234
esac
6335
done
6436

65-
shift $((OPTIND - 1))
66-
67-
if [ $((FLAGS & RESOURCE_FLAG)) -eq 0 ]; then
68-
usage_error "No file or url provided!"
37+
if [[ -z "$OPT_FILE""$OPT_URL" ]]; then
38+
echo "$SCRIPTNAME: you must specify either -f or -u"
39+
exit 1
40+
elif [[ -n "$OPT_FILE" ]] && [[ -n "$OPT_URL" ]]; then
41+
echo "$SCRIPTNAME: you must specify either -f or -u, but not both"
42+
exit 1
6943
fi
7044

71-
call_api $PARAMS
45+
shift $((OPTIND - 1))
46+
if [ $# != 0 ]; then echo "$SCRIPTNAME: too many arguments"; exit 1; fi
47+
48+
set --
49+
[ -n "$OPT_LS" ] && set -- "$@" --ez lockscreen "$OPT_LS"
50+
[ -n "$OPT_FILE" ] && set -- "$@" --es file "$OPT_FILE"
51+
[ -n "$OPT_URL" ] && set -- "$@" --es url "$OPT_URL"
52+
/data/data/com.termux/files/usr/libexec/termux-api Wallpaper "$@"

0 commit comments

Comments
 (0)