#!/usr/bin/env bash

# Contains all configuration functions

# Confirmation routine on leaving configuration.
get_confirm() {
	  if [ $BB_CURRENT_STATE = 0 ]; then
  		return 1
	  else
  		echo -e "${bb_conf_xit_2}\n ${bb_conf_err}'n' (${bb_conf_menu0}) ${BBTABLECOLOR}|${BBCOLOROFF} 'y' (${bb_conf_xit_1})"
		echo -e "${BBDECOLIN3}"
		while true; do
			echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
			read ans
			case "$ans" in
				n | no | No | NO ) # Nick - needs adding lang specific (Nien etc.)
					return 0
					;;
				y | yes | Y | Yes | YES ) # Nick - needs adding lang specific (Ja etc.)
					return 1
					;;
				*)
					echo -e "${bb_conf_err}y/n" 
					;; # Lang options?
			esac
		done
	  fi
}

# Are you REALLY sure routine for reset to defaults.
get_really_sure() {
	  echo -e "${bb_conf_menu26}?  ${bb_conf_ch19_5}\n ${bb_conf_err}'n' (${bb_conf_menu0}) ${BBTABLECOLOR}|${BBCOLOROFF} 'y' (${bb_conf_menu26})"
	  echo -e "${BBDECOLIN3}"
	  while true; do
  		echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
		read ans
		case "$ans" in
			n | no | No | NO ) # Nick - needs adding lang specific (Nien etc.)
				return 0
				;;
			y | yes | Y | Yes | YES ) # Nick - needs adding lang specific (Ja etc.)
				return 1
				;;
			*)  
				return 0
				;;
		esac
	  done
}

# Confirm new settings.
get_new_settings() {
	  echo -e "${bb_conf_menu25}? ${bb_conf_ch19_5}\n ${bb_conf_err}'n' (${bb_conf_menu0}) ${BBTABLECOLOR}|${BBCOLOROFF} 'y' (${bb_conf_menu25})"
	  echo -e "${BBDECOLIN3}"
	  while true; do
  		echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
		read ans
		case "$ans" in
			n | no | No | NO ) # Nick - needs adding lang specific (Nien etc.)
				return 0
				;;
			y | yes | Y | Yes | YES ) # Nick - needs adding lang specific (Ja etc.)
				return 1
				;;
			*)  
				return 0
				;;
		esac
	  done
}


### PRINT HEADER ###
pretty_top()
{
	  clear
	  echo -e "${BBTABLECOLOR}${BBDECOLINE}"
	  echo -e "${BBTABLECOLOR}|${BBHEADCOLOR} ${BBVERSION} ${BBTABLECOLOR}|"
	  echo -e "${BBTABLECOLOR}${BBDECOLINE}"
	  echo -e "${BBTABLECOLOR}|${BBCOLOROFF}"
}

change_writer_device()
{
	  # Save current value
	  OLDBBCDWRITER="${BBCDWRITER}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo -e "${BBOPTIONCOLOR}$bb_conf_ch1_1"
	  echo -e "${BBOPTIONCOLOR}$bb_conf_ch1_2"
	  echo -e "${BBOPTIONCOLOR}$bb_conf_ch1_3"
	  echo -e "${BBOPTIONCOLOR}$bb_conf_ch1_4"
	  echo -e "${BBOPTIONCOLOR}$bb_conf_ch1_5${BBMAINCOLOR}/etc/default/cdrecord ${BBOPTIONCOLOR}$bb_conf_ch1_6"
	  echo -e "${BBOPTIONCOLOR}$bb_conf_ch1_10"
	  echo -e "${BBDECOLIN3}"
	  echo -e "${BBMAINCOLOR}$bb_conf_ch1_7${BBCOLOROFF}"
	  "${BB_CDBURNCMD}" -scanbus
	  echo
	  echo -e "${BBMAINCOLOR}$bb_conf_ch1_8${BBCOLOROFF}"
	  "${BB_CDBURNCMD}" -scanbus dev=ATA
	  echo
	  echo "$(grep '\(cdrom\|dvd\|cdrw\|cdwriter\)' /etc/fstab)"
	  echo
	  echo -n -e "${BBINPUTCOLOR}$bb_conf_ch1_9${BBCOLOROFF}"
	  read BBCDWRITER

	  # If nothing was entered, restore old value.
	  if check_length "${BBCDWRITER}"; then
		BBCDWRITER="${OLDBBCDWRITER}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

change_cdrom_device()
{
	  OLDBBCDROM="${BBCDROM}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo -e "${BBMAINCOLOR}"
	  echo $bb_conf_ch2_1
	  echo
	  echo $bb_conf_ch2_2
	  echo -e "${BBSUBCOLOR}"
	  echo
	  grep "\(dvd\|cd\|cdrw\)" /etc/fstab
	  echo
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}$bb_conf_ch2_3${BBCOLOROFF}"
	  read BBCDROM
	  
	  if check_length "${BBCDROM}"; then
		BBCDROM="${OLDBBCDROM}"
	  else
	 	BB_CURRENT_STATE=1
	  fi	  
}

