#!/usr/bin/env bash

# Statics
TEMP_PATH="/tmp/bg"
ALL_IMAGE_URL=(
    "https://nieuwsredactie.fhj.nl/wp-content/uploads/2016/05/Big_Mac_hamburger.jpg"
    "https://i.kinja-img.com/gawker-media/image/upload/s--AaVjx1H1--/c_fill,fl_progressive,g_center,h_900,q_80,w_1600/wsejjr7ezhbmzfh7yjca.jpg"
    "https://www.plezierindekeuken.nl/wp-content/uploads/2015/07/Broodje-hamburger-van-rundergehakt.jpg"
    "https://www.sikkom.nl/wp-content/uploads/2017/01/Hamburger-1.jpg"
)

# Get a random image
# IMAGE_URL=${ALL_IMAGE_URL[$RANDOM % ${#ALL_IMAGE_URL[@]} ]}

# Download all images to /tmp/
ii=1
for im in "${ALL_IMAGE_URL[@]}"; do
    curl -s "$im" > "${TEMP_PATH}_${ii}.jpg"
    ((ii++))
done
((ii--))

# Set background
osascript <<EOF
    tell application "Finder"
        tell application "System Events"
            set theDesktops to a reference to every desktop

            repeat with aDesktop in theDesktops
                set img_indx to (random number from 1 to ${ii}) as text
                set img_path to "/tmp/bg_" & img_indx & ".jpg"
                set picture of aDesktop to img_path
            end repeat
        end tell
    end tell

EOF

# Remove all images
for bla in $(eval echo "{$ii..1}"); do
    rm "${TEMP_PATH}_${bla}.jpg"
done

# Done
printf "\nTada!\n"
