#!/bin/sh
#
# (P) & (C) 2003 by Dr. Peter Bieringer <pb at bieringer dot de>
#
# Original site of publishing:
#  ftp://ftp.bieringer.de/pub/linux/dvd/dvdauthor-addons/
#
# License: GPLv2
#
# Requires:
#  - gozer, imlib2, netpbm-progs
#
# ChangeLog
# 20030510/PB: initial creation
# 20030511/PB: import info function
# Version: 0.2.0
# Author: Dimitrios Dimitrakos mail@dimitrios.de
# $Id: helperfunctions.sh,v 1.11 2005/02/07 16:05:50 dimi Exp $

vco_user=`id -n -u`
homedir=`cat /etc/passwd|grep "^${vco_user}:"|cut -f6 -d ':'`
. $homedir/.vdrconvert/vdrconvert.env

##### Functions ######

## Generate a PNG image from given text
text_to_png() {
	local file_png="$1"; shift		# Destination file name (required)
	local font="$1"; shift			# Font (required)
	local color_fg="$1"; shift		# Foreground color (required)
	local color_bg="$1"; shift		# Background color (required)
	local text="$1"; shift			# Text (required)
	local addopts="$*"; shift		# Other options (optional)

	if [ -n "$PATH_FONTS" -a -n "$DVD_USE_FONT" ]
	then
		local options_gozer="--fontpath $PATH_FONTS"
	fi

	local nice_call=""
	if [ -n "$PRIO" ]
	then
		local nice_call="nice -n $PRIO"
	fi

	[ -z "$file_png" ] && exit

	[ ! -z "$RECODE" ] && rtext="`echo \"$text\" | $RECODE`" || rtext=$text
	# Create PNG from text
	$nice_call $GOZER $options_gozer --font $font --foreground $color_fg --background $color_bg --text "$rtext" $addopts $tmp_png || exit
}

## Generate a PNM image from given text
text_to_pnm() {
	local file_pnm="$1"; shift		# Destination file name (required)
	local file_alpha_pgm="$1"; shift	# Destination alpha channel file name (optional)
	local font="$1"; shift			# Font (required)
	local color_fg="$1"; shift		# Foreground color (required)
	local color_bg="$1"; shift		# Background color (required)
	local text="$1"; shift			# Text (required)
	local addopts="$*"; shift		# Other options (optional)

	if [ -n "$PATH_FONTS" -a -n "$DVD_USE_FONT" ]
	then
		local options_gozer="--fontpath $PATH_FONTS"
	fi

	local nice_call=""
	if [ -n "$PRIO" ]
	then
		local nice_call="nice -n $PRIO"
	fi

	local tmp_png="`$nice_call mktemp tmp-txt.png.XXXXXX`"

	# Workaround for gozer/mktemp suffix problem
	local tmp_pngnew="${tmp_png}.png"
	rm "${tmp_png}"
	local tmp_png="$tmp_pngnew"

	[ -z "$tmp_png" ] && exit

	[ ! -z "$RECODE" ] && rtext="`echo \"$text\" | $RECODE`" || rtext=$text
	# Create PNG from text
	$nice_call $GOZER $options_gozer --font $font --foreground $color_fg --background $color_bg --text "$rtext" $addopts $tmp_png || exit

	# Convert PNG to PNM
	$nice_call pngtopnm $tmp_png >$file_pnm

	if [ -n "$file_alpha_pgm" ]
	then
		# Create alpha channel PNM from PNG
		$nice_call pngtopnm -alpha $tmp_png >$file_alpha_pgm
	fi

	$nice_call rm -f $tmp_png
}

