#!/bin/bash
# any2ogg.sh, written (mostly) by Joost Rohde
#             (originally a small script by Timo Jyrinki)
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

if [ $# -le 0 ]; then
  echo 
  echo $0 - Converts a whole directory of video files to Ogg format \(Theora/Vorbis\)
  echo 
  echo Usage: $0 [extension] [input_dir] [output_dir] [vid_qty] [aud_qty] [scale]
  echo
  echo \* input_dir: Path to directory containing the source files
  echo \* output_dir: Where to place the converted .ogg files
  echo \* vid_qty: Video quality, 0-10 \(default: 6\)
  echo \* aud_qty: Audio quality, 0-10 \(default: 3\)
  echo \* scale: Scale in x direction, 0-3000 \(default: keep scale\)
  echo
  echo Example: \"$0 mpg /input/path /output/path 5 2 176 \" converts all the files with the .mpeg-suffix
  echo 
  exit
fi

# Do the parameter checking

#parsing of $1
if [[ -z $1 ]]; then
  echo 
  echo Give at least the extension to check for! 
  echo When no path\'s are given the current path will be used with the default quality levels.
  exit
fi

# Parsing of $2
if [ "$2" ]; then
# Make sure the directory exists.
	declare -r DirIn="$2"
	if [ ! -d "$DirIn" ]; then
	  echo
	  ls -l "$DirIn"
	  echo "ERROR: "$DirIn" is not a valid directory!"
	  echo
	  exit $EXIT_FAILURE
	  echo
    	  else
	  input_dir="$2/"
	fi
else
input_dir="./"
fi

# Parsing of $3
if [ "$3" ]; then
# Make sure the directory exists.
	declare -r DirOut="$3"
	if [ ! -d "$DirOut" ]; then
	  echo
	  ls -l "$DirOut"
	  echo "ERROR: $DirOut is not a valid directory!"
	  echo
	  exit $EXIT_FAILURE
    	  echo
	  else
	  output_dir="$3/"
    	fi
fi

# Parsing of $4
if [ $4 ]; then
	numeric=1
	case "$4" in
	''|*[!0-9]*) numeric=0;;
	esac

	if [ $numeric -eq 0 ]; then
	  echo
	  echo Please use an integer between 0 and 10 for video quality. Thank you.
	  echo 
	  exit
	fi
	if [ \( $4 -lt 0 \) -o \( $4 -gt 10 \) ]; then
	  echo
	  echo Please use an integer between 0 and 10 for video quality. Thank you.
	  echo 
	  exit
	fi
fi

# Parsing of $5
if [ $5 ]; then
	numeric=1
	case "$5" in
	''|*[!0-9]*) numeric=0;;
	esac
	if [ $numeric -eq 0 ]; then
	  echo
	  echo Please use an integer between 0 and 10 for audio quality. Thank you.
	  echo
	  exit
	fi
	if [ \( $5 -lt 0 \) -o \( $5 -gt 10 \) ]; then
	  echo
	  echo Please use an integer between 0 and 10 for audio quality. Thank you.
	  echo
	  exit
	fi
fi

# Options
extension=$1

if [ $4 ]; then
	videoq=$4
	else
	videoq=6
fi

if [ $5 ]; then
	audioq=$5
	else
	audioq=3
fi

# Batch conversion
echo \# Converting .${extension}-files to Ogg Theora / Ogg Vorbis \(.ogg\)
IFS=$'\n'
for file in $(find $input_dir -name "*.${extension}" -printf '%p\n'); do
  	output=`basename "${file}" .${extension}`.ogg
	if [ -z "$3" ]; then
	  output_dir=`dirname "${file}"`/
	fi
	  echo ./2ogg.sh "${file}" "$output_dir""${output}" "${videoq}" "${audioq}" "$6"
   	  ./2ogg.sh "${file}" "$output_dir""${output}" "${videoq}" "${audioq}" "$6"
done 
