#!/bin/bash

export DISPLAY=:0

DELAY=5

function ShowFile() {
    MON=${1}
    XOFF=${2}
    DUR=${3}
    URI=${4}

    f=${URI#file:}
    if [ "${URI:0:5}" == "file:" -a ! -r ${f} ]; then
      return
    fi
    xdg-open ${f} >/dev/null 2>/dev/null &
    PID=$!
    WID=""
    i=0
    while [ "${i}" -lt 100 -a -z "${WID}" ]; do
      if [ "${URI:0:5}" == "file:" -a ! -r ${f} ]; then
        break
      fi
      i=$((${i}+1))
      CPID=""
      j=0
      while [ "${j}" -lt 100 -a -z "${CPID}" ]; do
        if [ "${URI:0:5}" == "file:" -a ! -r ${f} ]; then
          break
        fi
        j=$((${j}+1))
        CPID=$(pgrep -P ${PID} | tail -n 1)
      done
      WID=$(wmctrl -l -p | grep "\s${CPID}\s")
      WID=${WID%% *}
    done
    if [ -n "${WID}" ]; then
      wmctrl -i -e 0,${XOFF},0,-1,-1 -r ${WID}
      sleep 0.5
      wmctrl -i -t 1 -r ${WID}
      if [ ${DUR} == "0" ]; then
        while [ $(pgrep -P ${PID} | tail -n 1) == ${CPID} ]; do
          sleep 0.5
        done
      else
        sleep ${DUR}
      fi
      wmctrl -i -c ${WID}
    fi
}

function PlayPls() {
  exec 3<${1}
  while read -u 3 -r pl; do
    d=${pl%%;*}
    f=${pl##*;}
    if [ "${f:0:5}" == "file:" ]; then
      ShowFile ${2} ${3} ${d} file:${1%/*}/${f:5}
    else
      ShowFile ${2} ${3} ${d} ${f}
    fi
  done
  exec 3<&-
}

function Worker() {
  MON=${1}
  XOFF=${2}

  while /bin/true; do
    if [ "$(wmctrl -d | wc -l)" != "2" ]; then
      if [ ${MON} = 1 ]; then
        wmctrl -n 2
        wmctrl -s 1
      else
        sleep 1
        continue
      fi
    fi

    e=0
    for m in $(ls -1d /media/usb?); do
      if [ -s ${m}/playlist${MON}.txt ]; then
        e=1
        ${0} -p ${m}/playlist${MON}.txt ${MON} ${XOFF}
      elif [ -s ${m}/playlist.txt ]; then
        e=1
        ${0} -p ${m}/playlist.txt ${MON} ${XOFF}
      elif [ ! -e ${m}/playlist${MON}.txt -a ! -e ${m}/playlist.txt ]; then
        fl=$(find /media/usb? -type f)
        for f in ${fl}; do
          e=1
          ShowFile ${MON} ${XOFF} ${DELAY} file:${f}
        done
      fi
    done
    if [ "$e" == "0" ]; then
      if [ -s /var/www/html/pictur/upload/playlist${MON}.txt ]; then
        e=1
        ${0} -p /var/www/html/pictur/upload/playlist${MON}.txt ${MON} ${XOFF}
      elif [ -s /var/www/html/pictur/upload/playlist.txt ]; then
        e=1
        ${0} -p /var/www/html/pictur/upload/playlist.txt ${MON} ${XOFF}
      elif [ ! -e /var/www/html/pictur/upload/playlist${MON}.txt -a \
             ! -e /var/www/html/pictur/upload/playlist.txt ]; then
        fl=$(find /var/www/html/pictur/upload -type f)
        for f in ${fl}; do
          e=1
          ShowFile ${MON} ${XOFF} ${DELAY} file:${f}
        done
      fi
    fi
    if [ "$e" == "0" ]; then
      sleep 10
    fi
  done
}

if [ "${1}" = "-p" -a -n "${2}" -a -n "${3}" -a -n "${4}" ]; then
  PlayPls "${2}" ${3} ${4}
  exit 0
fi

if [ "${1}" = "-m" -a -n "${2}" -a -n "${3}" ]; then
  trap "exit 0" TERM
  Worker ${2} ${3}
else
  trap "wmctrl -n 1;trap - TERM; kill -TERM -${$}; exit 0" INT TERM HUP

  IFS=$'\n' read -d '' -a XR < <(xrandr --listmonitors)
  MONS=${XR[0]#Monitors: }
  if [ -z "${MONS}" ]; then
    MONS=1
  fi

  OffsetX[1]=0 # X-Offset für den 1. Monitor
  for((i=2;i<=${MONS};i++)) {
    # X-Offset des aktuellen Monitors =
    # X-Offset des vorherigen Monitors ...
    OffsetX[${i}]=${XR[$(($i-1))]#*$(($i-2)): * }
    OffsetX[${i}]=${OffsetX[${i}]%%/*}
    # ... plus X-Auflösung des vorherigen Monitor
    OffsetX[${i}]=$((${OffsetX[${i}]}+${OffsetX[$(($i-1))]}))
    ${0} -m ${i} ${OffsetX[${i}]} &
  }
  Worker 1 0
fi
