#! /bin/bash

#################################################################################
#	ripdvd - A very simple console based dvd ripper				#
#	Copyright (C) 2006  Wedge						#
#										#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA#
#################################################################################


VERSION=2.0.5
AUTHOR="Wedge"
DATE="2006,2007,2008"

PREFIX="/usr/local"
CONFIG_DIR="${HOME}/.ripdvd"
CONFIG_FILE="${CONFIG_DIR}/config"
LOCALE_DIR="${PREFIX}/share/locale"
LIB_DIR="${PREFIX}/lib/ripdvd"

shopt -u expand_aliases
echo -en "\\033[0;37;40m"
clear
echo "~= Ripdvd v${VERSION} =~"
LANG_VAR=${LC_ALL:-$LANG}
case $LANG_VAR in
	fr*) LANG_FILE="${LOCALE_DIR}/fr/ripdvd.lng" ;;
	en*) LANG_FILE="${LOCALE_DIR}/en/ripdvd.lng" ;;
	*) echo "Your language is not supported yet, falling back to English"; LANG_FILE="${LOCALE_DIR}/en/ripdvd.lng" ;;
esac
source "${LANG_FILE}"


# Trap signals
trap 'user_interrupt' SIGHUP SIGTSTP SIGINT SIGQUIT SIGKILL SIGABRT SIGFPE SIGSEGV SIGTERM

# Load lib files
source "${CONFIG_FILE}" 2> /dev/null
for LIB in $(ls "${LIB_DIR}"); do
	source "${LIB_DIR}/${LIB}"
done

# Hmm, seems we've been launched without parameters ...
if [ $# -eq 0 ]; then
	usage
fi

# Check configuration file presence
# If not, we may have just been installed, so we check dependencies before configuration
# If yes, we update conf, just in case ...
test_deps
source "${CONFIG_FILE}"
if [ ! -f "${CONFIG_FILE}" ]; then
	set_conf
else
	update_conf
fi
source "${CONFIG_FILE}"

# Check configuration file version against program version
# If not the same, reconfigure
VER_CONF=${VER_CONF:-"0.0.0"}
if [ $(echo ${VERSION} | sed 's@\.@@g') -ne $(echo ${VER_CONF} | sed 's@\.@@g') ]; then
	set_conf
	source "${CONFIG_FILE}"
fi
if [ -f "${HOME}/.ripdvdrc" ]; then
	rm -f "${HOME}/.ripdvdrc"
fi

# Use getopt to parse parameters is better
GETOPTS=$(getopt -o chrstv -l \
	'aspect:, \
	abtr:, \
	acodec:, \
	aid:, \
	alang:, \
	autocrop, \
	automatic, \
	cartoon, \
	chapter:, \
	crop:, \
	deinterlace, \
	dest-dir:, \
	dvd-dev:, \
	nice:, \
	no-eject, \
	no-preview, \
	print-cmd, \
	re-read, \
	scan, \
	second-audio:, \
	size:, \
	skip:, \
	skip-one, \
	split:, \
	slang:, \
	sid:, \
	sfile, \
	title:, \
	track:, \
	two-pass, \
	vbtr:, \
	vcodec:, \
	width:, \
	configure, \
	help, \
	version, \
	test' -n 'ripdvd' -- "$@")
if [ $? != 0 ]; then
	echo
	usage
	exit 0
fi
eval set -- "${GETOPTS}"

# Parse parameters
while [ $# -gt 0 ]; do
	case "$1" in
		--abtr) AUDIO_RATE="$2"; shift 2 ;;
		--acodec) ACODEC="$2"; shift 2 ;;
		--aid) AUD_ID="$2"; shift 2 ;;
		--alang) LANG_AUD="$2"; shift 2 ;;
		--aspect) ASPECT="$2"; shift 2 ;;
		--autocrop) CROP="yes"; AUTOCROP="yes"; shift ;;
		--automatic) AUTOMATIC="yes"; shift ;;
		--cartoon) CARTOON="yes"; shift ;;
		--chapter) parse_chapter_opt "$2"; shift 2 ;;
		--crop) CROP="yes"; CROP_VAL="$2"; shift 2 ;;
		--deinterlace) use_deinterlace; shift ;;
		--dest-dir) DEST_DIR="$2"; create_dest_dir; shift 2 ;;
		--dvd-dev) DVD_DEV="$2"; check_dvd_dev; shift 2 ;;
		--nice) NICE="$2"; shift 2 ;;
		--no-eject) EJECT="no"; shift ;;
		--no-preview) PREVIEW="no"; shift ;;
		--print-cmd) PRINT_CMD="yes"; shift ;;
		--second-audio) SECOND_AUDIO_ID="$2"; shift 2 ;;
		--sfile) SUB_OUT_FILE="yes"; shift ;;
		--sid) SUB_ID="$2"; SUB_ID=$[$SUB_ID-1]; shift 2 ;;
		--size) SIZE_MOVIE="$2"; shift 2 ;;
		--skip) SKIP_TIME="$1"; shift 2 ;;
		--skip-one) SKIP_TIME=65; shift ;;
		--slang) LANG_SUB="$2"; shift 2 ;;
		--split) SPLITS="$2"; shift 2 ;;
		--title) NAME_MOVIE="$2"; shift 2 ;;
		--track) DVD_TRACK="$2"; shift 2 ;;
		--two-pass) TWO_PASS="yes"; shift ;;
		--vbtr) BTR="$2"; shift 2 ;;
		--vcodec) VCODEC="$2"; shift 2 ;;
		--width) USER_WIDTH="$2"; shift 2 ;;
		-r|--re-read) READ_INFO="yes"; shift ;;
		-c|--configure) clean_conf; set_conf; end_app ;;
		-h|--help) usage; echo -en "\\033[0;37;0m"; exit 0 ;;
		-s|--scan) SCAN="yes"; shift ;;
		-t|--test) test_deps; echo -en "\\033[0;37;0m"; exit 0 ;;
		-v|--version) version; echo -en "\\033[0;37;0m"; exit 0 ;;
		--) shift ; break ;;
		*) usage; echo -en "\\033[0;37;0m"; exit 0 ;;
	esac
