#!/usr/bin/env bash

# This file contains functionality for ISO-file handling

# Check whether ISO files exist
check_for_iso()
{
	  cd ${BBBURNDIR}
	  if [ $(find ${BBBURNDIR} -iname "*.[Ii][Ss][Oo]" | wc -l) -gt 0 ]; then	
	  	existing="yes"
	  fi
}

create_iso_from_dir()
{
	  if [ $(ls ${BBBURNDIR} | wc -l) -eq 0 ]; then
	  	echo "$bb_im_error_files $BBBURNDIR"
		wait_for_enter
	  else
		# Does an ISO file exist?
		check_for_iso
		if [ "$existing" != "yes" ]; then
			# Creating ISO from files
			echo; echo $bb_im_ch2_4
			#### i want a question regarding the BBLABEL ####
			if [ "$BBLABEL" = "<ask-me>" ]; then
				read -p "$bb_im_ch2_4b" BBLABEL
			fi
			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_im_ch2_5
				wait_for_enter
				continue
			else		#Some error occured
				echo $bb_im_ch2_6
				echo $bb_im_ch2_7
				wait_for_enter
				break
			fi
		else
			echo "$bb_im_ch2_1${BBBURNDIR}.$bb_im_ch2_2"
			echo $bb_im_ch2_3
			wait_for_enter
		fi
	  fi
}

create_iso_from_cd()
{
	# Does an ISO file exist?
	check_for_iso
	if [ "$existing" != "yes" ]; then
		echo "$bb_im_ch3_2${BBCDROM}"
		# Creating ISO using readcd      
		if eval "${BB_READCD} ${BB_READCD_OPTS} dev=${BBCDROM} f=${BBBURNDIR}/BashBurn.iso"; then
			echo $bb_im_ch2_5
			wait_for_enter
		else
			echo $bb_im_ch2_6
			echo $bb_im_ch2_7
			wait_for_enter
			break
		fi
	else
		echo "$bb_im_ch2_1${BBBURNDIR}.$bb_im_ch2_2"
		echo $bb_im_ch2_3
		wait_for_enter
	fi
}

mount_in_loopback()
{
	  echo "$bb_im_ch5_1"
	  echo "$bb_im_ch5_2"
	  echo -n "|> "
	  read bb_image_path
	  if [ "$bb_image_path" = "" ]; then
  		echo "$bb_im_ch5_3"
		wait_for_enter
	  else
  		"${BBROOTDIR}"/misc/loopback.sh "$bb_image_path"
		wait_for_enter
	  fi
}