# Put text into a bitmap
put_text() {
	local file="$1"; shift
	if [ ! -z "$PATH_FONTS" -a ! -z "$DVD_USE_FONT" ]
	then
		local font="$1"
		shift
	fi
	#local style="-s /usr/local/share/vdrconvert/style/shadow"
	local style="$1"; shift
	local color_fg="$1"; shift
	local color_bg="$1"; shift
	local loc_x="$1"; shift
	local loc_y="$1"; shift
	local text="$1"; shift
	local rest="$*"

	local tmp_png="`mktemp tmp-txt.png.XXXXXX`"
	local tmp_pnm="`mktemp tmp-txt.pnm.XXXXXX`"
	local tmp_alphapnm="`mktemp tmp-txt.alpha.pnm.XXXXXX`"
	local tmp_output="`mktemp tmp-output.pnm.XXXXXX`"

	if [ -z "$tmp_png" -o -z "$tmp_pnm" -o -z "$tmp_alphapnm" -o -z "$tmp_output" ]; then
		echo "ERROR: one or more tmp files cannot be created" >&2
		exit 1
	fi

	# Workaround for gozer/mktemp suffix problem
	local tmp_pngnew="${tmp_png}.png"
	rm "${tmp_png}"
	local tmp_png="$tmp_pngnew"

	if [ -z "$text" ]; then
		# Minimal text
		local text=" "
	fi

	if [ ! -w "." ]; then
		echo "ERR(put_text): file not writable: $file"
		return 1
	fi

	[ ! -z "$RECODE" ] && rtext="`echo \"$text\" | $RECODE`" || rtext=$text
	if [ -z "$PATH_FONTS" -o -z "$DVD_USE_FONT" ]
	then
	#set -x
		if [ -n "$PRIO" ]; then
			nice -n $PRIO $GOZER $style --background $color_bg --foreground $color_fg --text "$rtext" $rest $tmp_png 
		else
			$GOZER $style --background $color_bg --foreground $color_fg --text "$rtext" $rest $tmp_png 
		fi
	else
		if [ -n "$PRIO" ]; then
			nice -n $PRIO $GOZER $style --fontpath $PATH_FONTS --font $font --background $color_bg --foreground $color_fg --text "$rtext" $rest $tmp_png
		else
			$GOZER $style --fontpath $PATH_FONTS --font $font --background $color_bg --foreground $color_fg --text "$rtext" $rest $tmp_png
		fi
	#set +x
	fi
	
	if [ -n "$PRIO" ]; then
		nice -n $PRIO pngtopnm $tmp_png >$tmp_pnm
		nice -n $PRIO pngtopnm -alpha $tmp_png >$tmp_alphapnm
		nice -n $PRIO pnmcomp -xoff=$loc_x -yoff=$loc_y -alpha=$tmp_alphapnm $tmp_pnm $file >$tmp_output
	else
		pngtopnm $tmp_png >$tmp_pnm
		pngtopnm -alpha $tmp_png >$tmp_alphapnm
		pnmcomp -xoff=$loc_x -yoff=$loc_y -alpha=$tmp_alphapnm $tmp_pnm $file >$tmp_output
	fi
	mv $tmp_output $file

	rm -f $tmp_png $tmp_pnm $tmp_alphapnm $tmp_output
}

# Put text justified into a bitmap
put_text_just() {
	local file="$1"; shift
	if [ ! -z "$PATH_FONTS" -a ! -z "$DVD_USE_FONT" ]
	then
		local font="$1"
		shift
	fi
	local color_fg="$1"; shift
	local color_bg="$1"; shift
	local loc_x="$1"; shift
	local loc_y="$1"; shift
	local just="$1";shift
	local text="$1"; shift
	local rest="$*"

	local tmp_png="`mktemp tmp-txt.png.XXXXXX`"
	local tmp_pnm="`mktemp tmp-txt.pnm.XXXXXX`"
	local tmp_alphapnm="`mktemp tmp-txt.alpha.pnm.XXXXXX`"
	local tmp_output="`mktemp tmp-output.pnm.XXXXXX`"

	if [ -z "$tmp_png" -o -z "$tmp_pnm" -o -z "$tmp_alphapnm" -o -z "$tmp_output" ]; then
		echo "ERROR: one or more tmp files cannot be created" >&2
		exit 1
	fi

	# Workaround for gozer/mktemp suffix problem
	local tmp_pngnew="${tmp_png}.png"
	rm "${tmp_png}"
	local tmp_png="$tmp_pngnew"

	if [ -z "$text" ]; then
		# Minimal text
		local text=" "
	fi

	if [ ! -w "." ]; then
		echo "ERR(put_text): file not writable: $file"
		return 1
	fi

	[ ! -z "$RECODE" ] && rtext="`echo \"$text\" | $RECODE`" || rtext=$text
	if [ -z "$PATH_FONTS" -o -z "$DVD_USE_FONT" ]
	then
	#set -x
		if [ -n "$PRIO" ]; then
			nice -n $PRIO $GOZER --background $color_bg --foreground $color_fg --text "$rtext" $rest $tmp_png
		else
			$GOZER --background $color_bg --foreground $color_fg --text "$rtext" $rest $tmp_png
		fi
	else
		if [ -n "$PRIO" ]; then
			nice -n $PRIO $GOZER --fontpath $PATH_FONTS --font $font --background $color_bg --foreground $color_fg --text "$rtext" $rest $tmp_png
		else
			$GOZER --fontpath $PATH_FONTS --font $font --background $color_bg --foreground $color_fg --text "$rtext" $rest $tmp_png
		fi
	#set +x
	fi
	
	if [ -n "$PRIO" ]; then
		nice -n $PRIO pngtopnm $tmp_png >$tmp_pnm
		nice -n $PRIO pngtopnm -alpha $tmp_png >$tmp_alphapnm
	else
		pngtopnm $tmp_png >$tmp_pnm
		pngtopnm -alpha $tmp_png >$tmp_alphapnm
	fi

	# get the resolution of the 'redered text'
	local imagewidth=`pnmfile $tmp_pnm|awk '{print $4}'`
	#imageheight=`pnmfile $tmp_but_image|awk '{print $6}'`
	local txt_x=0
	
	case $just in
	    "left")
		txt_x=$loc_x
	    ;;
	    "right")
		txt_x=$[ $loc_x - $imagewidth ]
	    ;;
	    "center")
		txt_x=$[ $loc_x - ($imagewidth / 2) ]
	    ;;
    
	esac
	if [ -n "$PRIO" ]; then
		nice -n $PRIO pnmcomp -xoff=$txt_x -yoff=$loc_y -alpha=$tmp_alphapnm $tmp_pnm $file >$tmp_output
	else
		pnmcomp -xoff=$txt_x -yoff=$loc_y -alpha=$tmp_alphapnm $tmp_pnm $file >$tmp_output
	fi
	mv $tmp_output $file

	rm -f $tmp_png $tmp_pnm $tmp_alphapnm $tmp_output
}