done

if [ "${SCAN}x" != "x" ]; then
	unlock_dvd
	extract_dvd_infos
	print_dvd_infos
	end_app
fi

if [ "${PRINT_CMD}x" != "x" ]; then
	print_cmd
fi

if [ "${AUTOMATIC}x" != "x" ]; then
	automatic
fi

NICE=${NICE:-0}

unlock_dvd
# Extract DVD infos
extract_dvd_infos
# Retreiving of longest track
set_video_track
# Check for missing parameters
check_parameters
# Checking for space left on device
check_free_space
# Retreiving of movie's track length
set_dvd_length
# Chapter mode
if [ "${CHAPTER_OPTS}x" != "x" ]; then
	calc_time_chap
	DVD_LEN_SEC=${TOTAL_CHAP_TIME:-${DVD_LEN_SEC}}
	DVD_LEN=$(seconds_to_time ${DVD_LEN_SEC})
fi
# Autodetecting original resolution
original_size_autodetect

# (Auto)Crop if needed
if [ "${CROP}x" = "yesx" ]; then
	if [ "${AUTOCROP}x" = "yesx" ]; then
		guess_crop_values
	fi
	set_crop_opts
fi

# Retreiving of subtitles id if any subtitles language is provided
if [ "${LANG_SUB}x" != "x" ]; then
	set_sub_id
fi
if [ "${SUB_ID}x" != "x" ]; then
	set_sub_opts
fi

AUDIO_RATE=${AUDIO_RATE:-128}
if [ "${LANG_AUD}x" != "x" ]; then
	set_audio_id
else 
	calc_audio_id_from_format ${DVD_LON_TRACK} ${AUD_ID}
	set_aud_opts
fi

# Set aspect option
if [ "${ASPECT}x" != "x" ]; then
	set_aspect_opts
fi

if [ "${SECOND_AUDIO_ID}x" != "x" ]; then
	dump_audio_track ${SECOND_AUDIO_ID}
fi

# Calculation of bitrates
set_aud_opts
VIDEO_BITRATE=${BTR:-${VIDEO_BITRATE}}
if [ "${BTR}x" != "x" ]; then
	calc_movie_size
else
	calc_video_bitrate
fi
set_vid_opts

# Some useful informations
print_header
# Run preview if needed 
if [ "${PREVIEW}x" = "x" ]; then
	preview
else
	encode
fi

