#!/bin/sh
# Tiny script to send url, file, video, img, … to a Kodi instance.
# Use idok script. Please see https://github.com/metal3d/idok

# Works with Nautilus, Caja, …
    # Nautilus: cp send_to_kodi ~/.local/share/nautilus/scripts/ ; chmod +x ~/.local/share/nautilus/scripts/send_to_kodi
    # Nemo:     cp send_to_kodi ~/.gnome2/nemo-scripts/          ; chmod +x ~/.gnome2/nemo-scripts/send_to_kodi
    # Caja:     cp send_to_kodi ~/.config/caja/scripts/          ; chmod +x ~/.config/caja/scripts/send_to_kodi

################################################
#        notification depends of system        #
################################################
notif() {
  # the script is running in a term
  if [ $(env | grep '^TERM') ]; then printf "\n#### $(basename -- "$0") notification ####\n  ⇒  $1\n\n"
  else # in x, notifications
    if   command -v xmessage 2>/dev/xmessagenull; then xmessage "$1" -timeout 5
    elif command -v notify-send 2>/dev/null; then notify-send "$1"
    elif command -v kdialog 2>/dev/null; then { kdialog --title "$1" --passivepopup "This popup will disappear in 5 seconds" 5 & }
    elif command -v zenity 2>/dev/null; then { echo "message:$1" | zenity --notification --listen & }
    else echo "$1" > "$(basename -- $0)_notif.txt"
    fi
  fi
}

################################################
#               dependency check               #
################################################
depend_check() {
  #printf '%b' "depend_check: $arg \n"
  for arg; do
    command -v "$arg" 2>/dev/null || { notif >&2 "Error: Could not find \"$arg\" application."; exit 1; }
  done
}

################################################
#          check if input files > min          #
################################################
nb_files_check() {
  nb_files="$1"
  min_nb_files="$2"
  if [ $1 -lt $2 ]; then
    notif "$1 file selected, \"$(basename -- $0)\" needs at least $2 input file(s)."
    exit 1
  fi
}

################################################
#                    script                    #
################################################
nb_files_check $# 1

depend_check idok-x86_64

command idok-x86_64 $*

exit 0
