#!/bin/sh
# $Id: CreateRecCover.sh,v 1.4 2006/04/02 16:49:28 cjac Exp $
#
# Create covers that can be shown by Enigma Skin
#
# Could also be used for other skins as the Filenames are defined below.
#
#
# Needs :
# convert 	     From ImageMagick - no special version, install what came with 
#                your Distribution
# vdr2jpeg	     Download from http://www.deltab.de/vdr/vdr2jpeg.html
# sed
# svdrpsend.pl	  Only for --screenshot (from VDR package)
# uudecode	     Only for --screenshot
#
#
# Options :
# --screenshot : create a screenshot of the current frame VDR shows on your TV
#                needs recordingdirecory as parameter
# before	:       used when called from VDR (using the VDR -r parameter)
#                see "B." below
# edited	:       used when called from VDR (using the VDR -r parameter)
#                see "B." below
#
#
# This Script can be run in three ways :
#
# A. with a Recording name as Parameter 1 - creates a Cover with vdr2jpg at
# frame $FRAME or converts existing cover.jpg to the right size.
#
# B. within the -r script of VDR - given the same parameter that the -r script 
# gets.
#  - Called with "before" as parameter 1 it will try to get the tvm2vdr Image 
#    using the EventID (is also saved as cover.jpg) and if that fails then grab 
#    the Frame $TEMPFRAME with vdr2jpeg.
#  - Called with "edited" as parameter 1, if cover.jpg is not there it will grab 
#    the frame $FRAME with vdr2jpeg.
#
# C. called with "--screenshot" and Recording name as Parameter it will use the
# svdrp command GRAB to get current Picture that VDR shows. Can be used in
# reccmds.conf to create the Cover Image.
#
#
# Examples :
#
#  - Use manually:
#    /usr/local/bin/CreateRecCover.sh \
#    /video/Movies/The_Matrix/2006-01-01.00.00.99.99.rec
#
#  - Use as VDR "-r" script :
#    vdr -r /usr/local/bin/CreateRecCover.sh
#
#  - If you have already got a VDR "-r" script, then put this in one of the 
#    first lines :
#    /usr/local/bin/CreateRecCover.sh "$1" "$2"
#
#  - reccmds.conf - to manually create a Screenshot during Replay :
#    Create Cover : /usr/local/bin/CreateRecCover.sh --screenshot


### CHANGE HERE 

# Where to log output - set to /dev/null for no output/logging
LOG=/var/log/CreateRecCover.log

# Where to find the Images created by tvmovie2vdr and type
EPGIMAGES=/ramdisk/epgimages
EPGIMAGETYPE=png

# Where to find vdr2jpeg
VDR2JPG=$(which vdr2jpeg)

# Default frame number to get with vdr2jpeg (1/25s)
FRAME=500

# Temporary Frame taken at start of recording if Image from tvm2vdr not found 
# (15000 = 10 min.)
# 0=deaktivate (no image is taken until recording is cutted)
TEMPFRAME=15000

# X/Y size for text2skin (Enigma) Image
Xres=120
Yres=90

# Name of the Cover for text2skin (Enigma)
Text2SkinCover=Cover-Enigma.jpg

# Where to find convert from ImageMagick
Convert=$(which convert)

# Debugging ?
DEBUG=no

### DON'T CHANGE BELOW HERE !!


ZEROES=00000000

