move dependency checks to own function

This commit is contained in:
Björn Busse 2016-07-29 21:11:59 +02:00 committed by Björn Busse
parent 61be1574ab
commit 5ac7e3fd98
1 changed files with 18 additions and 28 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env rbash #!/usr/bin/env rbash
#
# xkcd_lock v1 # xkcdlock v1
# #
# A wrapper around screen lockers to display xkcd images on the lock screen # A wrapper around screen lockers to display xkcd images on the lock screen
# #
@ -24,6 +24,7 @@
# TODO: # TODO:
# - make sure we do not exceed screen boundaries in x and y # - make sure we do not exceed screen boundaries in x and y
# - add tooltip overlay and xkcd number # - add tooltip overlay and xkcd number
# - add support for other screen lockers
# - parallelize downloads # - parallelize downloads
[[ "$TRACE" ]] && set -x [[ "$TRACE" ]] && set -x
@ -44,6 +45,9 @@ IMG_DEFAULT="not_really_into_pokemon.png"
# background colour # background colour
BG_COLOUR="white" BG_COLOUR="white"
declare -a DEPS=("xrandr" "awk" "curl" "convert")
declare -a DEPS_LOCKER=("i3lock" "swaylock")
DOWNLOAD_DISCLAIMER="\nThe downloaded images will end up in your current working directory.\n\ DOWNLOAD_DISCLAIMER="\nThe downloaded images will end up in your current working directory.\n\
Since we are using restricted bash, we can not change path.\n\ Since we are using restricted bash, we can not change path.\n\
Use '-y' instead of '-d' to really start the download to the current working directory.\n" Use '-y' instead of '-d' to really start the download to the current working directory.\n"
@ -77,12 +81,17 @@ show_help() {
-v be verbose\n\n" -v be verbose\n\n"
} }
xkcd_get_latest_image() { check_dependencies() {
if [[ -z $(which curl) ]]; then for i in "${DEPS[@]}"
error "Could not find curl to get latest image" do
exit 1 if [[ -z $(which "${i}") ]]; then
fi error "Could not find ${i}"
exit 1
fi
done
}
xkcd_get_latest_image() {
log "Looking for latest image" log "Looking for latest image"
local img_url=$(curl -s https://xkcd.com/index.html | \ local img_url=$(curl -s https://xkcd.com/index.html | \
awk '/Image URL \(for hotlinking\/embedding\): / {print $5}' | \ awk '/Image URL \(for hotlinking\/embedding\): / {print $5}' | \
@ -112,12 +121,6 @@ xkcd_get_img_name() {
xkcd_get_all_images() { xkcd_get_all_images() {
VERBOSE="1" VERBOSE="1"
if [[ -z $(which curl) ]]; then
error "Could not find curl to download images"
exit 1
fi
log "Looking for latest image" log "Looking for latest image"
local nimg_latest=$(curl -s https://xkcd.com/index.html | \ local nimg_latest=$(curl -s https://xkcd.com/index.html | \
awk '/Permanent link to this comic: / {print $6}' | \ awk '/Permanent link to this comic: / {print $6}' | \
@ -260,26 +263,13 @@ main() {
esac esac
done done
check_dependencies
if ! [[ -d "$IMG_PATH" ]]; then if ! [[ -d "$IMG_PATH" ]]; then
error "Image directory does not exist: $IMG_PATH" error "Image directory does not exist: $IMG_PATH"
exit 1 exit 1
fi fi
if [[ -z $(which xrandr) ]]; then
error "Could not find xrandr to determine screen size"
exit 1
fi
if [[ -z "$locker" ]]; then
error "Could not find screen locker: $lock_bin"
exit 1
fi
if [[ -z $(which convert) ]]; then
error "Could not find $convert_bin \nYou need imagick"
exit 1
fi
case "$IMG_CHOICE" in case "$IMG_CHOICE" in
"latest") local img_fn=$(xkcd_get_latest_image);; "latest") local img_fn=$(xkcd_get_latest_image);;
"random") local img_fn=$(get_random_image);; "random") local img_fn=$(get_random_image);;