#!/usr/bin/env bash

# Decoline must be two characters longer than version
# or it will look funny.
export BBDECOLINE="+--------------+"
# Version number (should not contain whitespaces at beginning or end [petsound]).
export BBVERSION="BashBurn 2.1"

# Bashburn configuration file:
export HOMEDIR=$HOME # User identification
export BBCONFFILE="/usr/local/etc/bashburnrc" # Default config file, used if isn't config file in user dir 
export BBCONFFILEDEF="/usr/local/etc/bashburnrc"

if [[ -r "$HOMEDIR/.bashburnrc" ]]; then 
	export BBCONFFILE="$HOMEDIR/.bashburnrc"
fi

# 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
}

# This is run at first
init_bashburn()
{
	if [[ ! -r "${BBCONFFILE}" ]]; then
		cp ${BBCONFFILE}.sample ${BBCONFFILE}
	fi
		export BBROOTDIR="$( cat $BBCONFFILE | grep -v '^#' | grep BBROOTDIR:  | cut -d ":" -f 2- | sed -e "s/^[[:blank:]]//g")"
		source ${BBROOTDIR}/misc/commands.idx
		source ${BBROOTDIR}/misc/variables.idx
		export BBTEMPMOUNTDIR="${BBBURNDIR}/mnt"
	
	# Read in the language file
	source ${BBROOTDIR}/lang/${BBLANG}/BashBurn.lang
	
	# Read in common functions
	source ${BBROOTDIR}/misc/commonfunctions.sh
	
	# Check if $BBBURNDIR exists, and if you have write perms.
	
	if [[ ! -e "${BBBURNDIR}" ]]; then # Check for temporary directory  
		echo
		echo $bb_no_temp_dir 
		if mkdir "${BBBURNDIR}"; then 
			echo "'$BBBURNDIR' $bb_text_1"
			echo $bb_text_2
			wait_for_enter
		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
}

init_bashburn # Make some checks

####PROGRAM START####
while true; do
# This is for the numbering of the menu choices. It is
# updated once before each entry printout in the menu
# except exit which should always be 0. The variable must
# be reset within this while loop or bad things will happen.
(( bb_m_c = 0 ))
# <colors>
source "$BBROOTDIR"/misc/colors.idx
# </colors>
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})"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_1"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_2"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_3"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_4"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_5"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_6"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_7"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_8"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_9"
(( bb_m_c += 1 ))
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} $bb_m_c) $bb_menu_10"
echo -e "${BBTABLECOLOR}|${BBMAINCOLOR} 0) $bb_menu_0"
echo -e "${BBTABLECOLOR}|"
echo -n -e "${BBINPUTCOLOR}$bb_menu_input[0-$bb_m_c] |>${BBMAINCOLOR} "
read action
echo -e "$BBCOLOROFF"
# </table>

case $action in
	1)	# If you chose audio, do:
		${BBROOTDIR}/menus/audio_menu.sh
		continue
		;;
	2) 	# If you chose data, do:
		${BBROOTDIR}/menus/data_menu.sh
		continue
		;;
	3)	#If you chose ISO, do:
		${BBROOTDIR}/menus/iso_menu.sh 
		continue
		;;
	4)	#Burn an bin/cue file:
		${BBROOTDIR}/burning/bincue.sh
		continue
		;;
	5)	#Multisession - Alrighty then...
		${BBROOTDIR}/burning/multi.sh	
		continue
		;;
	6)	#If you chose to blank a CDRW
		check_cd_status
		if [ ${BB_CDSTATUS} == "BLANK" ]; then
			echo $bb_cdrw_blank6
			wait_for_enter
		else
			blank_cd	   
		fi
		continue 
		;;
	7)	# If you chose to configure BashBurn:
		${BBROOTDIR}/config/configure.sh
		# And in case we changed any options, read in vars again
		source ${BBROOTDIR}/misc/variables.idx
		continue
		;;
	8)	# Mount/unmount a cd
		${BBROOTDIR}/misc/mount.sh
		continue
		;;
	9)	# Check program's paths of burning, ripped, codecs, etc.
		${BBROOTDIR}/misc/check_path.sh
		continue
		;;
	10)	# Copy/link data to the temporary burn dir
		${BBROOTDIR}/misc/datadefine.sh
		continue
		;;
	0)	# Traitor! You quit BashBurn!
		# Reset terminal
		tput sgr0
		force_quit
		;;
	*)	# You actually chose wrong? ...hmmm...
		echo "$bb_exit_error[0-$bb_m_c]"  
		wait_for_enter
		continue
		;;
esac
done



syntax highlighted by Code2HTML, v. 0.9.1