#! /bin/bash
# The top line might need to be changed if your bash executable is located in another directory

export  BBDECOLINE="+----------------+"
export  BBVERSION="BashBurn 1.5.2"  # Version of BashBurn - don't change it!
export  BBCONFFILE=/etc/bashburnrc        # Name of BashBurns' config file. Don't change!


# Detect signals as 'CTRL+C', INIT, KILL, call to function force_quit, show BashBurn info and quit.
trap 'force_quit' 1 2 3 15 

force_quit()
{
	echo
	echo -e "${BBMAINCOLOR}$bb_quit1${BBHEADCOLOR}${BBVERSION}"
	echo -e "${BBMAINCOLOR}$bb_quit2${BBSUBCOLOR}$bb_quit3${BBMAINCOLOR}$bb_quit4${BBCOLOROFF}"
	sleep 2s
	break
}

# The wait function used below
wait_for_enter()
{
while true; do
        echo $bb_press_enter
        read temp
        if [[ "$temp" = "" ]]; then
                break
        else
                continue
        fi
done
}


# Here starts the reading in of variables
variable_reader()
{
	export BBISCONF="$( cat ${BBCONFFILE} | grep -v '^#' | grep BBISCONF:  | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"

	export BBROOTDIR="$( cat ${BBCONFFILE} | grep -v '^#' | grep BBROOTDIR:  | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
	export BBROOTDIR="${BBROOTDIR%*/}"
 
	export BBCDROM="$(   cat ${BBCONFFILE} | grep -v '^#' | grep BBCDROM:    | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
	export BBCDMNT="$(   cat ${BBCONFFILE} | grep -v '^#' | grep BBCDMNT:    | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
        export BBCDWRITER="$(cat ${BBCONFFILE} | grep -v '^#' | grep BBCDWRITER: | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
        export BBSPEED="$(   cat ${BBCONFFILE} | grep -v '^#' | grep BBSPEED:    | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
        export BBBLANKING="$(cat ${BBCONFFILE} | grep -v '^#' | grep BBBLANKING: | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
        export BBNUMDEV="$(  cat ${BBCONFFILE} | grep -v '^#' | grep BBNUMDEV:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
	export BBBURNDIR="$( cat ${BBCONFFILE} | grep -v '^#' | grep BBBURNDIR:  | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
	export BBBURNDIR="${BBBURNDIR%*/}"


	# additional informations of a CD:
	export BBLABEL="$( cat ${BBCONFFILE}	| grep -v '^#' 	| grep BBLABEL:	| cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBCOPYRIGHT="$( cat ${BBCONFFILE} 	| grep -v '^#' 	| grep BBCOPYRIGHT:  	| cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBDESCRIPTION="$( cat ${BBCONFFILE} 	| grep -v '^#' 	| grep BBDESCRIPTION:  	| cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBAUTHOR="$( cat ${BBCONFFILE} 	| grep -v '^#'	| grep BBAUTHOR:  	| cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBPUBLISHER="$( cat ${BBCONFFILE} 	| grep -v '^#'	| grep BBPUBLISHER:	| cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBNAMEOFPACKAGE="$( cat ${BBCONFFILE} 	| grep -v '^#'| grep BBNAMEOFPACKAGE:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"

	# some advanced options:
	export BBNORMALIZE="$( cat ${BBCONFFILE} 	| grep -v '^#'| grep BBNORMALIZE:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBOPT_ONE="$( cat ${BBCONFFILE} 	| grep -v '^#'| grep BBOPT_ONE:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBFIFODIR="$( cat ${BBCONFFILE} 	| grep -v '^#'| grep BBFIFODIR:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBDELTEMPBURN="$( cat ${BBCONFFILE} 	| grep -v '^#'| grep BBDELTEMPBURN:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBOVERBURN="$( cat ${BBCONFFILE} 	| grep -v '^#'| grep BBOVERBURN:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBCOPY_PROTECT="$( cat ${BBCONFFILE}     | grep -v '^#'| grep BBCOPY_PROTECT:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBBITRATE="$( cat ${BBCONFFILE}     | grep -v '^#'| grep BBBITRATE:   | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBLANG="$(   cat $BBCONFFILE     | grep -v '^#'| grep BBLANG:	| cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
	export BBDTAO="$(   cat $BBCONFFILE     | grep -v '^#'| grep BBDTAO:    | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]*//g")"
}

if [[ -r "${BBCONFFILE}" ]]; then	# If /etc/bashburnrc exists, read in the vars.
	variable_reader
	export BBTEMPMOUNTDIR="${BBBURNDIR}/mnt"
else
	echo "There is no '${BBCONFFILE}'" 1>&2		
	echo "That is the file where BashBurn stores its settings."
	echo "You must run the BashBurn installation script first as root to install"
	echo "all needed files. Do that now and then start 'bashburn' again."
	exit 1
fi


# Read in the language file
source ${BBROOTDIR}/lang/${BBLANG}/BashBurn.lang

#Check if $BBBURNDIR exists, and if you have write perms.

if [[ ! -e "${BBBURNDIR}" ]]; then                    #If temporary directory does not exist, create it  
       	echo
	echo $bb_no_temp_dir 
	if mkdir "${BBBURNDIR}"; then		# An user has usually write permission for the /tmp-dir.
		echo "'$BBBURNDIR' $bb_text_1"
       		echo $bb_text_2
		sleep 3s
	else
		echo "$bb_text_3 '$BBBURNDIR'" 
		echo $bb_text_4
		wait_for_enter
	fi
fi

if [[ ! -e "$BBROOTDIR" ]]; then
	echo "$bb_text_5 ($BBROOTDIR) $bb_text_6 ('${BBCONFFILE}')" 
	exit 1
fi

# Is BashBurn ever configured?
if [[ ${BBISCONF} = 0 ]]; then
	echo; echo $bb_conf_check1 
	echo $bb_conf_check2; echo
	wait_for_enter
fi

####PROGRAM START####
while true; do
	# <colors>
	BBCOLOROFF="\033[1;0m"	# standard color
	BBHEADCOLOR="\033[1;32m"	# green
	BBMAINCOLOR="\033[1;37m"	# white
	BBSUBCOLOR="\033[1;36m"	# blue
	BBTABLECOLOR="\033[1;31m"	# red
	BBINPUTCOLOR="\033[1;35m"	# lilac
	# </colors>
	setterm -clear
	# <table>
	echo -e "${BBTABLECOLOR}${BBDECOLINE}"
	echo -e "${BBTABLECOLOR}|${BBHEADCOLOR} ${BBVERSION} ${BBTABLECOLOR}|"
	echo -e "${BBTABLECOLOR}${BBDECOLINE}"
	echo -e "${BBTABLECOLOR}|" 
	echo -e "${BBTABLECOLOR}|-(${BBSUBCOLOR}MAIN${BBTABLECOLOR})"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 1) $bb_menu_1"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 2) $bb_menu_2"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 3) $bb_menu_3"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 4) $bb_menu_4"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 5) $bb_menu_5"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 6) $bb_menu_6"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 7) $bb_menu_7"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 8) $bb_menu_8 ${BBBURNDIR}"
        echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 9) $bb_menu_9 ${BBBURNDIR}"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 10) $bb_menu_10"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 11) $bb_menu_11"
	echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 0) $bb_menu_0"
	echo -e "${BBTABLECOLOR}|"
	echo -n -e "${BBINPUTCOLOR}$bb_menu_input[0-11] |>${BBMAINCOLOR} "
	read action
	echo -e "$BBCOLOROFF"
	# </table>
	
	case $action in
  		1)	# If you chose audio, do:
			variable_reader
			${BBROOTDIR}/menus/audio_menu.sh 	      	# The audio menu
			continue
		;;
		2) 	# If you chose data, do:
                        variable_reader
			${BBROOTDIR}/menus/data_menu.sh			# The data menu
			continue
  		;;
		3)	#If you chose ISO, do:
			${BBROOTDIR}/menus/iso_menu.sh			# The ISO menu 
  			continue
		;;
		4)
			#Burn an bin/cue file:
			${BBROOTDIR}/burning/bincue.sh
		;;
  		5)	#Multisession - Alrighty then...
			${BBROOTDIR}/burning/multi.sh			# The multisession menu
			continue
		;;
		6)	#If you chose to blank CDRW:
			echo $bb_cdrw_blank1
			if cdrecord -v dev=${BBCDWRITER} blank=${BBBLANKING} speed=${BBSPEED} ;then	#Blanking CDRW
				echo $bb_cdrw_blank2
				wait_for_enter				# Wait for confirmation
			else
				echo $bb_cdrw_blank3
				if cdrecord -v dev=${BBCDWRITER} blank=${BBBLANKING} speed=${BBSPEED} -force 1>/dev/null; then	#Forced blanking
					echo $bb_cdrw_blank4
					wait_for_enter
				else
					echo $bb_cdrw_blank5
					wait_for_enter
				fi
			fi
			sleep 2s				# wait two seconds
			continue 			    	# Back to menu
  		;;
  		7)	# If you chose to configure BashBurn:
			variable_reader				# Read all variables again. Just to make sure...
			${BBROOTDIR}/config/configure.sh			# Configure menu
			variable_reader				# Read in vars again
			continue
		;;
		8)
			echo $bb_burndir_view ${BBBURNDIR}; echo	
			ls -lhgGL --color ${BBBURNDIR}		# View files in the BBBURNDIR
			wait_for_enter

		;;
  		9)	# to clear/remove files in the temporary burn-dir (e.g. /tmp/burn)
			echo $bb_burndir_view ${BBBURNDIR}; echo
			ls -lhgGL --color ${BBBURNDIR}
			while true; do
				echo "$bb_burndir_del1 ${BBBURNDIR}? $bb_burndir_del1b"
				echo -n "|> "
				read temp
				if [ $temp = "y" ]; then
					rm -rf ${BBBURNDIR}/*
					echo "${BBBURNDIR} $bb_burndir_del2"
					sleep 2s
					break
				else
					echo $bb_burndir_del3
					sleep 2s
					break
				fi
			done
  		;;
		10)	# Mount/unmount a cd
			${BBROOTDIR}/misc/mount.sh
			continue
		;;
		11)	# Check program's paths of burning, ripped, codecs, etc.
			${BBROOTDIR}/misc/check_path.sh
			continue
		;;
 		0)	# Traitor! You quit BashBurn!
			force_quit
  		;;
  		*)	# You actually chose wrong? ...hmmm...
			echo "$bb_exit_error[0-11]"  
			sleep 1s
			continue				#Else go to menu
  		;;
	esac
done
