#!/bin/sh

##### RatioFinder
# I wrote this comment even before I typed
# a single line of code.
# I know it will work fine, because I have
# such great comments to work with.

## Take in the arguments.
tmpdir=$1
optim=$2
tiny=$3
mpeg=$4
wide=$5

/usr/bin/ulimit -u 256

if test -f /tmp/vhtemp/$tmpdir/visualhub_movdur
	then
		origwidth=`cat /tmp/vhtemp/$tmpdir/visualhub_movdur | sed -e 's/ //g' | awk -F , '{print $2}'`
		origheight=`cat /tmp/vhtemp/$tmpdir/visualhub_movdur | sed -e 's/ //g' | awk -F , '{print $3}'`
	else
		origwidth=`cat /tmp/vhtemp/$tmpdir/visualhub_dur | grep ',Video,' | grep -v parameters | head -1 | awk -F , '{print $9}'`
		origheight=`cat /tmp/vhtemp/$tmpdir/visualhub_dur | grep ',Video,' | grep -v parameters | head -1 | awk -F , '{print $10}'`
	fi
origres=`echo "$origwidth"x"$origheight"`

topcrop=`head -1 /tmp/vhtemp/$tmpdir/visualhub_cropmarks`
bottomcrop=`head -2 /tmp/vhtemp/$tmpdir/visualhub_cropmarks | tail -1`
leftcrop=`tail -2 /tmp/vhtemp/$tmpdir/visualhub_cropmarks | head -1`
rightcrop=`tail -1 /tmp/vhtemp/$tmpdir/visualhub_cropmarks`

stretchratio=1

## Compensate for HD anamorphic weirdness.
if [ "$origres" == "1440x1080" ]
then
mpeg=1.77777
fi

if [ "$origres" == "1280x1080" ]
then
mpeg=1.77777
fi

if [ "$origres" == "1280x720" ]
then
mpeg=1
fi

if [ "$origres" == "1920x1080" ]
then
mpeg=1
fi

## What about 4:3 720p stuff? A dilemma.
#if [ "$origres" == "960x720" ]
#then
#mpeg=1.77777
#fi

if [ "$origres" == "480x480" ]
then
mpeg=1.33333
fi

if [ "$origres" == "352x240" ]
then
mpeg=1.33333
fi

if [ "$mpeg" == "1.77777" ]
	then
	widthstretch=`echo $origheight \* 1.7777777778 | bc`
	stretchratio=`echo $widthstretch / $origwidth | bc -l`
fi
if [ "$mpeg" == "1.33333" ]
	then
	widthstretch=`echo $origheight \* 1.3333333333 | bc`
	stretchratio=`echo $widthstretch / $origwidth | bc -l`
fi
if [ "$mpeg" == "1,77777" ]
	then
	widthstretch=`echo $origheight \* 1.7777777778 | bc`
	stretchratio=`echo $widthstretch / $origwidth | bc -l`
fi
if [ "$mpeg" == "1,33333" ]
	then
	widthstretch=`echo $origheight \* 1.3333333333 | bc`
	stretchratio=`echo $widthstretch / $origwidth | bc -l`
fi

widthcrop1=`echo $origwidth - $leftcrop - $rightcrop | bc`
widthcrop=`echo $widthcrop1 \* $stretchratio | bc | awk -F . '{print $1}'`
heightcrop=`echo $origheight - $topcrop - $bottomcrop | bc`


origratio=`echo $widthcrop / $heightcrop | bc -l | cut -c 1-3`
origres=`echo "$widthcrop"x"$heightcrop"`
origpx=`echo $widthcrop \* $heightcrop | bc`
#origwidth=`echo $origres | awk -F x '{print $1}'`
#origheight=`echo $origres | awk -F x '{print $2}'`

forcewide=`cat /tmp/vhtemp/$tmpdir/visualhub_dvdwide`

#### Choose Your Destiny. ####
##Default of 4:3
ratio=4by3

