#!/usr/bin/env bash

# This file contains common functions used in
# several files.

# Read in language files
source ${BBROOTDIR}/lang/${BBLANG}/BashBurn.lang
source ${BBROOTDIR}/lang/${BBLANG}/commonfunctions.lang

# Checks if argument is zero characters
check_length()
{
	NULLSTRING=0
	NONNULL=1
        TESTSTRING=$1				# Set TESTSTRING to argument
        if [[ ${#TESTSTRING} = 0 ]]; then	# Is TESTSTRING empty?
                return $NULLSTRING		# Yes, return 0
        else
                return $NONNULL			# No, return 1
        fi	
}

# Wait for enter to be pressed
wait_for_enter()
{
	 while true; do
	 	echo $bb_press_enter
		read temp
		if [[ "$temp" = "" ]]; then
			break
		else
			continue
		fi
	done
}

# Function to check whether CD is blank or not
check_cd_status()
{
	  if eval 'dd if=${BBCDWRITER} of=/dev/null bs=1 count=1 &> /dev/null' ; then
	  		BB_CDSTATUS="USED"
	  else
	  		BB_CDSTATUS="BLANK"
	  fi
}

# Function to blank CD
blank_cd()
{
	  echo $bb_cdrw_blank1
	  if eval "${BB_CDBURNCMD} -v dev=${BBCDWRITER} blank=${BBBLANKING} speed=${BBSPEED}" ;then
	  		echo $bb_cdrw_blank2
			wait_for_enter
	  else
	  		# Forced blanking
			echo $bb_cdrw_blank3
			if eval "${BB_CDBURNCMD} -v dev=${BBCDWRITER} blank=${BBBLANKING} speed=${BBSPEED} -force"; then
				echo $bb_cdrw_blank4
				wait_for_enter
			else
				echo $bb_cdrw_blank5
				blank_failed="TRUE"
				wait_for_enter
			fi
	  fi
}

ask_for_blanking()
{
	  if [ ${BB_CDSTATUS} == "USED" ]; then
	  		echo $bb_cf_text1
			echo $bb_cf_text2
			echo $bb_cf_text3
			echo -n "(yes/no/abort) |> "
			read choice
			if [ ${choice} == "yes" ]; then
				blank_cd
			elif [ ${choice} == "abort" ]; then
				echo $bb_cf_text4
				wait_for_enter
				exit
			else
				echo $bb_cf_text5
				sleep 3s
			fi
			if [ ${blank_failed} == "TRUE" ]; then
				echo $bb_cf_text6
				wait_for_enter
				exit
			fi
	  fi
}

