#!/bin/sh -l
# use login-shell to retrieve vars from /etc/profile

export ELM_THEME=/usr/share/elementary/themes/default.edj

export DISPLAY=:0.0

# take advantage of multi-core/cpu with Evas (threads)
if test $(grep -ic "^processor" /proc/cpuinfo) -gt 1; then
  export EVAS_RENDER_MODE=non-blocking
fi

# get the resolution X.org has actually started
XRANDR_INFO=/tmp/xrandr
while true; do
  xrandr -display :0.0 -q > $XRANDR_INFO
  [ -s $XRANDR_INFO ] && break
done

RES=`cat $XRANDR_INFO | grep "Screen 0" | sed 's/.* current \([0-9]*\) x \([0-9]*\),.*/\1x\2/'`
RESX=`echo $RES | cut -f1 -dx`
RESY=`echo $RES | cut -f2 -dx`
ENNA_RES="${RESX}x${RESY}"

while true; do
  enna -g $ENNA_RES -t stb -f -c /etc/enna.cfg
  if [ "$?" = 0 ]; then
    [ -x /usr/bin/vdrshutdown ] && /usr/bin/vdrshutdown
    systemctl poweroff
    exit 0
  else
    # ensure no MPlayer instance is running if enna has previously crashed
    killall -9 mplayer > /dev/null 2>&1
    exit 1
  fi
done
