#!/usr/bin/env bash

# This file contains the functionality for multisession burning

burn_function()
{
	  if [ "$BBMULTI" = "-multi" ]; then
		OLD_BB_CDBURNCMD=$BB_CDBURNCMD
		BB_CDBURNCMD="$OLD_BB_CDBURNCMD $BBMULTI"
	  fi
	  # Burn the created ISO-file
	  ${BB_CDBURNCMD} dev="$BBCDWRITER" speed="$BBSPEED" ${BBPADDING} ${BBOPT_ONE:+"driveropts=$BBOPT_ONE"} -eject -v "$BBBURNDIR"/*.[Ii][Ss][Oo]
	  echo $bb_multi_burn_5
}

burn_multi()
{
	#Does an ISO-file exist?
  	if [[ "$(find ${BBBURNDIR} -iname '*[Ii][Ss][Oo]')" != "" ]]; then
		# Yes it did
		echo $bb_multi_burn_1
		echo $bb_multi_burn_2
		echo $bb_multi_burn_3
		echo $bb_multi_burn_4
		burn_function
		wait_for_enter
	else
		# An ISO did not exist, we attempt to create one
		echo $bb_multi_burn_6
		if [ $BBGET_PREV_SESSION = "0" ];then
		# First session, no need to get -msinfo data
			echo; echo $bb_multi_burn_13
			sleep 2s
		else
			# Save old mkisofs command
			OLD_BB_ISOCMD=$BB_ISOCMD
			BBMSINFODATA=$($BB_CDBURNCMD dev=$BBCDWRITER -msinfo)
			# echo "BBMSINFODATA: $BBMSINFODATA"
			BB_ISOCMD="$OLD_BB_ISOCMD -C \"$BBMSINFODATA\" -M $BBCDWRITER"
		fi
	  
		echo; echo $bb_multi_burn_14
		#### i want a question regarding the BBLABEL ####
		if [ "$BBLABEL" = "<ask-me>" ]; then
	  		read -p "$bb_multi_burn_14b" BBLABEL
		fi
		# Create the ISO
		if eval "$BB_ISOCMD -r -f -v -J -hide-joliet-trans-tbl -copyright \"$BBCOPYRIGHT\" -A \"$BBDESCRIPTION\" -p \"$BBAUTHOR\" -publisher \"$BBPUBLISHER\" -volset \"$BBNAMEOFPACKAGE\" -V \"$BBLABEL\" -o \"$BBBURNDIR\"/BashBurn.iso \"$BBBURNDIR\""; then 
			echo $bb_multi_burn_15
			burn_function
			# Restore old mkisofs command
			if [ "$BB_ISOCMD" != "$OLD_BB_ISOCMD" ]; then
				BB_ISOCMD="$OLD_BB_ISOCMD"
			fi
			# Restore old cdrecord command
			if [ "$BB_CDBURNCMD" != "$OLD_BB_CDBURNCMD" ]; then
				BB_CDBURNCMD="$OLD_BB_CDBURNCMD"
			fi
			wait_for_enter
		else
			# Something went wrong. CD isn't burnt.
			echo $bb_multi_burn_16
			echo "$bb_multi_burn_17 ${BBBURNDIR}"
			echo $bb_multi_burn_18
			wait_for_enter
		fi
	fi
}

