#!/bin/sh
#
# mdrip 0.1
#         extension for drip ( http://drip.sourceforge.net ), which enables
#         - encoding with multiple computers
#         - interruptable encoding
#         mdrip uses the dripcache files and the parameters of drip.
#         each dripcache file is encoded in a single step and combined afterwards
#
# Usage
#         General
#         -------
#         Use drip to create dripcache files. Look inside ~/.drip/drip.log for the
#         encoding parameters and replace the drip_params in the config section.
#         Go to dripcache location. Call mdrip for each encoding process once
#         After encoding is finished, call mdrip_combine
#
#         Encoding with multiple computer (or even MP computers)
#         Call mdrip for every encoding process on the target computer. You may call mdrip on
#         a 4 way MP machine 3 times, so you have some cpu time left for real work :-)
#         Each process (on a MP computer or network distributed comuters) reserves a dripcache
#         file for encoding by creating a coding$n file. After encoding one dripcache file, mdrip
#         encodes the next dripcache file
#
#         Stopping encoding
#         -----------------
#         encoding can be stopped by creating a file name mdrip_stop in general
#         or mdrip_stop.HOST.PID for a specific coding process. After the current
#         encoding has finished and a mdrip_stop file exists, no futher encoding is done
#
#         mdrip_reset_unfinished_codings
#         ------------------------------
#         Can be used to clear up the reservation files (coding*)
#         Should only be used, when no encoding is in progress!
#
#         mdrip_combine
#         -------------
#         combines the single encoded avis to one big avi.
#         Should be called after all dripcachs files are encoded.
#         Should be used on the machine with local (i.e. not network) filesystem for performance
#         reasons. Uses avicat from avifile package.
#
# Install
#         copy mdrip somewhere in your path (e.g. /usr/local/bin )
#         create links:
#         ln -s mdrip mdrip_reset_unfinished_codings
#         ln -s mdrip mdrip_combine
#
# Hint
#         Reduce the size of the dripcache size to something smaller than the current ~ 2 GB
#         Search for "if (written_blocks>990000) {" in src/callbacks.c and change it to
#         something like "if (written_blocks>290000) {", which results in filesize ~ 600 MB
#         This will increase concurrence and the time where the encoding process is interuptable
#         Don't make the size too small. The autocropping step is executed for every file!
#
# Todo
#         Handle the autocropping once and use the values for every process
#         Integration in the GUI
#
# Changelog
#     mdrip v 0.1,                       Rainer Lay ( mailto:rainer.lay@gmx.de )
#         Initial release
#
#####################################################################################################
#
# config start
#
#
# parameter for dripencoder
# specific to each video
# can be found in ~/.drip/drip.log
drip_params="-b 930 -as 0 -as2 1 -w 640 -h 352 -n 1 -ac -e 182447 -r 1,77778 -d -au 85 -l 7308574720"
#
# have to be set to YES, if the avi parts (0.avi, 1.avi, ...) should be deleted after combining
delete_avi_parts=no
#
# name of result avi
result_avi=result.avi
#
# if set to YES, the avi parts are combined automatically together
# Should be used with care. Combining should be done on computer with locally stored avis
auto_combine=no
#
# config end
#
#####################################################################################################




scriptname="$(basename $0)"
echo $scriptname

case $scriptname in 
mdrip)
	
    if [ -z "$1" ]; then
	$0 `ls dripcache*.vob`
	exit 0
    fi

    for f in $*; do
	n=`echo $f|cut -c14- |sed 's/.vob//g'`

	if [ ! -f $n.avi -a ! -f coding$n ]; then
	    # I want to code it!
	    touch coding$n
	    # I (host, pid) am coding it
	    touch coding$n${HOST}$$ 	
	    echo "nice dripencoder -A $drip_params $f $n.avi"
	    rm coding$n${HOST}$$
	    rm coding$n
	fi
	[ -f mdrip_stop -o -f mdrip_stop.${HOST}$$ ] && break
    done

    # coding finished?
    coding_files=`ls coding*`
    if [ $auto_combine="YES" -a -z "$coding_files" ]; then
	echo "coding is finished. Start combining"
	exe="`dirname $0`/mdrip_combine"
	$exe
    fi
    ;;

mdrip_reset_unfinished_codings)
    if [ -z "$1" ]; then
	$0 `ls dripcache*.vob`
	exit 0
    fi

    for f in $*; do
	n=`echo $f|cut -c14-|sed 's/.vob//g'`

	if [ -f coding$n ]; then
	    # coding was in progress
	    # delete all coding related files
	    rm  $n.avi `ls coding$n*`
	fi

	if [ ! -f $n.avi ]; then
	    echo "$n.avi is missing"
	fi
    done
    ;;
        
mdrip_combine)
    [ -f 100.avi ] && echo "this script only combines avis up to 99.avi. You have to extend it for avis > 100.avi!"

    avis=""
    for n in {,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}; do
	if [ -f $n.avi ]; then
	    avis="$avis $n.avi"
	fi
    done

    avicat $avis -o $result_avi

    [ $? -a ! "$delete_avi_parts"="YES"] && rm $avis
    ;;

esac    