function CreateCover()
{
  if [ -d "$1" -a "${1##*.}" = "rec" -o "${1##*.}" = "rec/" ]; then
    if [ "$screenshotbackground" = "yes" ]; then
      # Grab Screenshot and convert to text2skin Image
      sleep 1
      svdrpsend.pl 'grab -' | sed -n -e 's/^216-//p' -e '1ibegin-base64 644 -' -e '$a====' | uudecode > "$1/cover.jpg"
      if [ -s "$1/cover.jpg" ]; then
        $Convert -resize "$Xres"x"$Yres" "$1/cover.jpg" "$1/$Text2SkinCover"
        svdrpsend.pl MESG Create Skin Cover : Screenshot done.
      else
        svdrpsend.pl MESG Create Skin Cover : ERROR, Screenshot NOT created!
      fi
    else
      # if cover.jpg is there the it came from tvm2vdr or some other script
      if [ -e "$1/cover.jpg" ]; then
	# If text2skin cover not there then create it
	[ ! -e "$1/$Text2SkinCover" ] && $Convert -resize "$Xres"x"$Yres" "$1/cover.jpg" "$1/$Text2SkinCover"
      else
	# Create Cover
	cd $1
	$VDR2JPG -r ./ -f $FRAME -x $Xres -y $Yres
	mv -f ${ZEROES:${#FRAME}}$FRAME.jpg $Text2SkinCover
	cd -
	[ $DEBUG = yes ] && ls -al $1/
      fi
    fi
  else
    printf "\nERROR: parameter 1 is not a VDR Recording Directory !\n\n"
  fi
}


### MAIN SCRIPT


printf "\n\n---------------------------------------------------------------------\n\n$(date) : Start ${0##*/} %s\n\n" "$*" >>$LOG

if [ $# -lt 1 ]; then
  printf "\nERROR: no parameter given, please read $0 for more info ...\n\n" | tee -a $LOG
  exit 1
fi

# Get "--" options
while [ "${1:0:2}" = "--" ]; do 
  eval ${1:2}=yes
  shift
done

if [ "$screenshot" = "yes" ]; then
  # Call Script in the Background to create Screenshot without menu
  $0 --screenshotbackground "$1" >>$LOG 2>&1 &
  exit 0
fi

if [ -z "$VDR2JPG" ]; then
  printf "\n\nERROR: vdr2jepg not found, please see $0 for more info ...\n\n" | tee -a $LOG
  exit 1
fi

# from now on all output to Logfile
exec 1>>$LOG 2>&1


if [ $# -eq 1 ]; then
  [ $DEBUG = yes ] && set -x

  # Call Function to Create the Cover
  CreateCover "$1"
else
 
  case "$1" in
    before|edited)
	# Start this script in the background so that we don't crash VDR if it takes too long.
	$0 $1-background "$2" >>$LOG 2>&1 &
    ;;
    before-background)
	[ $DEBUG = yes ] && set -x

	# wait for VDR to create recording and info.vdr
	WaitCnt=0
	while [ ! -e "$2/info.vdr" -a $WaitCnt -lt 10 ]; do
	  sleep 10
	  let WaitCnt++
	done
	EventID=$(egrep "^E " "$2/info.vdr" | cut -f 2 -d ' ')
	[ ! -z "$EventID" ] && if [ -e "$EPGIMAGES/$EventID.$EPGIMAGETYPE" ]; then
	  # Save Original and text2skin Cover
	  [ "$EPGIMAGETYPE" = "jpg" ] && cp "$EPGIMAGES/$EventID.$EPGIMAGETYPE" "$2/cover.jpg" || $Convert "$EPGIMAGES/$EventID.$EPGIMAGETYPE" "$2/cover.jpg"
	  cp "$2/cover.jpg" "$2/$Text2SkinCover"
	else

	  # Create Temporary Cover ?
	  if [ $TEMPFRAME -gt 0 -a  ! -e "$2/$Text2SkinCover" -a ! -e "$2/CreateSkinCover.working" ]; then
	    FRAME=$TEMPFRAME

	    # wait until TEMPFRAME is in Recording
	    printf "$$" > "$2/CreateSkinCover.working"
	    sleep $(expr $TEMPFRAME \/ 25 \+ 20)
	    rm -f "$2/CreateSkinCover.working"

	    # Call Function to Create the Cover
	    CreateCover "$2"
	  else
	    [ -e "$2/CreateSkinCover.working" ] && printf "\n$0 is allredy waiting to create create Cover, Exiting ...\n\n"
	    [ -e "$2/$Text2SkinCover" ] && printf "\n$0 : $Text2SkinCover is already there, Exiting ...\n\n"
	  fi
	fi
    ;;
    edited-background)
	[ $DEBUG = yes ] && set -x

	# If Cover not there then Create it now
	[ ! -e "$2/$Text2SkinCover" ] && $0 "$2"
    ;;
    *)
	# Nothing at the moment
    ;;
  esac
fi

printf "\n\n$(date) : END ${0##*/} %s\n\n"i "$*" >>$LOG
