#!/bin/bash

indir="$1"
outdir="$2"
for movie in "$indir"/*; do 
	case "$movie" in
		*"_DISC1_")
			#merge all corresponding discs into one movie
			echo "$(basename "$movie") is first of many discs and/or has extras discs"
			movie_name="$(basename "$movie")"
			movie_name=${movie_name%"_DISC1_"} # sure we could do it in one step with indirection but like this it's more clear
			main_features=()
			# we are assuming that you will never a movie composed of more than 9 discs. if that were the case, the following would not work as _DISC10_ would come before _DISC2_ in sort order (because alphabetical sort is used by bash) and then your movie would be merged in the wrong order
			for part in "$(dirname "$movie")/${movie_name}"_DISC[1-9]_; do
				# get the main feature of the disc
				longest_title=""
				max_duration="00:00:00"
				for title in "$part"/*; do
					duration=$(mkvinfo "$title" | grep "| + Duration: " | cut -d ' ' -f4)
					if [[ "$duration" > "$max_duration" ]]; then
						max_duration="$duration"
						longest_title="$title"
					fi
				done
				
				# for now we consider the main feature to be the longest title of the disc. I don't think this is in fact accurate (see how dvdbackup determines main feature), but for now it works
				main_feature="$longest_title"
				echo "main feature for part $(basename "$part") is $(basename "$longest_title")"
				main_features+=("$main_feature")
			done
			mkdir -p "$outdir/$movie_name"
			# merge main features
			if [[ ${#main_features[@]} == 1 ]]; then
				cp "${main_features[0]}" "$outdir/$movie_name/$movie_name.mkv"
			else
				mkvmerge -o "$outdir/$movie_name/$movie_name.mkv" '[' "${main_features[@]}" ']'
			fi
			rm "${main_features[@]}"
			# all the remaining titles are extras
			mkdir -p "$outdir/$movie_name/Extras"
			mv --backup=numbered "$(dirname "$movie")/${movie_name}"_DISC[1-9]_/* "$outdir/$movie_name/Extras/" #--backup=numbered because multiple discs may have titles with the same number. you could also make a separate dir for each disc
			rmdir "$(dirname "$movie")/${movie_name}"_DISC[1-9]_
			;;
		*"_DISC"[[:digit:]]"_")
			#we should skip this one, because we handle it in the first case
			: #this means noop
			;;
		*"_MULTIMOVIE_")
			#we should skip this one, because it has to be done manually
			: #this means noop
			;;
		*"_EXTRAS_"*)
			movie_name="$(basename "$movie")"
			movie_name=${movie_name%"_EXTRAS_"*} # sure we could do it in one step with indirection but like this it's more clear
			extra_name="$(basename "$movie")"
			extra_name=${extra_name#"${movie_name}_EXTRAS_"} # sure we could do it in one step with indirection but like this it's more clear
			echo "$extra_name is an extra for $movie_name"

			longest_title=""
			max_duration="00:00:00"
			for title in "$movie"/*; do
				duration=$(mkvinfo "$title" | grep "| + Duration: " | cut -d ' ' -f4)
				if [[ "$duration" > "$max_duration" ]]; then
					max_duration="$duration"
					longest_title="$title"
				fi
			done
			
			# for now we consider the main feature to be the longest title of the disc. I don't think this is in fact accurate (see how dvdbackup determines main feature), but for now it works
			main_feature="$longest_title"
			mkdir -p "$outdir/$movie_name/Extras/$extra_name/Extras"
			mv "$main_feature" "$outdir/$movie_name/Extras/$extra_name/$extra_name.mkv"
			mv "$movie"/* "$outdir/$movie_name/Extras/$extra_name/Extras"
			rmdir "$movie"
			;;
		*)
			echo "$(basename "$movie") is a normal singledisc"

			longest_title=""
			max_duration="00:00:00"
			for title in "$movie"/*; do
				duration=$(mkvinfo "$title" | grep "| + Duration: " | cut -d ' ' -f4)
				if [[ "$duration" > "$max_duration" ]]; then
					max_duration="$duration"
					longest_title="$title"
				fi
			done
			
			# for now we consider the main feature to be the longest title of the disc. I don't think this is in fact accurate (see how dvdbackup determines main feature), but for now it works
			main_feature="$longest_title"
			echo "main feature for movie $(basename "$movie") is $(basename "$longest_title")"

			mkdir -p "$outdir/$(basename "$movie")"
			mv "$main_feature" "$outdir/$(basename "$movie")/$(basename "$movie").mkv"
			mkdir -p "$outdir/$(basename "$movie")/Extras"
			mv "$movie"/* "$outdir/$(basename "$movie")/Extras/"
			rmdir "$movie"
			;;
	esac
done