if [ "$origratio" == "1.2" ]
then
ratio=4by3
fi

if [ "$origratio" == "1.3" ]
then
ratio=4by3
fi

if [ "$origratio" == "1.4" ]
then
ratio=4by3
fi

## 1.5 is 720x480. This works nicely for most files,
## but MPEG-2 and DV files at this res might be 16:9.
## I check for the MPEG-2 ratio later, but can't do the same for DV.
if [ "$origratio" == "1.5" ]
then
ratio=4by3
fi

if [ "$origratio" == "1.6" ]
then
ratio=16by9
fi

if [ "$origratio" == "1.7" ]
then
ratio=16by9
fi

if [ "$origratio" == "1.8" ]
then
ratio=16by9
fi

if [ "$origratio" == "1.9" ]
then
ratio=16by9
fi

### ...and ultra-wide screen detection goes BACKWARDS! WOO!
if [ "$origratio" == "2.5" ]
then
ratio=wide
fi

if [ "$origratio" == "2.4" ]
then
ratio=wide
fi

if [ "$origratio" == "2.3" ]
then
ratio=wide
fi

if [ "$origratio" == "2.2" ]
then
ratio=wide
fi

if [ "$origratio" == "2.1" ]
then
ratio=wide
fi

if [ "$origratio" == "2" ]
then
ratio=wide
fi

if [ "$origratio" == "2.0" ]
then
ratio=wide
fi


## iPod screen settings.
if [ "$optim" == "ipod" ]
then
if [ $tiny == "tiny" ]
	then
		if [ $ratio == "4by3" ]
			then
			echo 240x176
			exit 0
			fi
		if [ $ratio == "16by9" ]
			then
			echo 240x128
			exit 0
			fi
		if [ $ratio == "wide" ]
			then
			echo 240x96
			exit 0
			fi
		echo 240x176
		exit 0
	else
		if [ "$ratio" == "4by3" ]
			then
				echo 320x240
				exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo 320x176
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo 320x144
			exit 0
			fi
		fi
echo 320x240
exit 0
fi

## PSP screen settings.
if [ "$optim" == "psp" ]
then
if [ $tiny == "tiny" ]
	then
		if [ $ratio == "4by3" ]
			then
			echo 240x176
			exit 0
			fi
		if [ $ratio == "16by9" ]
			then
			echo 240x128
			exit 0
			fi
		if [ $ratio == "wide" ]
			then
			echo 240x96
			exit 0
			fi
		echo 240x176
		exit 0
	else
## Anamorphic is dead.
#		if [ "$ratio" == "4by3" ]
#			then
#				if [ "$tiny" == "wide" ]
#				then
#					echo "240x208 -padtop 32 -padbottom 32"
#					exit 0
#				else
#					if [ "$tiny" == "anam" ]
#					then
#						echo 240x272
#						exit 0
#					fi
#					echo 320x240
#					exit 0
#				fi
#			fi
		if [ "$ratio" == "16by9" ]
			then
			echo "368x208"
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo 416x176
			exit 0
			fi
		fi
echo 320x240
exit 0
fi

## PSP Firmware 3.30 screen settings.
if [ "$optim" == "psp330" ]
then
if [ $tiny == "tiny" ]
	then
		if [ $ratio == "4by3" ]
			then
			echo 320x240
			exit 0
			fi
		if [ $ratio == "16by9" ]
			then
			echo 320x176
			exit 0
			fi
		if [ $ratio == "wide" ]
			then
			echo 320x144
			exit 0
			fi
		echo 320x240
		exit 0
	else
		if [ "$ratio" == "4by3" ]
			then
			echo "368x272 -padleft 56 -padright 56"
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo 480x272
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo "480x208 -padtop 32 -padbottom 32"
			exit 0
			fi
		fi
echo 320x240
exit 0
fi

