      1	#!/usr/bin/perl
      2	
      3	# CDRX.pl v.0.3.1
      4	# Jason's CD-RW burning script
      5	# 2002.01.19
      6	
      7	use strict;
      8	
      9	###################
     10	# Global Variables
     11	###################
     12	
     13	my (%index_hash,
     14		$dev_type,
     15		$ref_label,
     16		$referrer,
     17		$referrer_check,
     18		$thing,
     19		$type,
     20		$target_dev,
     21		$target_label,
     22		$dev_choice,
     23		%speeds,
     24		$readdir,
     25		$writedir,
     26		$iso_name,
     27		%dev_array,
     28		$dir_Create_iso_referrer,
     29		%file_list,
     30		$new_read_dir,
     31		$new_write_dir,
     32		$burn_copy_cd_referrer,
     33		$burn_iso_cd_referrer,
     34		$burn_dir_cd_referrer,
     35		$burn_files_cd_referrer,
     36		$scsibus,
     37		$target,
     38		$lun,
     39		$dev_name,
     40		$index_num,
     41		%index_hash,
     42		$install_referrer,
     43		@scan,
     44		$cdrom_def,
     45		$cdrom_label,
     46		$cdrw_def,
     47		$cdrw_label,
     48		$write_speed,
     49		$read_dir_def,
     50		$write_dir_def);
     51	
     52	#################
     53	# Main Flow
     54	#################
     55	
     56	whoami();
     57	check_reqs();
     58	unless (</var/db/cdrx.*>) {
     59		intro();
     60		new_install();
     61	}
     62	Menu();
     63	
     64	#######################
     65	# Start of Subroutines
     66	#######################
     67	
     68	sub intro {
     69		print "\n\n\n\n\n\n\n\n\n\n\n";
     70		print "\t\t*******************************************************\n";
     71		print "\t\t***                                                 ***\n";
     72		print "\t\t***                Welcome to CDRX.                 ***\n";
     73		print "\t\t***                 Version 0.3.1                   ***\n";
     74		print "\t\t***                                                 ***\n";
     75		print "\t\t***                       ---                       ***\n";
     76		print "\t\t***                                                 ***\n";
     77		print "\t\t***                   Jason Dixon                   ***\n";
     78		print "\t\t***                                                 ***\n";
     79		print "\t\t***  Proceed to the Main Menu by pressing enter...  ***\n";
     80		print "\t\t*******************************************************\n";
     81		print "\n\n\n\n\n\n\n\n\n\n\n";
     82		<STDIN>;
     83	}
     84	
     85	#################
     86	# Menus
     87	#################
     88	
     89	sub Menu {
     90		my $main_menu_choice;
     91		printbuffer();
     92		print "::: CDRX Main Menu :::\n\n\n";
     93		print "Choose one of the following: \n\n\n";
     94		print "\t(1) Setup\n";
     95		print "\t(2) CD-RW blanking\n";
     96		print "\t(3) Create ISO Image\n";
     97		print "\t(4) Burn to CD-R/RW\n";
     98		print "\t(5) Quit\n\n\n";
     99		chomp ($main_menu_choice = <STDIN>);
    100		$main_menu_choice =~ s/ +//;
    101		while (!($main_menu_choice =~ /^[1-5]\b/)) {
    102			print "Not a valid response.  Please choose 1-5: \n";
    103			chomp ($main_menu_choice = <STDIN>);
    104		}
    105		SWITCH: {
    106	                if ($main_menu_choice == 1) { Setup(); }
    107	       	        if ($main_menu_choice == 2) { Blank(); }
    108	       		if ($main_menu_choice == 3) { Create_menu(); }
    109	               	if ($main_menu_choice == 4) { Burn_menu(); }
    110			else {
    111				printbuffer(); 
    112				print "\t\t\t\t******************\n";
    113				print "\t\t\t\t***  Goodbye!  ***\n"; 
    114				print "\t\t\t\t******************\n\n\n\n\n\n";
    115				exit; 
    116			}
    117		}
    118	}
    119	
    120	sub Setup {
    121		my $setup_menu_choice;
    122		printbuffer();
    123		print "::: CDRX Setup :::\n\n\n";
    124		print "Choose one of the following: \n\n\n";
    125		print "\t(1) View current settings\n";
    126		print "\t(2) Setup CD-rom read device\n";
    127		print "\t(3) Setup CD-R/W write device\n";
    128		print "\t(4) Choose CD-R/W write speed\n";
    129		print "\t(5) Configure \"READ\" directory\n";
    130		print "\t(6) Configure temporary write directory\n";
    131		print "\t(7) Return to Main Menu\n\n\n";
    132		chomp ($setup_menu_choice = <STDIN>);
    133		$setup_menu_choice =~ s/ +//;
    134		while (!($setup_menu_choice =~ /^[1-7]\b/)) {
    135			print "Not a valid response.  Please choose 1-7: \n";
    136			chomp ($setup_menu_choice = <STDIN>);
    137		}
    138		SWITCH: {
    139	                if ($setup_menu_choice == 1) { view_settings(); }
    140	                if ($setup_menu_choice == 2) { setup_cdrom(); }
    141	                if ($setup_menu_choice == 3) { setup_cdrw(); }
    142			if ($setup_menu_choice == 4) { choose_speed(); }
    143	                if ($setup_menu_choice == 5) { config_read_dir(); }
    144	                if ($setup_menu_choice == 6) { config_temp_dir(); }
    145			else { Menu(); }
    146	 	}
    147	}
    148	
    149	sub Create_menu {
    150		my $create_menu_choice;
    151	        printbuffer();
    152	        print "::: Create ISO Image :::\n\n\n";
    153	        print "Choose one of the following:\n\n\n";
    154	        print "\t(1) Create ISO from CD-rom device\n";
    155	        print "\t(2) Create ISO from \"READ\" directory\n";
    156		print "\t(3) Create ISO from assorted files\n";
    157	        print "\t(4) Return to Main Menu\n\n\n";
    158	        chomp ($create_menu_choice = <STDIN>);
    159	        $create_menu_choice =~ s/ +//;
    160	        while (!($create_menu_choice =~ /^[1-4]\b/)) {
    161	                print "Not a valid response.  Please choose 1-4: \n";
    162	                chomp ($create_menu_choice = <STDIN>);
    163	        }
    164		SWITCH: {
    165			if ($create_menu_choice == 1) { cd_Create_iso(); }
    166			if ($create_menu_choice == 2) { dir_Create_iso(); }
    167			if ($create_menu_choice == 3) { files_Create_iso(); }
    168			else { Menu(); }
    169		}
    170	}
    171	
    172	sub Burn_menu {
    173		my $burn_menu_choice;
    174		printbuffer();
    175		print "::: Burn to CD-R/W :::\n\n\n";
    176		print "Choose one of the following:\n\n\n";
    177		print "\t(1) CD-to-CD instant copy\n";
    178		print "\t(2) Burn an existing ISO Image\n";
    179		print "\t(3) Burn from \"READ\" directory\n";
    180		print "\t(4) Burn assorted files\n";
    181		print "\t(5) Return to Main Menu\n\n\n";
    182		chomp ($burn_menu_choice = <STDIN>);
    183		$burn_menu_choice =~ s/ +//;
    184		while (!($burn_menu_choice =~ /^[1-5]\b/)) {
    185			print "Not a valid response.  Please choose 1-5: \n";
    186			chomp ($burn_menu_choice = <STDIN>);
    187		}
    188		SWITCH: {
    189			if ($burn_menu_choice == 1) { burn_copy_cd(); }
    190			if ($burn_menu_choice == 2) { burn_iso_cd(); }
    191			if ($burn_menu_choice == 3) { burn_dir_cd(); }
    192			if ($burn_menu_choice == 4) { burn_files_cd(); }
    193			else { Menu(); }
    194		}
    195	}
    196	
    197	
    198	#################
    199	# Setup
    200	#################
    201	
    202	sub view_settings {
    203		db_handle();
    204		printbuffer();
    205	        print "::: View settings :::\n\n\n";
    206		print "CD-rom read device (target,lun label):     $cdrom_def  $cdrom_label\n";
    207		print "CD-R/W write device (target,lun label):    $cdrw_def  $cdrw_label\n";
    208		print "CD-R/W write speed:                        $write_speed\n";
    209		print "READ directory:                            $read_dir_def\n";
    210		print "WRITE directory:                           $write_dir_def\n";
    211		print "\n\n\nPress enter to return to the Setup Menu\n";
    212		<STDIN>;
    213		Setup();
    214	}
    215	
    216	sub setup_cdrom { 
    217		my($referrer_check) = @_;
    218		my $i;
    219		$referrer = "CDROM";
    220		$ref_label = "CDROMLBL";
    221		$dev_type = "READ";
    222		$thing = "device";
    223		db_handle();
    224		scanbus();
    225		printbuffer();
    226		print "::: Setup CD-rom read device :::\n\n\n";
    227		if ($cdrom_label) {
    228			print "Your default CD-rom device is the $cdrom_label\n\n";
    229		}
    230		print "The list of available devices are:\n\n";
    231		for ($i=0; $i<@scan; $i++) {
    232			print "\t\($index_hash{$i}[0]\) $index_hash{$i}[4]\n";
    233		}
    234		devchoice();
    235		dotfile($referrer, $target_dev);
    236		dotfile($ref_label, $target_label);
    237		settings_result_print($index_hash{$dev_choice - 1}[4],$dev_type,$thing,$referrer_check);
    238	}
    239	
    240	sub setup_cdrw {
    241		my($referrer_check) = @_;
    242		my $i;
    243		$referrer = "CDRW";
    244		$ref_label = "CDRWLBL";
    245		$dev_type = "WRITE";
    246		$thing = "device";
    247		db_handle();
    248		scanbus();
    249		printbuffer();
    250		print "::: Setup CD-R/W write device :::\n\n\n";
    251		if ($cdrw_label) {
    252			print "Your default CD-R/W device is the $cdrw_label\n\n";
    253		}
    254		print "The list of available devices are:\n\n";
    255		for ($i=0; $i<@scan; $i++) {
    256			print "\t\($index_hash{$i}[0]\) $index_hash{$i}[4]\n";
    257		}
    258		devchoice();
    259		dotfile($referrer, $target_dev);
    260		dotfile($ref_label, $target_label);
    261		settings_result_print($index_hash{$dev_choice - 1}[4],$dev_type,$thing,$referrer_check);
    262	}
    263	
    264	sub devchoice {
    265		print "\nPlease choose one of the above listed devices as your $dev_type device:\n\n";
    266		chomp ($dev_choice = <STDIN>);
    267		$dev_choice =~ s/ +//;
    268		SWITCH: {
    269			if ($dev_choice eq "") { print "That is not a valid option.\n"; devchoice(); }
    270			if ($dev_choice =~ /[\D]/) { print "That is not a valid option.\n"; devchoice(); }
    271			if (!($index_hash{$dev_choice - 1}[0])) { print "That is not a valid option.\n"; devchoice(); }
    272			else {
    273				$target_dev = join(",", $index_hash{$dev_choice - 1}[1], $index_hash{$dev_choice - 1}[2], $index_hash{$dev_choice - 1}[3]);
    274				$target_label = $index_hash{$dev_choice - 1}[4];
    275			}
    276		}
    277		return ($target_dev, $target_label);
    278	}
    279	
    280	sub choose_speed {
    281		my($referrer_check) = @_;
    282		my ($index,$rate);
    283		my $speed_choice;
    284		$referrer = "SPEED";
    285		$type = "write";
    286		$thing = "speed";
    287		printbuffer();
    288		print "::: Choose CD-R/W write speed :::\n\n\n";
    289		print "Please choose one of the following speeds for your CD-R/W device:\n\n";
    290		%speeds = qw( 1 2X 2 4X 3 6X 4 8X 5 12X 6 16X 7 20X 8 24X 9 32X );
    291		while (($index,$rate) = each(%speeds)) {
    292			print "($index)\t$rate\n";
    293		}
    294		print "\n\n";
    295		chomp ($speed_choice = <STDIN>);
    296		$speed_choice =~ s/ +//;
    297		while (!($speeds{$speed_choice})) {
    298			print "Not a valid response.  Please choose 1-9: \n";
    299			chomp ($speed_choice = <STDIN>);
    300	        }
    301		dotfile($referrer, $speeds{$speed_choice});
    302		settings_result_print($speeds{$speed_choice},$type,$thing,$referrer_check);
    303	}
    304	
    305	sub config_read_dir {
    306		my($referrer_check) = @_;
    307		$referrer = "READDIR";
    308		$type = "READ";
    309		$thing = "directory";
    310		db_handle();
    311	        printbuffer();
    312	        print "::: Setup READ directory :::\n\n\n";
    313		if ($referrer_check == 1) {
    314			print "HINT:\nThe \"READ\" directory is where you expect to read ISO images,\n";
    315			print "files or directories from while performing your burn.\n";
    316			print "Don't worry too much about this, you can always change it later.\n\n";
    317			print "A good suggestion might be your home directory.\n\n\n";
    318		} else {
    319			print "Your \"READ\" directory is where you expect to read ISO images or files from.\n";
    320			print "Configure this to reflect where you have stored your files TO BE BURNED.\n\n\n";
    321		}
    322		if ($read_dir_def) {
    323			print "Your default \"READ\" directory is $read_dir_def\n\n";
    324		}
    325	       	print "Please insert the directory to read your ISO image from:\n\n";
    326	       	chomp ($readdir = <STDIN>);
    327		while (!(opendir(READDIR, "$readdir"))) {
    328			print "Sorry, can't use $readdir.  Please try again:\n";
    329			chomp ($readdir = <STDIN>);
    330		}
    331	       	closedir(READDIR);
    332	       	dotfile($referrer, $readdir);
    333		settings_result_print($readdir,$type,$thing,$referrer_check);
    334	}
    335	
    336	sub config_temp_dir { 
    337		my($referrer_check) = @_;
    338		$referrer = "WRITEDIR";
    339		$type = "WRITE";
    340		$thing = "directory";
    341		db_handle();
    342		printbuffer();
    343		print "::: Setup WRITE directory :::\n\n\n";
    344		if ($referrer_check == 1) {
    345			print "HINT:\nThe \"WRITE\" directory is basically just a directory\n";
    346			print "for this program to write ISO images to.  You want to\n";
    347			print "make sure this directory or partition has enough space\n";
    348			print "available before writing to disk.\n\n";
    349			print "A good suggestion might be /usr/local or /tmp\n";
    350			print "(providing /tmp has enough space).\n\n\n";
    351		} else {
    352			print "Your \"WRITE\" directory is where you would like to write\n";
    353			print "your ISO images, whether permanently (creating ISO images)\n";
    354			print "or temporarily (burning direct to CD-R/W).\n\n\n";
    355		}
    356		if ($write_dir_def) {
    357			print "Your default \"WRITE\" directory is $write_dir_def\n\n";
    358		}
    359		print "Please insert the directory to write your temporary and/or ISO image to:\n\n";
    360		chomp ($writedir = <STDIN>);
    361		while (!(opendir(WRITEDIR, "$writedir"))) {
    362			print "Sorry, can't use $writedir.  Please try again:\n";
    363			chomp ($writedir = <STDIN>);
    364		}
    365		closedir(WRITEDIR);
    366		dotfile($referrer, $writedir);
    367		settings_result_print($writedir,$type,$thing,$referrer_check);
    368	}
    369	
    370	sub settings_result_print {
    371	        my($object,$type,$thing,$return_handler) = @_;
    372	        printbuffer();
    373	        print "*******************************************\n";
    374	        print "You've chosen $object\n";
    375	        print "as your $type $thing.\n\n";
    376	        if ($return_handler == 1) {
    377	                print "Press enter to return to the Install Wizard\n";
    378		} elsif ($return_handler == 2) {
    379			print "Press enter to continue \"CD-to-CD\" copying\n";
    380		} elsif ($return_handler == 3) {
    381			print "Press enter to continue \"ISO to CD\" burning\n";
    382		} elsif ($return_handler == 4) {
    383			print "Press enter to continue creating your ISO\n";
    384		} elsif ($return_handler == 5) {
    385			print "Press enter to continue \"directory to CD\" burning\n";
    386	        } else {
    387	                print "Press enter to return to the Setup Menu\n";
    388	        }
    389	        print "*******************************************\n\n\n\n\n\n";
    390	        <STDIN>;
    391	        if ($return_handler == 1) {
    392	                install_handler();
    393		} elsif ($return_handler == 2) {
    394			burn_copy_cd();
    395		} elsif ($return_handler == 3) {
    396			burn_iso_cd();
    397		} elsif ($return_handler == 4) {
    398			dir_Create_iso();
    399		} elsif ($return_handler == 5) {
    400			burn_dir_cd();
    401	        } else {
    402	                Setup();
    403	        }
    404	}
    405	
    406	#################
    407	# Blanking
    408	#################
    409	
    410	sub Blank {
    411		my $blank_choice;
    412		my $option;
    413		my $continue;
    414		my $no_media;
    415		printbuffer();
    416		print "Choose one of the following blank types: \n\n\n";
    417		print "\t(1) All = Blank the entire disk\n";
    418		print "\t(2) Fast = Minimally blank the disk \(PNA, TOC and pregap\)\n";
    419		print "\t(3) Return to Main Menu\n\n\n";
    420		chomp ($blank_choice = <STDIN>);
    421		while (!($blank_choice =~ /^[1-3]\b/)) {
    422			print "Not a valid response.  Please choose 1-3: \n";
    423			chomp ($blank_choice = <STDIN>);
    424		}
    425		if ($blank_choice == 1) {
    426			$option = "all";
    427		} elsif ($blank_choice == 2) {
    428			$option = "fast";
    429		} else {
    430			Menu();
    431		}
    432		printbuffer();
    433		print "*********************************************\n";
    434		print "About to start a \"$option\"-style CD-RW blanking.\n\n";
    435		print "Enter Y to continue or any other key\nto return to the Main Menu\n";
    436		print "*********************************************\n\n\n\n\n\n";
    437		chomp ($continue = <STDIN>);
    438		if ($continue =~ /[Yy]/) {
    439			db_handle();
    440			printbuffer();
    441			print "Blanking...\n\n\n";
    442			system"cdrecord dev=$cdrw_def blank=$option 2>/tmp/cdrx.burn.err";
    443			$no_media = `cat /tmp/cdrx.burn.err | grep cdrecord | grep 'output error'`;
    444			while ($no_media =~ /output error/) {
    445				printbuffer();
    446				print "It appears you have no disk (or a bad one) in your CD-R/W drive.\n\n";
    447				print "Please correct and press enter to try again...\n\n\n";
    448				<STDIN>;
    449				system"cdrecord dev=$cdrw_def blank=$option 2>/tmp/cdrx.burn.err";
    450				$no_media = `cat /tmp/cdrx.burn.err | grep cdrecord | grep 'output error'`;
    451			}
    452			printbuffer();
    453			print "******************************************\n";
    454			print "Blanking is complete.\n\n";
    455			print "Press enter to return to the Main Menu\n";
    456			print "******************************************\n\n\n";
    457			<STDIN>;
    458			Menu();
    459		} else {
    460			Menu();
    461		}
    462	}
    463	
    464	
    465	#################
    466	# ISO 
    467	#################
    468	
    469	sub cd_Create_iso {
    470		my $read_cdrom_dev;
    471	        printbuffer();
    472	        print "::: Create ISO from CDrom :::\n\n\n";
    473		$read_cdrom_dev = quick_choose_cdrom();
    474		write_dir_check();
    475		db_handle();
    476		name_your_iso();
    477		print "Creating ISO Image...\n\n\n";
    478		sleep 2;
    479		system"dd if=$read_cdrom_dev of=$write_dir_def/$iso_name bs=1024k";
    480		finished_iso_print();
    481	}
    482	
    483	sub dir_Create_iso {
    484		my($referrer_check) = @_;
    485		my @contents;
    486		my @read_dir_size;
    487		my @write_dir_avail;
    488		printbuffer();
    489		unless ($referrer_check == 5) {
    490			print "::: Create ISO from directory :::\n\n\n";
    491		} else {
    492			print "::: Burn from \"READ\" directory to CD :::\n\n\n";
    493		}
    494		read_dir_check();
    495		printbuffer();
    496		print "The contents of your \"READ\" directory will be listed below.\n";
    497		print "Press the spacebar to scroll through multiple screens.\n\n";
    498		print "Press enter to continue...\n\n\n";
    499		<STDIN>;
    500		printbuffer();
    501		db_handle();
    502		@contents = system"ls -aR $read_dir_def | more";
    503		foreach (@contents) {
    504			print $_;
    505		}
    506		@read_dir_size = split(/\t/, `du -ms $read_dir_def`);
    507		print "\n\n\nThe total size (in megabytes) of all files to be included is:  $read_dir_size[0].\n\n";
    508		print "Press enter to continue\n";
    509		<STDIN>;
    510		printbuffer();
    511		unless ($referrer_check == 5) {
    512			write_dir_check();
    513		}
    514		@write_dir_avail = split(/ +/, `df -m $write_dir_def | grep -v sys`);
    515		if ($write_dir_avail[3] > $read_dir_size[0]) {
    516			unless ($referrer_check == 5) {
    517				name_your_iso();
    518			} else {
    519				$iso_name = "cdrx-tmp2.iso";
    520			}
    521			print "Creating ISO Image...\n\n\n";
    522			sleep 2;
    523			system"mkisofs -r -o $write_dir_def/$iso_name $read_dir_def";
    524			unless ($referrer_check == 5) {
    525				finished_iso_print();
    526			} else {
    527				sub_burn_dir_cd();
    528			}
    529		} else {
    530			printbuffer();
    531			print "**************************************************\n";
    532			print "Sorry, your \"WRITE\" directory does not have\n";
    533			print "enough space to handle the temporary ISO image.\n\n";
    534			print "Press enter to change your directory settings...\n";
    535			print "**************************************************\n\n\n\n\n\n";
    536			<STDIN>;
    537			unless ($referrer_check == 5) {
    538				$dir_Create_iso_referrer = 4;
    539			} else {
    540				$dir_Create_iso_referrer = 5;
    541			}
    542			config_temp_dir($dir_Create_iso_referrer);
    543		}
    544	}
    545	
    546	sub files_Create_iso {
    547		my($referrer_check) = @_;
    548		my @burn_list = ();
    549		my @real_burn_list = ();
    550		my %file_list;
    551		my $file_choice;
    552		my %uniq = ();
    553		my $burn_string;
    554		printbuffer();
    555		unless ($referrer_check == 6) {
    556	        	print "::: Create ISO from files :::\n\n\n";
    557		} else {
    558			print "::: Burn assorted files to CD :::\n\n\n";
    559		}
    560		read_dir_check();
    561		printbuffer();
    562		print "Listing the contents of your current directory...\n\n";
    563		%file_list = dir_file_list();
    564		print "\nPlease choose a file number, \"L\" to list again\n\"C\" to change directories, or \"Q\" to quit:\n";
    565		chomp ($file_choice = <STDIN>);
    566		while (!($file_choice =~ /[Qq]/)) {
    567			if ($file_list{$file_choice -1}) {
    568				push(@burn_list,$file_list{$file_choice -1});
    569				print "\nPlease choose again [#/L/C/Q]:\n";
    570				chomp ($file_choice = <STDIN>);
    571			} elsif ($file_choice =~ /[Ll]/) {
    572				dir_file_list();
    573				print "\nPlease choose again [#/L/C/Q]:\n";
    574				chomp ($file_choice = <STDIN>);
    575			} elsif ($file_choice =~ /[Cc]/) {
    576				printbuffer();
    577				read_dir_check();
    578				printbuffer();
    579				print "Listing the contents of your current directory...\n\n";
    580				dir_file_list();
    581				print "\nPlease choose again [#/L/C/Q]:\n";
    582				chomp ($file_choice = <STDIN>);
    583			} else {
    584				print "\nNot a valid choice.  Please try again (#/List/Change/Quit):\n";
    585				chomp ($file_choice = <STDIN>);
    586			}
    587		}
    588		@burn_list = sort(@burn_list);
    589		foreach (@burn_list) {
    590			push(@real_burn_list,$_) unless $uniq{$_}++;
    591		}
    592		print "\n\nYour selections are:\n\n";
    593		foreach (@real_burn_list) {
    594			print "$_\n";
    595		}
    596		print "\n\n\n";
    597		write_dir_check();
    598		unless ($referrer_check == 6) {
    599			name_your_iso();
    600		} else {
    601			$iso_name = "cdrx-tmp3.iso";
    602		}
    603		db_handle();
    604		printbuffer();
    605		print "Creating ISO Image...\n\n\n";
    606		sleep 2;
    607		$burn_string = join(" ", @real_burn_list);
    608		system"mkisofs -r -o $write_dir_def/$iso_name $burn_string 2>/tmp/cdrx.error";
    609		unless ($referrer_check == 6) {
    610			finished_iso_print();
    611		} else {
    612			sub_burn_files_cd();
    613		}
    614	}
    615	
    616	sub dir_file_list {
    617		my $p;
    618		my $s=1;
    619		my $t=1;
    620		my @file_list = ();
    621		%file_list = ();
    622		db_handle();
    623		opendir(READ_DIR,$read_dir_def);
    624		foreach (sort readdir(READ_DIR)) {
    625			chomp;
    626			unless ((-d "$read_dir_def/$_") || (!-r "$read_dir_def/$_")) {
    627				push(@file_list,"$read_dir_def/$_");
    628			}
    629		}
    630		closedir(READ_DIR);
    631	        for ($p=0; $p<@file_list; $p++) {
    632	                $file_list{$p} = $file_list[$p];
    633	                print "($s) $file_list[$p]\n";
    634			if ($t == 20) {
    635	                        print "\n[Press enter to continue]\n";
    636	                        <STDIN>;
    637				$t=0;
    638	                }
    639			$t++;
    640	                $s++;
    641	        }
    642		return %file_list;
    643	}
    644	
    645	sub read_dir_check {
    646		db_handle();
    647		print "Your default \"READ\" directory is $read_dir_def\n";
    648	        print "Press enter to read from this directory\n";
    649	        print "or enter a new directory here: \n\n";
    650		chomp ($new_read_dir = <STDIN>);
    651	        if ($new_read_dir) {
    652	                $referrer = "READDIR";
    653	                while (!(opendir(READDIR, "$new_read_dir"))) {
    654				print "Sorry, can't use $new_read_dir.  Please try again:\n";
    655				chomp ($new_read_dir = <STDIN>);
    656	                }
    657	                closedir(READDIR);
    658	                dotfile($referrer, $new_read_dir);
    659	                print "\nYour new \"READ\" directory is $new_read_dir.\n\n";
    660	                print "Press enter to continue...\n";
    661	                <STDIN>;
    662	        }
    663	}
    664	
    665	sub write_dir_check {
    666		db_handle();
    667		print "Your default \"WRITE\" directory is $write_dir_def\n";
    668	        print "Press enter to write to this directory\n";
    669	        print "or enter a new directory here: \n\n";
    670		chomp ($new_write_dir = <STDIN>);
    671	        if ($new_write_dir) {
    672	                $referrer = "WRITEDIR";
    673	                while (!(opendir(WRITEDIR, "$new_write_dir"))) {
    674	                        print "Sorry, can't use $new_write_dir.  Please try again:\n\n";
    675	                        chomp ($new_write_dir = <STDIN>);
    676	                }
    677	                closedir(WRITEDIR);
    678	                dotfile($referrer, $new_write_dir);
    679	                print "\nYour new \"WRITE\" directory is $new_write_dir.\n\n";
    680	                print "Press enter to continue...\n";
    681	                <STDIN>;
    682	        }
    683	}
    684	
    685	sub name_your_iso {
    686		db_handle();
    687	        print "\nProceeding to create your ISO.\n\n";
    688		print "What would you like the file to be named? :\n\n";
    689		chomp ($iso_name = <STDIN>);
    690		check_iso_name();
    691		while (-e "$write_dir_def/$iso_name") {
    692			print "\nSorry, there is already a file by the name of $iso_name in $write_dir_def.\n";
    693			print "Please choose another name:\n\n";
    694			chomp ($iso_name = <STDIN>);
    695			check_iso_name();
    696		}
    697	        print "\n\nYour iso will be called $iso_name.\n\n";
    698		print "Press enter to continue...\n";
    699		<STDIN>;
    700		return;
    701	}
    702	
    703	sub check_iso_name {
    704	        $iso_name =~ tr/A-Z/a-z/;
    705		$iso_name =~ s/\W+//;
    706	        while (!($iso_name)) {
    707	                print "That is not a valid response.  Please try again:\n\n";
    708	                chomp ($iso_name = <STDIN>);
    709	        }
    710	        if (!($iso_name =~ /iso$/)) {
    711	                $iso_name = $iso_name . ".iso";
    712	        }
    713		return;
    714	}
    715		
    716	sub finished_iso_print {
    717	        printbuffer();
    718	        print "******************************************\n";
    719	        print "Your ISO is now finished.\n\n";
    720	        system"ls -l $write_dir_def/$iso_name";
    721	        print "\n\nPress enter to return to the Main Menu\n";
    722	        print "******************************************\n\n\n\n\n\n";
    723	        <STDIN>;
    724	        Menu();
    725	}
    726	
    727	sub quick_choose_cdrom {
    728		my @dev_array;
    729		my $k;
    730		my $m=1;
    731		my $cdrom_read_choice;
    732		print "Please choose one of the following devices to read from:\n\n";
    733	        @dev_array = split(/\n/, `ls /dev/cdr*`);
    734	        for ($k=0; $k<@dev_array; $k++) {
    735	                $dev_array{$k} = $dev_array[$k];
    736	                print "($m) $dev_array[$k]\n";
    737	                $m++;
    738	        }
    739	        print "\n";
    740	        chomp ($cdrom_read_choice = <STDIN>);
    741	        $cdrom_read_choice =~ s/ +//;
    742	        $cdrom_read_choice =~ s/\D+$//;
    743	        $cdrom_read_choice =~ s/^\D+//;
    744	        while ((!($cdrom_read_choice =~ /\d/)) || (!($dev_array{$cdrom_read_choice - 1}))) {
    745	                print "That is not an option, please choose again:\n";
    746	                chomp ($cdrom_read_choice = <STDIN>);
    747	        }
    748	        print "\nYou have chosen $dev_array{$cdrom_read_choice - 1}.\n\n";
    749		return $dev_array{$cdrom_read_choice - 1};
    750	}
    751	
    752	
    753	#################
    754	# Burning
    755	#################
    756	
    757	sub burn_copy_cd {
    758		my $cdrom_fs_size;
    759		my $read_cdrom_dev;
    760		my $mount_err;
    761		my @write_dir_avail;
    762		db_handle();
    763		printbuffer();
    764		print "::: CD-to-CD instant copy :::\n\n\n";
    765		$read_cdrom_dev = quick_choose_cdrom();
    766		if (!(`mount -l -tiso9660` =~ /$read_cdrom_dev/)) {
    767			system"mount -tiso9660 $read_cdrom_dev /mnt 2>/tmp/cdrx.mount.err";
    768			$mount_err = `grep No /tmp/cdrx.mount.err`;
    769			while ($mount_err) {
    770				print "Sorry, it appears you have no valid media in that drive.\n";
    771				print "Press enter to try again...\n\n";
    772				<STDIN>;
    773				system"mount -tiso9660 $read_cdrom_dev /mnt 2>/tmp/cdrx.mount.err";
    774				$mount_err = `grep No /tmp/cdrx.mount.err`;
    775			}
    776			$cdrom_fs_size = `du -ms /mnt`;
    777			$cdrom_fs_size =~ s/\D+$//;
    778			system"umount /mnt";
    779		} else {
    780			$cdrom_fs_size = `du -ms $read_cdrom_dev`;
    781			$cdrom_fs_size =~ s/\D+$//;
    782		}
    783		@write_dir_avail = split(/ +/, `df -m $write_dir_def | grep -v sys`);
    784		if ($write_dir_avail[3] > $cdrom_fs_size) {
    785			print "Writing temporary ISO image...\n\n";
    786			system"umount $read_cdrom_dev 2>/dev/null";  				# making sure dev isn't mounted
    787			$iso_name = "cdrx-tmp.iso";
    788			system"dd if=$read_cdrom_dev of=$write_dir_def/$iso_name bs=1024k";
    789			print "Burning temporary ISO image to CD-R/W...\n\n";
    790			chop($write_speed);
    791			system"cdrecord -v -eject dev=$cdrw_def speed=$write_speed -data $write_dir_def/$iso_name 2>/tmp/cdrx.burn.err";
    792			no_media_check();
    793			system"rm -f $write_dir_def/$iso_name";
    794			burn_complete_print();
    795		} else {
    796		print "Sorry, your \"WRITE\" directory does not have enough space to handle the temporary ISO image.\n";
    797		print "Press enter to change your directory settings...\n\n";
    798		<STDIN>;
    799		$burn_copy_cd_referrer = 2;
    800		config_temp_dir($burn_copy_cd_referrer);
    801		}
    802	}
    803		
    804	sub burn_iso_cd {
    805		my @iso_list = ();
    806		my %iso_list = ();
    807		my $i;
    808		my $j=1;
    809		my $iso_image_choice;
    810		my $write_choice;
    811		my @iso_name_split;
    812		db_handle();
    813		printbuffer();
    814		print "::: Write ISO Image to CD-R/RW :::\n\n\n";
    815		read_dir_check();
    816		db_handle();
    817		print "Your choice of ISO images include:\n\n";
    818		while (<$read_dir_def/*>) {
    819			if (/[..][iI][sS][oO]$/) {
    820				push(@iso_list,$_);
    821			}
    822		}
    823		if (@iso_list == 0) {
    824			print "There are no ISO's in your \"READ\" directory.\n";
    825			print "Press enter to change your directory settings...\n\n";
    826			<STDIN>;
    827			$burn_iso_cd_referrer = 3;
    828			config_read_dir($burn_iso_cd_referrer);
    829		}
    830	        for ($i=0; $i<@iso_list; $i++) {
    831	                $iso_list{$i} = $iso_list[$i];
    832	                print "($j) $iso_list[$i]\n";
    833	                $j++;
    834	        }
    835	        print "\n\nPlease choose one of the ISO images for writing:\n";
    836	        chomp ($iso_image_choice = <STDIN>);
    837	        $iso_image_choice =~ s/ +//;
    838	        $iso_image_choice =~ s/\D+//;
    839	        while ((!($iso_image_choice =~ /\d/)) || (!($iso_list{$iso_image_choice - 1}))) {
    840	                print "That is not a valid option, please try again:\n";
    841	                chomp ($iso_image_choice = <STDIN>);
    842	        }
    843	        printbuffer();
    844	        print "*******************************************\n";
    845	        print "You have chosen $iso_list{$iso_image_choice - 1}.\n";
    846	        print "Are you sure you wish to continue?\n\n";
    847	        print "Please enter \"Y\" to proceed with write\n";
    848	        print "or any other key to return to Main Menu\n";
    849	        print "*******************************************\n\n\n\n\n\n";
    850		chomp ($write_choice = <STDIN>);
    851	        $write_choice =~ s/ +//;
    852	        if (!($write_choice =~ /[Yy]/)) {
    853	                Menu();
    854	        } else {
    855	        printbuffer();
    856	        print "Starting CD-R/W write in 3 seconds...\n\n\n";
    857	        sleep 3;
    858		print "Burning...\n\n\n";
    859	        chop($write_speed);
    860		system"cdrecord -v -eject dev=$cdrw_def speed=$write_speed -data $iso_list{$iso_image_choice - 1} 2>/tmp/cdrx.burn.err";
    861		@iso_name_split = split(/\//,$iso_list{$iso_image_choice - 1});
    862		$iso_name = $iso_name_split[$#iso_name_split];
    863		no_media_check();
    864		burn_complete_print();
    865		}
    866	}
    867	
    868	sub burn_dir_cd {
    869		$burn_dir_cd_referrer = 5;
    870		dir_Create_iso($burn_dir_cd_referrer);
    871	}
    872	
    873	sub sub_burn_dir_cd {
    874		db_handle();
    875		print "Burning temporary ISO image to CD-R/W...\n\n";
    876		chop($write_speed);
    877		system"cdrecord -v -eject dev=$cdrw_def speed=$write_speed -data $write_dir_def/$iso_name 2>/tmp/cdrx.burn.err";
    878		no_media_check();
    879		system"rm -f $write_dir_def/$iso_name";
    880		burn_complete_print();
    881	}
    882	
    883	sub burn_files_cd {
    884		$burn_files_cd_referrer = 6;
    885		files_Create_iso($burn_files_cd_referrer);
    886	}
    887	
    888	sub sub_burn_files_cd {
    889		db_handle();
    890		print "Burning temporary ISO image to CD-R/W...\n\n";
    891		chop($write_speed);
    892		system"cdrecord -v -eject dev=$cdrw_def speed=$write_speed -data $write_dir_def/$iso_name 2>/tmp/cdrx.burn.err";
    893		no_media_check();
    894		system"rm -f $write_dir_def/$iso_name";
    895		burn_complete_print();
    896	}	
    897	
    898	sub burn_complete_print {
    899	        printbuffer();
    900	        print "******************************************\n";
    901	        print "Your CD burning is complete.\n\n";
    902	        print "Press enter to return to the Main Menu  \n";
    903	        print "******************************************\n\n\n\n\n\n";
    904	        <STDIN>;
    905	        Menu();
    906	}
    907	
    908	sub no_media_check {
    909		my $no_media;
    910		$no_media = `cat /tmp/cdrx.burn.err | grep cdrecord | grep 'output error'`;
    911		while ($no_media =~ /output error/) {
    912			printbuffer();
    913			print "It appears you have no disk, a bad disk,\nor a non-blank disk in your CD-R/W drive.\n\n";
    914			print "Please correct and press enter to try again...\n\n\n";
    915			<STDIN>;
    916			system"cdrecord -v -eject dev=$cdrw_def speed=$write_speed -data $write_dir_def/$iso_name 2>/tmp/cdrx.burn.err";
    917			$no_media = `cat /tmp/cdrx.burn.err | grep cdrecord | grep 'output error'`;
    918		}
    919		return;
    920	}
    921	
    922	
    923	##################
    924	# helper functions
    925	##################
    926	
    927	sub scanbus {
    928		my $i;
    929		my @initial_breakup_array;
    930		my $target_and_lun;
    931		my @target_and_lun;
    932		@scan = split(/\n/, `cdrecord --scanbus | grep -v [\*] | grep -A10 scsibus | grep -v scsibus`);
    933		for ($i=0; $i<@scan; $i++) {
    934			@initial_breakup_array = split(/'/, $scan[$i]);
    935			$target_and_lun = $initial_breakup_array[0];
    936			$target_and_lun =~ s/\s+//;
    937			$target_and_lun =~ s/\s.*//;
    938			@target_and_lun = split(/,/, $target_and_lun);
    939			$scsibus = $target_and_lun[0];
    940			$target = $target_and_lun[1];
    941			$lun = $target_and_lun[2];
    942			$dev_name = join(" ", $initial_breakup_array[1], $initial_breakup_array[3]);
    943			$dev_name =~ s/\'+/ /;
    944			$dev_name =~ s/\'/ /;
    945			$index_num = ($i + 1);
    946			$index_hash{$i} = [$index_num, $scsibus, $target, $lun, $dev_name];
    947		}
    948		return;
    949	}
    950	
    951	sub dotfile {
    952	        my($key, $value) = @_;
    953		my %settings;
    954	        dbmopen (%settings, "/var/db/cdrx", 0666) || die "can't open cdrx database: $!";
    955	        $settings{$key} = $value;
    956	        dbmclose (%settings);
    957	}
    958	
    959	sub db_handle {
    960		my %settings;
    961	        dbmopen (%settings, "/var/db/cdrx", 0666) || die "can't open cdrx database: $!";
    962	        $cdrom_def = $settings{"CDROM"};
    963	        $cdrom_label = $settings{"CDROMLBL"};
    964	        $cdrw_def = $settings{"CDRW"};
    965	        $cdrw_label = $settings{"CDRWLBL"};
    966	        $write_speed = $settings{"SPEED"};
    967	        $read_dir_def = $settings{"READDIR"};
    968	        $write_dir_def = $settings{"WRITEDIR"};
    969	        dbmclose (%settings);
    970	        return;
    971	}
    972	
    973	sub whoami {
    974	        my $whoami = `whoami`;
    975	        chomp $whoami;
    976	        if (!($whoami eq "root")) {
    977	                printbuffer();
    978	                print"  *********************************************************************\n";
    979	                die "  * Sorry, you must be the superuser (root) to run this script.  Bye! *\n  *********************************************************************\n\n\n\n";
    980	        }
    981	}
    982	
    983	sub check_reqs {
    984	        my @reqs= qw(cdrecord dd mkisofs);
    985	        foreach (<@reqs>) {
    986	                if (!(`which $_ 2>/dev/null`)) {
    987	                        die "\n***************************************\n$_ was not found.\n\nPlease make sure $_ is installed,\nor check your \$PATH variable.\n***************************************\n\n";
    988	                }
    989	        }
    990	}
    991	
    992	sub new_install {
    993		unless (-d("/var/db")) {
    994			mkdir("/var/db",0755) || die "cannot mkdir /var/db/: $!";
    995		}
    996		unless (</var/db/cdrx.*>) {
    997			printbuffer();
    998			print "****************************************************************\n\n";
    999			print "Welcome to the CDRX install wizard.\n\n";
   1000			print "This wizard will attempt to help you setup your initial settings.\n";
   1001			print "This includes your:\n\n";
   1002			print "\t- CDrom device\n";
   1003			print "\t- CD-R/W device\n";
   1004			print "\t- CD-R/W write speed\n";
   1005			print "\t- \"Read-from\" directory\n";
   1006			print "\t- \"Write-to\" directory\n\n\n";
   1007			print "Please press enter to continue...\n\n\n";
   1008			print "****************************************************************\n\n\n";
   1009			<STDIN>;
   1010			printbuffer();
   1011			install_handler();
   1012			Menu();
   1013		}
   1014	}
   1015	
   1016	sub install_handler { 
   1017		$install_referrer = 1;
   1018		db_handle();
   1019		SWITCH: {
   1020			if (!($cdrom_def)) { setup_cdrom($install_referrer); }
   1021			if (!($cdrw_def)) { setup_cdrw($install_referrer); }
   1022			if (!($write_speed)) { choose_speed($install_referrer); }
   1023			if (!($read_dir_def)) { config_read_dir($install_referrer); }
   1024			if (!($write_dir_def)) { config_temp_dir($install_referrer); }
   1025		}
   1026		return;
   1027	}
   1028	
   1029	sub printbuffer {
   1030	        print "\n\n\n\n\n\n\n\n\n\n\n";
   1031	        print "\n\n\n\n\n\n\n\n\n\n\n";
   1032	        print "\n\n\n\n\n\n\n\n\n\n\n";
   1033		print "\n\n\n\n\n\n\n\n\n\n\n";
   1034		print "\n\n\n\n\n\n\n\n\n\n\n";
   1035		print "\n\n\n\n\n\n\n\n\n\n\n";
   1036	}
   1037	
