#!/bin/bash

# Parameters
FOLDER="/mnt/media/TV"
MIN_RATE=275000
MIN_SIZE="425M"
MIN_HEIGHT=500
NAME_REGEX="\.(mov|avi|mkv|mp4|ts)$"

# Command-line overrides
if [ -n "${1}" ]; then
	FOLDER="${1}"
fi
if [ -n "${2}" ]; then
	MIN_SIZE="${2}"
fi
if [ -n "${3}" ]; then
	MIN_RATE="${3}"
fi
if [ -n "${4}" ]; then
	MIN_HEIGHT="${4}"
fi
if [ -n "${5}" ]; then
	NAME_REGEX="${5}"
fi

# Name-based paramters
ZERO=0
if basename "${0}" | grep -q '0'; then
	ZERO=1
fi

# Sanity check
if [ ! -d "${FOLDER}" ]; then
	echo "Usage: `basename "${0}"` [folder] [min_size] [min_rate] [min_height] [name_regex]" 1>&2
	exit 1
fi

# Find large video files
# Later tests exclude Handbrake-encoded files
FILES="`find "${FOLDER}" -maxdepth 1 -size "+${MIN_SIZE}" | grep -iE "${NAME_REGEX}"`"

# Loop with newline-as-IFS
OLDIFS="${IFS}"
IFS=$'\n'
for i in ${FILES}; do

	# Skip invalid files
	if [ -z "${i}" ] || [ ! -r "${i}" ]; then
		continue
	fi

	# We do not want to recode videos that we already encoded
	HANDBRAKE=0
	STRINGS="`head -c 10000000 "${i}" | strings -n 10`"
	if echo "${STRINGS}" | grep -q HandBrake; then
		HANDBRAKE=1
	elif echo "${STRINGS}" | grep -E '^x264 - core (79|112)' | grep -E 'threads=(12|24)' | grep -Eq 'crf=2[05].0'; then
		HANDBRAKE=1
	fi
	if [ $HANDBRAKE -eq 1 ]; then
		continue
	fi

	# We only care about "big" videos
	# Movies with no known height should be left alone
	# Movies with a "0" height can be assumed to be tall enough
	HEIGHT="`~/bin/video/movInfo.pl "${i}" VIDEO_HEIGHT 2>/dev/null`"
	if [ -z "${HEIGHT}" ]; then
		HEIGHT=1
	fi
	if [ $HEIGHT -eq 0 ]; then
		HEIGHT=$MIN_HEIGHT
	fi
	if [ $HEIGHT -lt $MIN_HEIGHT ]; then
		continue
	fi

	# Skip files is low bitrates
	SIZE="`stat -f %z "${i}" 2>/dev/null`"
	if [ -z "${SIZE}" ]; then
		SIZE=1
	fi
	LENGTH="`~/bin/video/movInfo.pl "${i}" LENGTH 2>/dev/null | sed 's%\..*$%%'`"
	if [ -z "${LENGTH}" ] || [ $LENGTH -eq 0 ]; then
		LENGTH=1
	fi
	RATE=$(( $SIZE / $LENGTH ))
	if [ $RATE -le $MIN_RATE ]; then
		continue
	fi


	# If we're still around, the file matched all criteria
	if [ $ZERO -gt 0 ]; then
		echo -en "${i}\0"
	else
		echo "${i}"
	fi
done
IFS="${OLDIFS}"