## TV screen settings.
if [ "$optim" == "tv" ]
then
	if [ "$tiny" == "h264" ]
	then
		if [ "$widthcrop" -le 640 ]
			then
			if [ "$origpx" -le 307200 ]
			then
			echo $origres
			exit 0
			fi
		fi
		if [ "$ratio" == "4by3" ]
				then
				echo 640x480
				exit 0
				fi
		if [ "$ratio" == "16by9" ]
				then
				echo 640x352
				exit 0
				fi
		if [ "$ratio" == "wide" ]
				then
				echo 640x272
				exit 0
				fi
			fi
		if [ "$tiny" == "tiny" ]
		then
			if [ "$ratio" == "4by3" ]
				then
				echo 400x304
				exit 0
				fi
			if [ "$ratio" == "16by9" ]
				then
				echo 480x272
				exit 0
				fi
			if [ "$ratio" == "wide" ]
				then
				echo 480x192
				exit 0
				fi
	else
##Use the video's original resolution if it's iPod-kosher.
		if [ "$widthcrop" -le 720 ]
			then
			if [ $origpx -le 307200 ]
				then
				echo $origres
				exit 0
				fi
			fi
		if [ "$ratio" == "4by3" ]
			then
			echo 640x480
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo 720x400
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo 720x304
			exit 0
			fi
		fi
echo 640x480
exit 0
fi

## iPhone screen settings.
if [ "$optim" == "iphone" ]
then
	if [ "$tiny" == "tiny" ]
		then
		if [ "$ratio" == "4by3" ]
			then
			echo 192x144
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo 224x128
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo 240x112
			exit 0
			fi
	fi
##Use the video's original resolution if it's iPod-kosher.
		if [ "$widthcrop" -le 480 ]
			then
			if [ "$origpx" -le 138240 ]
				then
				echo $origres
				exit 0
				fi
			fi
			if [ "$ratio" == "4by3" ]
				then
				echo 432x320
				exit 0
				fi
			if [ "$ratio" == "16by9" ]
				then
				echo 480x272
				exit 0
				fi
			if [ "$ratio" == "wide" ]
				then
				echo 480x192
				exit 0
				fi
echo 432x320
exit 0
fi

## Apple TV settings.
if [ "$optim" == "appletv" ]
then
##Use the video's original resolution if it's AppleTV-kosher.
		if [ $widthcrop -le 1280 ]
			then
			echo $origres
			exit 0
			fi
		if [ "$ratio" == "4by3" ]
			then
			echo 960x720
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo 1280x720
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo 1280x544
			exit 0
			fi
echo 1280x720
exit 0
fi

## Apple TV settings.
if [ "$optim" == "appletv30" ]
then
##Use the video's original resolution if it's AppleTV-kosher.
		if [ $widthcrop -le 960 ]
			then
			if [ $heightcrop -le 540 ]
				then
				echo $origres
				exit 0
				fi
			fi
		if [ "$ratio" == "4by3" ]
			then
			echo 720x540
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo 960x540
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo 960x408
			exit 0
			fi
echo 960x540
exit 0
fi


## DivX Home Theater settings.
if [ "$optim" == "divxhome" ]
then
##Use the video's original resolution if it's DivX-kosher.
		if [ $origpx -le 350000 ]
			then
			if [ $widthcrop -lt 720 ]
				then
					echo $origres
					exit 0
				fi
			fi
		if [ "$ratio" == "4by3" ]
			then
			echo 704x528
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo 720x400
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo 720x304
			exit 0
			fi
echo 640x480
exit 0
fi

## DivX Portable settings.
if [ "$optim" == "divxport" ]
then
##Use the video's original resolution if it's DivX-kosher.
		if [ $origpx -le 307200 ]
			then
			echo $origres
			exit 0
			fi
		if [ "$ratio" == "4by3" ]
			then
			echo 640x480
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo 640x368
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo 640x272
			exit 0
			fi
echo 640x480
exit 0
fi

