#! /bin/bash

arg=$1

if [[ "$arg" == "" ]] ; then
  echo "Usage: set-wallpaper [--restore]|[/path/to/image/or/video]"
  exit
fi

__check_existence() {
  local bin="$1"

  which "$bin" > /dev/null 2>> /dev/null && echo "true" || echo "false"
}

__set_wallpaper() {
  killall mpvpaper 2>> /dev/null
  killall swaybg 2>> /dev/null

  mimeType=$(file --mime-type $1 | awk '{ print $2 }')
  if [[ "$mimeType" == video* || "$mimeType" == audio* || "$mimeType" == "image/gif" ]] ; then
    echo "Setting video wallpaper using mpvpaper"

    if [[ "$(__check_existence mpvpaper)" == "false" ]] ; then
      echo "You need mpvpaper to use video wallpapers"
      exit 1
    fi

    /sbin/mpvpaper -o "no-audio" VGA-1 $1

    echo "Video wallpaper has been set"
  elif [[ "$mimeType" == image* ]] ; then
    if [[ "$(__check_existence swaybg)" == "true" && "$WAYLAND_DISPLAY" != "" ]] ; then

      echo "Setting image wallpaper using swaybg"

      /sbin/swaybg -i $1 -m fill &
    elif [[ "$(__check_existence feh)" == "true" ]] ; then
      echo "Setting image wallpaper using feh"

      feh --no-fehbg --bg-fill $1
    else
      echo "I don't know what to use. No swaybg or feh"
      exit 1
    fi


    echo "Image wallpaper has been set"
  fi
}

if [[ "$arg" == "--restore" ]] ; then
  path=$(cat $HOME/.wallpaper)

  __set_wallpaper $path
else
  if [[ "$arg" == ./* ]] ; then
    arg="$PWD/$arg"
  fi

  echo "$arg" > $HOME/.wallpaper

  echo "Saved $arg as a wallpaper but you need to run 'set-wallpaper --restore'"
fi

