#!/bin/bash

DBSERVER=localhost
EPGIMAGE_DIR="/var/cache/vdr/epgimages"
MPOSTER_DIR="/var/cache/vdr/epgimages/movies"
SPOSTER_DIR="/var/cache/vdr/epgimages/series"
SVDRP="svdrpsend"


CHANNEL="$($SVDRP CHAN |grep ^250 |cut -c 5-6)"
SEARCH="$($SVDRP LSTE $CHANNEL now |grep ^215-E |cut -d " " -f 2)"
n=0
if test -e $EPGIMAGE_DIR/$SEARCH'_0.jpg';then n=1 ;fi

# suche in Db events in Spalte masterid nach $SEARCH wenn gefunden scrmovieid aus gefundener Zeile = $a
b=$(mysql --host=$DBSERVER --user=epg2vdr --password=epg --database=epg2vdr --execute="select scrmovieid from events where masterid like '$SEARCH' limit 1;"|cut -f2)
a=$(echo $b | cut -d' ' -f 2)
if test -e $MPOSTER_DIR/$a/poster.jpg; then
	if ! test -e $EPGIMAGE_DIR/$SEARCH'_'1.jpg;then
	for i in $MPOSTER_DIR/$a/*.jpg; do
	    if [ -f $i ]; then
    	    THUMB=$SEARCH'_'$n.jpg
	    cp -s $i $EPGIMAGE_DIR/$THUMB
	    n=`expr $n + 1`
	    fi
	done
	fi
fi

# suche in Db events in Spalte masterid nach $SEARCH wenn gefunden scrseriesid aus gefundener Zeile = $a
b=$(mysql --host=$DBSERVER --user=epg2vdr --password=epg --database=epg2vdr --execute="select scrseriesid from events where masterid like '$SEARCH' limit 1;"|cut -f2)
c=$(echo $b | cut -d' ' -f 2)
if ! test -e $EPGIMAGE_DIR/$SEARCH'_'1.jpg;then
    if test -e $SPOSTER_DIR/$c/poster1.jpg;then  cp -s $SPOSTER_DIR/$c/poster1.jpg $EPGIMAGE_DIR/$SEARCH'_'$n.jpg;n=`expr $n + 1`;fi
    if test -e $SPOSTER_DIR/$c/poster2.jpg;then  cp -s $SPOSTER_DIR/$c/poster2.jpg $EPGIMAGE_DIR/$SEARCH'_'$n.jpg;n=`expr $n + 1`;fi
    if test -e $SPOSTER_DIR/$c/banner1.jpg;then  cp -s $SPOSTER_DIR/$c/banner1.jpg $EPGIMAGE_DIR/$SEARCH'_'$n.jpg;n=`expr $n + 1`;fi
    if test -e $SPOSTER_DIR/$c/fanart1.jpg;then  cp -s $SPOSTER_DIR/$c/fanart1.jpg $EPGIMAGE_DIR/$SEARCH'_'$n.jpg;n=`expr $n + 1`;fi
    if test -e $SPOSTER_DIR/$c/fanart2.jpg;then  cp -s $SPOSTER_DIR/$c/fanart2.jpg $EPGIMAGE_DIR/$SEARCH'_'$n.jpg;n=`expr $n + 1`;fi
    if test -e $SPOSTER_DIR/$c/fanart3.jpg;then  cp -s $SPOSTER_DIR/$c/fanart3.jpg $EPGIMAGE_DIR/$SEARCH'_'$n.jpg;n=`expr $n + 1`;fi
    for i in $SPOSTER_DIR/$c/actor*.jpg; do
    if [ -f $i ]; then
    THUMB=$SEARCH'_'$n.jpg
    cp -s $i $EPGIMAGE_DIR/$THUMB
    n=`expr $n + 1`
    fi
    done
fi