## NTSC VCD settings.
if [ "$optim" == "ntscvcd" ]
then
		if [ "$ratio" == "4by3" ]
			then
			echo 352x240
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo "352x176 -padtop 32 -padbottom 32"
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo "352x144 -padtop 48 -padbottom 48"
			exit 0
			fi
		echo 352x240
		exit 0
fi

## PAL VCD settings.
if [ "$optim" == "palvcd" ]
then
		if [ "$ratio" == "4by3" ]
			then
			echo 352x288
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo "352x192 -padtop 48 -padbottom 48"
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo "352x160 -padtop 64 -padbottom 64"
			exit 0
			fi
		echo 352x240
		exit 0
fi

## PAL DV settings.
if [ "$optim" == "paldv" ]
then
		if [ "$ratio" == "4by3" ]
			then
			echo "720x576"
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
				if [ $tiny == "16by9" ]
					then
					echo "720x576 -aspect 16:9"
					exit 0
				else
					echo "720x448 -padtop 64 -padbottom 64"
					exit 0
				fi
			fi
		if [ "$ratio" == "wide" ]
			then
				if [ $tiny == "16by9" ]
					then
					echo "720x352 -padtop 112 -padbottom 112 -aspect 16:9"
					exit 0
				else
					echo "720x448 -padtop 64 -padbottom 64 -aspect 16:9"
					exit 0
				fi
			exit 0
			fi
		echo "720x576"
		exit 0
fi

## NTSC DV settings.
if [ "$optim" == "ntscdv" ]
then
		if [ "$ratio" == "4by3" ]
			then
				if [ $tiny == "16by9" ]
					then
					echo "720x480 -aspect 16:9"
					exit 0
				else
				echo "720x480 -aspect 3:2"
				exit 0
				fi
			fi
		if [ "$ratio" == "16by9" ]
			then
				if [ $tiny == "16by9" ]
					then
					echo "720x480 -aspect 16:9"
					exit 0
				else
					echo "720x352 -padtop 64 -padbottom 64"
					exit 0
				fi
			fi
		if [ "$ratio" == "wide" ]
			then
				if [ $tiny == "16by9" ]
					then
					echo "720x384 -padtop 48 -padbottom 48 -aspect 16:9"
					exit 0
				else
					echo "720x272 -padtop 96 -padbottom 112"
					exit 0
				fi
			fi
		echo "720x480"
		exit 0
fi

## PAL DVD settings.
if [ "$optim" == "paldvd" ]
then
		if [ "$ratio" == "4by3" ]
			then
			if [ $tiny == "16by9" ]
					then
						if [ "$forcewide" == "16by9" ]
						then
						echo "720x576 -aspect 16:9"
						exit 0
						fi
					echo "560x576 -padleft 80 -padright 80 " #-aspect 16:9
					exit 0
				else
					echo "720x576 -aspect 4:3"
					exit 0
				fi
			fi
		if [ "$ratio" == "16by9" ]
			then
				if [ $tiny == "16by9" ]
					then
					echo "720x576 -aspect 16:9"
					exit 0
				else
					echo "720x448 -padtop 64 -padbottom 64 -aspect 4:3"
					exit 0
				fi
			fi
		if [ "$ratio" == "wide" ]
			then
				if [ $tiny == "16by9" ]
					then
					echo "720x448 -padtop 64 -padbottom 64 -aspect 16:9"
					exit 0
				else
					echo "720x352 -padtop 112 -padbottom 112 -aspect 4:3"
					exit 0
				fi
			fi
		echo "720x576 -aspect 4:3"
		exit 0
fi

