#!/bin/bash
#
# requires: ...topnm, pnmscale, pnmcomp, ppmntsc, ppmtoy4m, mpeg2enc
#
#
## DON'T CHANGE /FILE TO CONVERT
IMG=$1
#
## DON'T CHANGE
MPG=$2
#
## DON'T CHANGE
THEMECACHEDIR=$3
#
## DON'T CHANGE
COVERXY=$4
#
##DON'T CHANGE
COVERSIZE=$5
#
DIR=`dirname "$MPG"`
if [ ! -d "$DIR" ]; then
  mkdir -p "$DIR"
fi
#
#
# get the file type and set the converts to JPEG
#
#supported picture-formats for covers are bmp,png,gif,jpg
#
FILE_TYPE=`file -i -L -b "$IMG" 2>/dev/null | cut -f2 -d/`
case "$FILE_TYPE" in
  bmp)
  PICFORMAT='Windows or OS/2 bitmap .short bmp'
  ;;
  png | x-png)
  PICFORMAT='Portable Network Graphics .short png'
  ;;
  gif)
  PICFORMAT='CompuServe Graphics Interchange .short gif'
  ;;
  jpg | jpeg)
  PICFORMAT='Joint Photographic Expert Group .short JPEG ;)'
  ;;
  *)
  echo "filetype '$FILE_TYPE' is not supported"
  exit 1
  ;;
esac
#
#
LANG=C # get the decimal point right
#
#
# resize, convert the cover to jpegs and composite the cover with background
#
IMG2=/tmp/resize-background.jpg
COVIMG=$THEMECACHEDIR/background.jpg
COMPIMG=/tmp/composite.jpg
#
#
convert -verbose -resize $COVERSIZE! "$IMG" "$IMG2"
#
composite -verbose -geometry $COVERXY "$IMG2" "$COVIMG" "$COMPIMG" 
#
# now run the conversion
#
jpeg2yuv -n 1 -I p -f 25 -j $COMPIMG | \
mpeg2enc -f 7 -T 90 -F 3 -np -a 2 -v 2 -o "$MPG"
#
# log out and cleanup
#
rm $IMG2
rm $COMPIMG
echo ""
echo ""
echo "##########################################"
echo "##  This script is licensed under GPL2  ##"
echo "##########################################"
echo ""
echo ""
#
## UNCOMMENT THE NEXT LINE FOR DEBUG ( #exit 2)
# exit 2
#
echo "FORMAT:        $FORMAT"
echo "Aufloesung:    $TW x $TH"
echo "IMG:           $IMG"
echo "IMG2:          $IMG2"
echo "MPG:           $MPG"
echo "THEMECACHEDIR: $THEMECACHEDIR"
echo "BACKGROUND:    $COVIMG"
echo "COMPIMAGE:     $COMPIMG"
echo "COVERXY:       $COVERXY"
echo "COVERSIZE:     $COVERSIZE"
echo "DIR:           $DIR"
echo "FILE_TYPE:     $FILE_TYPE"
echo "PICFORMAT:     $PICFORMAT"
echo ""
if [ -f /usr/bin/convert ]; then
echo "convert:       /usr/bin/convert exist"
elif [ -f /usr/local/bin/convert ]; then
echo "convert:       /usr/local/bin/convert exist"
else
echo "convert:       convert not in /usr/bin nor /usr/local/bin"
fi
#
if [ -f /usr/bin/composite ]; then
echo "composite:     /usr/bin/composite exist"
elif [ -f /usr/local/bin/composite ]; then
echo "composite:     /usr/local/bin/composite exist"
else
echo "composite:     composite not in /usr/bin nor /usr/local/bin"
fi
#
if [ -f /usr/bin/jpeg2yuv ]; then
echo "jpeg2yuv:      /usr/bin/jpeg2yuv exist"
elif [ -f /usr/local/bin/jpeg2yuv ]; then
echo "jpeg2yuv:      /usr/local/bin/jpeg2yuv exist"
else
echo "jpeg2yuv:      jpeg2yuv not in /usr/bin nor /usr/local/bin"
fi
#
if [ -f /usr/bin/mpeg2enc ]; then
echo "mpeg2enc:      /usr/bin/mpeg2enc exist"
elif [ -f /usr/local/bin/mpeg2enc ]; then
echo "mpeg2enc:      /usr/local/bin/mpeg2enc exist"
else
echo "mpeg2enc:      mpeg2enc not in /usr/bin nor /usr/local/bin"
fi
echo ""
echo "PATH:          $PATH"
echo ""
echo "ImageMagick Version:"
Magick-config --version
echo "convert Version:"
convert -version
echo ""
echo "composite Version:"
composite -version
echo ""
echo ""
#echo "mjpegtools Version:"
#echo ""
#echo ""
