#! /bin/sh

# Jonathan Frech, 2021-04-08, 2021-05-14


screenWidth=1920
screenHeight=1080
scale=2


! command -v curl     >/dev/null && printf 'please install: curl\n' && exit 1
! command -v jot      >/dev/null && \
! command -v shuf     >/dev/null && printf 'please install: jot or shuf\n' && exit 1
! command -v identify >/dev/null && printf 'please install: identify (ImageMagick)\n' && exit 1
! command -v convert  >/dev/null && printf 'please install: convert (ImageMagick)\n' && exit 1
! command -v feh      >/dev/null && printf 'please install: feh\n' && exit 1


src='wholly-brainfuck_last-stdout_l6.png'
img="/tmp/blog.jfrech.com_244_$src"
[ ! -f "$img" ] && rm -f "$img" && curl -fsSL "https://blog.jfrech.com/244/$src" -o "$img"

out="/tmp/$(uuidgen).png"

targetW=$(($screenWidth / $scale))
targetH=$(($screenHeight / $scale))

w="$(identify -format "%w" "$img")"
h="$(identify -format "%h" "$img")"

command -v jot >/dev/null \
    && offX="$(jot -r 1 0 "$((w-targetW))")" \
    || offX="$(shuf -i0-"$((w-targetW))" -n1)"
command -v jot >/dev/null \
    && offY="$(jot -r 1 0 "$((h-targetH))")" \
    || offY="$(shuf -i0-"$((h-targetH))" -n1)"

convert -crop "$targetW"x"$targetH"+"$offX"+"$offY" -scale "$scale"00% "$img" "$out"

printf 'setting wallpaper as: %s\n' "$out"
feh --bg-fill "$out"
