#!/usr/bin/env bash

NOTIFY="FALSE"
IMAGE="RANDOM"

function get_random_slug(){
	curl -s "https://earthview.withgoogle.com/" | hq 'a.intro__explore' attr href
}

function notification(){
	notify-send "$( jq -r '.name' /tmp/backgroundinfo.json)"
}

function set_random_image(){
	set_specific_image "$slug"
}

function download_image(){
	if [ "$IMAGE" == "RANDOM" ]; then
		local slug=$(get_random_slug)
	else
		local slug="$IMAGE"
	fi
	curl -s "https://earthview.withgoogle.com/_api${slug}.json" > /tmp/backgroundinfo.json
	image=$( jq -r '.photoUrl' /tmp/backgroundinfo.json )
	curl -s -o /tmp/background.jpg "$image"
}

function set_image(){
	download_image
	if command -v nitrogen > /dev/null; then
		nitrogen --set-zoom-fill /tmp/background.jpg
	elif command -v nitrogen > /dev/null; then
		feh --bg-fill /tmp/background.jpg
	else
		echo "No wallpaper tools found"
	fi
	if [ "$NOTIFY" == "TRUE" ]; then
		notification
	fi
}

while [ -n "$1" ]; do
	case "$1" in
		"--notify")
			NOTIFY="TRUE"
			shift
			;;
		"--only-notify")
			notification
			shift
			exit 0
			;;
	esac
done

set_image