change_cd_mount_point()
{
	  OLDBBCDMNT="${BBCDMNT}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo -e "${BBMAINCOLOR}"
	  echo $bb_conf_ch3_1
	  echo $bb_conf_ch3_2
	  echo -e "${BBSUBCOLOR}"
	  echo
	  grep "\(dvd\|cd\|cdrw\)" /etc/fstab
	  echo
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}$bb_conf_ch3_3${BBCOLOROFF}"
	  read BBCDMNT

	  if check_length "${BBCDMNT}"; then
		BBCDMNT="${OLDBBCDMNT}"
	  else
	 	BB_CURRENT_STATE=1
	  fi	  
}

change_writer_speed()
{
	  OLDBBSPEED="${BBSPEED}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch4_1
	  echo $bb_conf_ch4_2
	  echo $bb_conf_ch4_3
	  echo $bb_conf_ch4_4
	  echo $bb_conf_ch4_5
	  echo $bb_conf_ch4_6
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}$bb_conf_ch4_7${BBCOLOROFF}"
	  read BBSPEED

	  if check_length "${BBSPEED}"; then
		BBSPEED="${OLDBBSPEED}"
	  else
	 	BB_CURRENT_STATE=1
	  fi
}

change_blanking()
{
	  OLDBBBLANKING="${BBBLANKING}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  "${BB_CDBURNCMD}" blank=help
	  echo -e "${BBMAINCOLOR}$bb_conf_ch5_1${BBCOLOROFF}"
	  echo -e "$bb_conf_ch5_2${BBOPTIONCOLOR}${BBBLANKING}${BBCOLOROFF}"
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBBLANKING

	  if check_length "${BBBLANKING}"; then
		BBBLANKING="${OLDBBBLANKING}"
	  else
	 	BB_CURRENT_STATE=1
	  fi
}

