#!/bin/dash
#150503 step: see https://github.com/step-/nathans-wallpaper/

# Determine the path to this application.
if ! [ -e "$APPDIR" ]; then # don't do this if APPDIR is preset.
  APPDIR=${0%/*}; APPDIR=`cd "$APPDIR";pwd`
fi
export APPDIR # to wallslide
wallslide=$APPDIR/../../../bin/wallslide

slideshow_is_running()
{
  {
  if type pidof; then pidof -x wallslide
  elif typeof pgrep; then pgrep wallslide
  else ps | grep -q wallslide
  fi
  } >/dev/null 2>&1
}

killslide()
{
  slideshow_is_running && killall wallslide
}

case $1 in
  -stop)
    killslide
  ;;
  -start)
    if slideshow_is_running; then
      killslide
    else
      # $2::=[mode':']image-dir-path; mode::='Centre'|'Scale'|'Tile'|'Stretch'|'Distort'
      [ -d "${2#*:}" ] && exec "$wallslide" "$2"
      exec "$wallslide" "${2%/*}" # legacy
    fi
  ;;
  -start-list|-start-dir)
    if slideshow_is_running; then
      killslide
    else
      exec "$wallslide" "$2"
    fi
esac