# Put a bitmap file into another bitmap file
put_file() {
	local file=$1
	local loc_x=$2
	local loc_y=$3
	local file2=$4

	if [ -n "$PRIO" ]; then
		local tmp_pnm="`nice -n $PRIO mktemp tmp-txt.pnm.XXXXXX`"
	else
		local tmp_pnm="`mktemp tmp-txt.pnm.XXXXXX`"
	fi

	if [ -z "$tmp_pnm" ]; then
		echo "ERROR: one or more tmp files cannot be created" >&2
		exit 1
	fi

	if [ ! -w "." ]; then
		echo "ERR(put_file): file not writable: $file"
		return 1
	fi
	if [ ! -r "$file" ]; then
		echo "ERR(put_file): file not readable: $file"
		return 1
	fi
	if [ ! -r "$file2" ]; then
		echo "ERR(put_file): file2 not readable: $file2"
		return 1
	fi

	if [ -n "$PRIO" ]; then
		nice -n $PRIO pnmpaste $file2 $loc_x $loc_y $file >$tmp_pnm
	else
		pnmpaste $file2 $loc_x $loc_y $file >$tmp_pnm
	fi
	
	status="$?"

	mv $tmp_pnm $file
	rm -f $tmp_pnm

	return $status
}

find_streams()
{
    local DEMUXER="$1"; shift
    local STREAMSDIR="$1"; shift
    local WANTPIDS="$*"

    local HAVESTREAMS=""

    for i in $STREAMSDIR/*.ac3 $STREAMSDIR/*.mpa
    do
        if [ -f $i ]
	then
	    HAVESTREAMS="$HAVESTREAMS $i"
	fi
    done
    if [ ! -z "$DEMUXER" -a ! -z "$WANTPIDS" ]
    then
	for k in $WANTPIDS
	do
		let pid=`echo $k|cut -c5-`
		case $k in
			apid[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32])
	  			case $DEMUXER in
					vdrsync|px)
						j=`printf "c%d" $[ $pid-1 ]`
						jj="mpa"
						;;
					vdrsync[2,3])
						j=`printf "%04d" $[ $pid+10-1 ]`
						jj="mpa"
						;;
					pva)
						[ $pid -eq 1 ] && j="001" || j=`printf "001-%02d" $[ $pid-1 ]`
						jj="mpa"
						;;
					*)
						;;
				esac
				;;

		    	dpid[1,2])
	  			case $DEMUXER in
					vdrsync)
						j="bd"
						jj="ac3"
						;;
					*)
						j=`printf "%03d" $pid`
						jj="ac3"
						;;
				esac
				;;
			*)
			;;
		esac
		[ ! -z "${NSTREAMS}" ] && NSTREAMS="${NSTREAMS} $j.$jj" || \
			    			    NSTREAMS="$j.$jj"
	done
	echo "$NSTREAMS"
    fi
}

