#!/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.
optim=$1
tiny=$2
mpeg=$3
wide=$4

/usr/bin/ulimit -u 256

origwidth=`cat /tmp/visualhub_dur | grep fps | rev | awk -F x '{print $2}' | awk '{print $1}' | rev | head -1`
origheight=`cat /tmp/visualhub_dur | grep fps | rev | awk -F x '{print $1}' | rev | awk -F , '{print $1}' | head -1`
origres=`echo "$origwidth"x"$origheight"`

topcrop=`head -1 /tmp/visualhub_cropmarks`
bottomcrop=`head -2 /tmp/visualhub_cropmarks | tail -1`
leftcrop=`tail -2 /tmp/visualhub_cropmarks | head -1`
rightcrop=`tail -1 /tmp/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

if [ "$origres" == "960x720" ]
then
mpeg=1.77777
fi

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

widthcrop1=`echo $origwidth - $leftcrop - $rightcrop | bc`
widthcrop=`echo $widthcrop1 \* $stretchratio | bc`
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/visualhub_dvdwide`

#### Choose Your Destiny. ####
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


## 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
		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

## TV screen settings.
if [ "$optim" == "tv" ]
then
	if [ "$tiny" == "h264" ]
	then
		if [ "$origwidth" -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 [ $origpx -le 307200 ]
			then
			echo $origres
			exit 0
			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

## 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 [ $origwidth -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
			echo "720x480"
			exit 0
			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