## NTSC DVD settings.
if [ "$optim" == "ntscdvd" ]
then
		if [ "$ratio" == "4by3" ]
			then
				if [ $tiny == "16by9" ]
					then
						if [ "$forcewide" == "16by9" ]
						then
						echo "720x480 -aspect 16:9"
						exit 0
						fi
					echo "560x480 -padleft 80 -padright 80 " #-aspect 16:9
					exit 0
				else
					echo "720x480 -aspect 4:3"
					exit 0
				fi
			fi
		if [ "$ratio" == "16by9" ]
			then
				if [ "$tiny" == "16by9" ]
					then
					echo "720x480 -aspect 16:9"
					exit 0
				else
					echo "720x352 -padtop 64 -padbottom 64 -aspect 4:3"
					exit 0
				fi
			fi
		if [ "$ratio" == "wide" ]
			then
				if [ "$tiny" == "16by9" ]
					then
					echo "720x384 -padtop 48 -padbottom 48 -aspect 16:9"
					exit 0
				else
					echo "720x272 -padtop 96 -padbottom 112 -aspect 4:3"
					exit 0
				fi
			fi
		echo "720x480 -aspect 4:3"
		exit 0
fi

## Small PAL DVD settings.
if [ "$optim" == "paldvdsmall" ]
then
		if [ "$ratio" == "4by3" ]
			then
			echo "352x576 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo "352x448 -padtop 64 -padbottom 64 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo "352x352 -padtop 112 -padbottom 112 -aspect 4:3"
			exit 0
			fi
		echo "352x576 -aspect 4:3"
		exit 0
fi

## Small NTSC DVD settings.
if [ "$optim" == "ntscdvdsmall" ]
then
		if [ "$ratio" == "4by3" ]
			then
			echo "352x480 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo "352x352 -padtop 64 -padbottom 64 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo "352x272 -padtop 96 -padbottom 112 -aspect 4:3"
			exit 0
			fi
		echo "352x480 -aspect 4:3"
		exit 0
fi


## PAL SVCD settings.
if [ "$optim" == "palsvcd" ]
then
		if [ "$ratio" == "4by3" ]
			then
			echo "480x576 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo "480x448 -padtop 64 -padbottom 64 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo "480x352 -padtop 112 -padbottom 112 -aspect 4:3"
			exit 0
			fi
		echo "480x576 -aspect 4:3"
		exit 0
fi

## NTSC SVCD settings.
if [ "$optim" == "ntscsvcd" ]
then
		if [ "$ratio" == "4by3" ]
			then
			echo "480x480 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo "480x352 -padtop 64 -padbottom 64 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo "480x272 -padtop 96 -padbottom 112 -aspect 4:3"
			exit 0
			fi
		echo "480x480 -aspect 4:3"
		exit 0
fi

## High TiVo settings.
if [ "$optim" == "tivohigh" ]
then
		if [ "$ratio" == "4by3" ]
			then
			echo "544x480 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
			echo "544x352 -padtop 64 -padbottom 64 -aspect 4:3"
			exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
			echo "544x272 -padtop 96 -padbottom 112 -aspect 4:3"
			exit 0
			fi
		echo "544x480 -aspect 4:3"
		exit 0
fi

## HD 720p settings.
if [ "$optim" == "720p" ]
then
		if [ "$ratio" == "4by3" ]
			then
					echo "960x720 -padleft 160 -padright 160 -aspect 16:9" #-aspect 16:9
					exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
					echo "1280x720 -aspect 16:9"
					exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
					echo "1280x544 -padtop 80 -padbottom 96 -aspect 16:9"
					exit 0
			fi
		echo "1280x720 -aspect 16:9"
		exit 0
fi

## HD 1080 settings.
if [ "$optim" == "1080" ]
then
		if [ "$ratio" == "4by3" ]
			then
					echo "1440x1080 -padleft 240 -padright 240  -aspect 16:9" #-aspect 16:9
					exit 0
			fi
		if [ "$ratio" == "16by9" ]
			then
					echo "1920x1080 -aspect 16:9"
					exit 0
			fi
		if [ "$ratio" == "wide" ]
			then
					echo "1920x816 -padtop 128 -padbottom 136 -aspect 16:9"
					exit 0
			fi
		echo "1920x1080 -aspect 16:9"
		exit 0
fi


## If really weird stuff happens, fallback to regular res.
echo $origres

exit 0
