#! /bin/bash

# SlacK3b - a cd/dvd burning application

# programs
dvdburner=/usr/bin/growisofs
cdburner=/usr/bin/cdrecord
isomaker=/usr/bin/mkisofs

### VARIABLES ###
cd_max_dim=734003200
temp_dir=/tmp/SlacK3b
burn_device=/dev/hdc


### ERRORS ###
E_NOTISO=21
E_BIGGERISO=22


### FUNCTIONS ###

# function that checks the temporary directory
check_temp () {
	if [[ ! -d $temp_dir ]];then
		mkdir -p $temp_dir
	else
		rm -rf $temp_dir
		mkdir -p $temp_dir
	fi
}

# function that shows a welcome message
welcome () {
	dialog --clear --title "SlacK3b" --msgbox "Welcome to SlacK3b
	
	SlacK3b is a tool for cd/dvd burning.
	Of course it's not a matter of reinventing the wheel,
	but it's opinion of the author that a wrapper around `basename $dvdburner`,
	`basename $cdburner` and `basename $isomaker` was needed.
	Someone could say that already exists a lot of applications that to this
	kind of stuff, but none of these programs are simple shell scripts with 
	no dependancies at all other than the 3 programs I listed before and 
	the dialog library which is installed by default on almost every Linux pc.

	I hope you'll enjoy using SlacK3b, I made it as simple as I could...
	
	danix" 0 0
}

# function that shows the main menu of SlacK3b
main_menu () {
	dialog --clear --title "Select an operation" --menu "Select an option from the following menu:" 0 0 0 \
	0 "Burn an already existing ISO image on cd" \
	1 "Start creating a CD image containing data and burn it" \
	2 "Create a DVD image for data storage and burn it on DVD" \
        3 "Blank a CD/RW" \
	4 "Burn an already existing ISO image on dvd" 2> $temp_dir/oper_menu

}

# function that checks an iso image for cd or dvd
checkiso () {
	dialog --clear --title "Select an ISO image" --fselect ~/ 0 0 2> $temp_dir/iso_selected
	isoimage=`cat $temp_dir/iso_selected`
	if [[ -f $isoimage ]];then
		if [[ $isoimage == *.iso ]];then
			isoimage_dim=$(stat -c%s "$isoimage")
			if [[ $isoimage_dim -lt $cd_max_dim ]];then
				cd_burniso
			else
				dialog --clear --title "Warning" --msgbox "The image `basename $isoimage` you were trying to burn on CD is bigger than 700 MB, so you should burn it on a DVD disk. Aborting." 0 0
				exit $E_BIGGERISO
			fi
		else
			dialog --clear --title "Warning" --msgbox "The image you were trying to burn is not a valid ISO image. Aborting." 0 0
			exit $E_NOTISO
		fi
	else
		dialog --clear --title "Warning" --msgbox "The image `basename $isoimage` you were trying to burn does not exists. Aborting." 0 0
		exit $E_NOTISO
	fi
}

# function that burns the iso on cd
cd_burniso () {
	dialog --clear --title "Select the burning speed" --radiolist "Select the burning speed from the choices below" 0 0 0 \
	0 "1x" "on" \
	1 "2x" "off" \
	2 "4x" "off" \
	3 "8x" "off" \
	4 "12x" "off" \
	5 "16x" "off" \
	6 "24x" "off" \
	7 "32x" "off" \
	8 "42x" "off" \
	9 "54x" "off" 2> $temp_dir/cd_burniso_speed
	cd_burn_iso_speed=`cat $temp_dir/cd_burniso_speed`
	case $cd_burn_iso_speed in
		0 ) burning_speed=1
			;;
		1 ) burning_speed=2
			;;
		2 ) burning_speed=4
			;;
		3 ) burning_speed=8
			;;
		4 ) burning_speed=12
			;;
		5 ) burning_speed=16
			;;
		6 ) burning_speed=24
			;;
		7 ) burning_speed=32
			;;
		8 ) burning_speed=42
			;;
		9 ) burning_speed=54
			;;
	esac
	dialog --title "Burning ISO image on CD-R" --cr-wrap --infobox "Burning `basename $isoimage`\n\
	\nThis process can take quite some time, if you'd like to watch the progress you can switch to another console and type:\n\
	\ntail -f $temp_dir/iso_recording" 0 0
	cdrecord -v dev=$burn_device -dao -eject speed=$burning_speed $isoimage &>$temp_dir/iso_recording
}

# function that blanks a cdrw
blank_cd () {
	dialog --clear --title "Select blanking speed" --radiolist "Select the blanking speed for the CDRW from the list below" 0 0 0 \
	0 "2x" "on" \
	1 "4x" "off" \
	2 "12x" "off" \
	3 "24x" "off" \
	4 "max" "off" 2> $temp_dir/cd_blanking_speed
	blanking_cd_speed=`cat $temp_dir/cd_blanking_speed`
	case $blanking_cd_speed in
		0 ) blanking_speed=2
			;;
		1 ) blanking_speed=4
			;;
		2 ) blanking_speed=12
			;;
		3 ) blanking_speed=24
			;;
		4 ) blanking_speed=52
			;;
	esac
	dialog --title "Blanking CDRW" --cr-wrap --infobox "Blanking CDRW\n\
	\nBlanking a CDRW can take quite some time. If you'd like to watch the progress you can flip over to another console and type:\n\
	\ntail -f $temp_dir/cd_blanking" 0 0
	cdrecord -v blank=fast -eject speed=$blanking_speed dev=$burn_device &>$temp_dir/cd_blanking
}



# let's start executing the program
check_temp
welcome
main_menu

operation=`cat $temp_dir/oper_menu`

case $operation in
	0 ) checkiso
		;;
	1 ) dialog --clear --title "info" --msgbox "Trying to burn a data CD. This feature will be coming out soon. Stay tuned" 0 0
		;;
	2 ) dialog --clear --title "info" --msgbox "Trying to burn a data DVD. This feature will be coming out soon. Stay tuned" 0 0
		;;
	3 ) blank_cd
		;;
	4 ) dialog --clear --title "info" --msgbox "Trying to burn an ISO image on dvd. This feature will be implemented soon." 0 0
esac


exit