set_num_devices()
{
	  OLDBBNUMDEV="${BBNUMDEV}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch6_1
	  echo $bb_conf_ch6_2
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBNUMDEV
	  
	  if check_length "${BBNUMDEV}"; then
		BBNUMDEV="${OLDBBNUMDEV}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

change_rootdir()
{
	  OLDROOTDIR="${BBROOTDIR}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch7_1
	  echo -e "${BBMAINCOLOR}$bb_conf_ch7_2${BBCOLOROFF}"
	  echo -e "$bb_conf_ch7_3$bb_conf_ch7_4"
	  echo -e "$bb_conf_ch5_2${BBOPTIONCOLOR}${BBROOTDIR}${BBCOLOROFF}"
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBROOTDIR
	  
	  if check_length "${BBROOTDIR}"; then # [ ${#BBROOTDIR} = 0 ]; then
  		BBROOTDIR=${OLDROOTDIR}
	  else
		BB_CURRENT_STATE=1
	  fi
}

change_burndir()
{
	  OLDBURNDIR="${BBBURNDIR}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch8_1
	  echo $bb_conf_ch8_2
	  echo -e "$bb_conf_ch7_3$bb_conf_ch7_4"
	  echo -e "$bb_conf_ch5_2${BBOPTIONCOLOR}${BBBURNDIR}${BBCOLOROFF}"
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBBURNDIR
	  
	  if check_length "${BBBURNDIR}"; then # if [ ${#BBBURNDIR} = 0 ]; then
  		BBBURNDIR=${OLDBURNDIR}
	  else
		BB_CURRENT_STATE=1
	  fi
}

change_label()
{
	  OLDBBLABEL="${BBLABEL}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch9_1
	  echo $bb_conf_ch9_2
	  echo $bb_conf_ch9_3
	  echo -e "$bb_conf_ch9_4${BBOPTIONCOLOR}${BBLABEL}${BBCOLOROFF}." 
	  echo $bb_conf_ch9_4b
	  echo $bb_conf_ch9_4c
	  echo $bb_conf_ch9_5
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBLABEL

	  if check_length "${BBLABEL}"; then
		BBLABEL="${OLDBBLABEL}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

change_copyright_cd()
{
	  OLDBBCOPYRIGHT="${BBCOPYRIGHT}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch10_1
	  echo -e "$bb_conf_ch5_2${BBOPTIONCOLOR}${BBCOPYRIGHT}${BBCOLOROFF}"
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBCOPYRIGHT

	  if check_length "${BBCOPYRIGHT}"; then
		BBCOPYRIGHT="${OLDBBCOPYRIGHT}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_author()
{
	  OLDBBAUTHOR="${BBAUTHOR}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch11_1
	  echo $bb_conf_ch11_2
	  echo $bb_conf_ch11_3
	  echo -e "$bb_conf_ch11_4${BBOPTIONCOLOR}${BBAUTHOR}${BBCOLOROFF}"
	  echo $bb_conf_ch11_5
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBAUTHOR

	  if check_length "${BBAUTHOR}"; then
		BBAUTHOR="${OLDBBAUTHOR}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_publisher()
{
	  OLDBBPUBLISHER="${BBPUBLISHER}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch12_1
	  echo $bb_conf_ch12_2
	  echo $bb_conf_ch12_3
	  echo -e "$bb_conf_ch12_4${BBOPTIONCOLOR}${BBPUBLISHER}${BBCOLOROFF}."
	  echo $bb_conf_ch12_5
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBPUBLISHER

	  if check_length "${BBPUBLISHER}"; then
		BBPUBLISHER="${OLDBBPUBLISHER}"
	  else
	  	BB_CURRENT_STATE=1
	  fi
}

set_description()
{
	  OLDBBDESCRIPTION="${BBDESCRIPTION}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch13_1
	  echo $bb_conf_ch13_2
	  echo $bb_conf_ch13_3
	  echo -e "${BBOPTIONCOLOR}${BBDESCRIPTION}${BBCOLOROFF}"
	  echo $bb_conf_ch13_4
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBDESCRIPTION

	  if check_length "${BBDESCRIPTION}"; then
		BBDESCRIPTION="${OLDBBDESCRIPTION}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_package_name()
{
	  OLDNAMEOFPACKAGE="${BBNAMEOFPACKAGE}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch14_1
	  echo $bb_conf_ch14_2
	  echo $bb_conf_ch14_3
	  echo $bb_conf_ch14_4
	  echo -e "$bb_conf_ch14_5${BBOPTIONCOLOR}${BBNAMEOFPACKAGE}${BBCOLOROFF}"
	  echo $bb_conf_ch14_6
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBNAMEOFPACKAGE

	  if check_length "${BBNAMEOFPACKAGE}"; then
		BBNAMEOFPACKAGE="${OLDNAMEOFPACKAGE}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_normalize()
{
	  OLDNORMALIZE="${BBNORMALIZE}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch15_1
	  echo $bb_conf_ch15_2
	  echo $bb_conf_ch15_3
	  echo -e "$bb_conf_ch15_4${BBOPTIONCOLOR}${BBNORMALIZE}${BBCOLOROFF}."
	  echo $bb_conf_ch15_5
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBNORMALIZE

	  if check_length "${BBNORMALIZE}"; then
		BBNORMALIZE="${OLDNORMALIZE}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_driver_options()
{
	  OLDOPTS="${BBOPT_ONE}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch16_1
	  echo $bb_conf_ch16_2
	  echo -e "$bb_conf_ch5_2${BBOPTIONCOLOR}<${BBOPT_ONE}>${BBCOLOROFF}"
	  ${BB_CDBURNCMD} dev=$BBCDWRITER -checkdrive driveropts=help 
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBOPT_ONE

	  if check_length "${BBOPT_ONE}"; then
		BBOPT_ONE="${OLDOPTS}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_fifo_dir()
{
	  OLDBBFIFODIR="${BBFIFODIR}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch17_1
	  echo $bb_conf_ch17_2
	  echo -e "$bb_conf_ch17_3${BBOPTIONCOLOR}${BBFIFODIR}${BBCOLOROFF}$bb_conf_ch17_4"
	  echo -e "(${BBOPTIONCOLOR}${BBFIFODIR}${BBCOLOROFF}$bb_conf_ch17_5"
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBFIFODIR

	  if check_length "${BBFIFODIR}"; then
		BBFIFODIR="${OLDBBFIFODIR}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

delete_temp()
{
	  OLDBBDTB="${BBDELTEMPBURN}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo -e "$bb_conf_ch18_1${BBOPTIONCOLOR}${BBBURNDIR}${BBCOLOROFF}$bb_conf_ch18_2"
	  echo $bb_conf_ch18_3
	  echo $bb_conf_ch18_4
	  echo -e "$bb_conf_ch5_2${BBOPTIONCOLOR}${BBDELTEMPBURN}${BBCOLOROFF}."
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBDELTEMPBURN

	  if check_length "${BBDELTEMPBURN}"; then
		BBDELTEMPBURN="${OLDBBDTB}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_overburn()
{
	  OLDBBOVERBURN="${BBOVERBURN}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch19_1
	  echo $bb_conf_ch19_2
	  echo $bb_conf_ch19_3
	  echo $bb_conf_ch19_4
	  echo $bb_conf_ch19_5
	  echo -e "$bb_conf_ch5_2${BBOPTIONCOLOR}${BBOVERBURN}${BBCOLOROFF}."
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBOVERBURN

	  if check_length "${BBOVERBURN}"; then
		BBOVERBURN="${OLDBBOVERBURN}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_audio_copyprotect()
{
	  OLDBBCPP="${BBCOPY_PROTECT}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch20_1
	  echo -e "$bb_conf_ch20_2$bb_conf_ch5_2${BBOPTIONCOLOR}${BBCOPY_PROTECT}${BBCOLOROFF}"
	  echo -e "${BBMAINCOLOR}$bb_conf_ch20_3${BBCOLOROFF}$bb_conf_ch20_4${BBMAINCOLOR}$bb_conf_ch20_5${BBCOLOROFF}$bb_conf_ch20_6"
	  echo $bb_conf_ch20_7
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBCOPY_PROTECT

	  if check_length "${BBCOPY_PROTECT}"; then
		BBCOPY_PROTECT="${OLDBBCPP}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_bitrate()
{
	  OLDBBBITRATE="${BBBITRATE}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch21_1
	  echo $bb_conf_ch21_2
	  echo $bb_conf_ch21_3
	  echo $bb_conf_ch21_4
	  echo "ie. 64, 128, 160, 192, 256."
	  echo -e "$bb_conf_ch21_5$bb_conf_ch5_2${BBOPTIONCOLOR}${BBBITRATE}${BBCOLOROFF}"
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBBITRATE

	  if check_length "${BBBITRATE}"; then
		BBBITRATE="${OLDBBBITRATE}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_language()
{
	  OLDBBLANG="${BBLANG}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch22_1
	  echo $bb_conf_ch22_2; echo
	  ls ${BBROOTDIR}/lang
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBLANG

	  if check_length "${BBLANG}"; then
		BBLANG="${OLDBBLANG}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_dtao()
{
	  OLDBBDTAO="${BBDTAO}"
	  pretty_top
	  echo -e "${BBINFOLINE}"
	  echo $bb_conf_ch23_1
	  echo -e ${BBOPTIONCOLOR}$bb_conf_ch23_2${BBCOLOROFF}$bb_conf_ch23_2b${BBOPTIONCOLOR}$bb_conf_ch23_2c${BBCOLOROFF}
	  echo $bb_conf_ch23_3
	  echo -e ${BBOPTIONCOLOR}$bb_conf_ch23_4${BBCOLOROFF}$bb_conf_ch23_5
	  echo $bb_conf_ch23_6
	  echo $bb_conf_ch23_7
	  echo -e "${BBDECOLIN3}"
	  echo
	  echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	  read BBDTAO

	  if check_length "${BBDTAO}"; then
		BBDTAO="${OLDBBDTAO}"
	  else
		BB_CURRENT_STATE=1
	  fi
}

set_gaps()
{
	OLDBBPADDING="${BBPADDING}"
	pretty_top
	echo -e "${BBINFOLINE}"
	echo $bb_conf_ch24_1
	echo $bb_conf_ch24_2
	echo -e $bb_conf_ch24_3a${BBOPTIONCOLOR} -nopad ${BBCOLOROFF}$bb_conf_ch24_3b
	echo -e $bb_conf_default_is${BBOPTIONCOLOR} -pad ${BBCOLOROFF}$bb_conf_ch24_4
	echo -e ${BBDECOLIN3}
	echo
	echo -n -e "${BBINPUTCOLOR}|> ${BBCOLOROFF}"
	read BBPADDING

	if check_length "${BBPADDING}"; then
		BBPADDING="${OLDBBPADDING}"
	else
		BB_CURRENT_STATE=1
	fi
}

