--- rewrite_eeprom_v1_8	2009-03-07 19:19:15.000000000 -0300
+++ ./rewrite_eeprom_v1_9	2009-03-10 17:20:32.000000000 -0300
@@ -22,13 +22,13 @@
 #  The solution for it is to reprogram the eeprom with their original contents.
 #
 #  Modules this script is known to work with: em28xx and saa7134 
+#  * Not tested against newer em28xx chipsets like the em2874 and em2884
 #
 ########################################################
 # NOTE                                                 #
 ################################################################################
 # Since the script will try to detect the proper i2c bus address, it will only #
 # work well if you have just one V4L device connected.                         #
-#									       #
 ################################################################################
 #
 ########################################
@@ -84,47 +84,114 @@
 # After running the script, your original contents should be restored. Try to
 # remove it and reinsert to see if it will be properly detected again.
 #
-#
 use Switch;
 
 $argv = $ARGV[0];
 
+# Files 
 $file_eeprom = "/tmp/eeprom-original.txt";
 $file_bus = "/tmp/i2c-detect-bus.txt";
 $file_addr = "/tmp/i2c-detect-addr.txt";
 $file_i2c_tmp = "/tmp/i2ctmp.txt";
 
+# Bus  
 $businfo = "";
 $addrinfo = "";
 
+# Modules 
 $modules = "em28xx|saa713";
+$_  = $modules;
+tr/|/ /d;
+s/saa713/saa7134/g;
+$modules_str = $_;
 
+# eeprom stuff
+$eeprom_backup_dir = "~/.eeprom";
 $NR_EEPROM_REGS = 0;
 
+##########################
+# Subroutines            #
+##########################
+
+sub build_script
+{
+	my $script_prevent;
+
+	# Building script 
+
+	$script_prevent  = "#!/bin/bash\n";
+	$script_prevent .= "#\n";
+	$script_prevent .= "# Notes:\n";
+	$script_prevent .= "# - i2c_dev module should be loaded\n";
+	$script_prevent .= "# - v4l driver should be loaded\n\n";
+
+
+	$script_prevent .= "if [ ! \"\$UID\" = \"0\" ]; then\n\techo \"You must run this script as root\";\n\texit;\nfi\n\n";
+
+	$script_prevent .= "i2cset &> /dev/null\n";
+	$script_prevent .= "if [ \$\? != \"1\" ]; then\n";
+	$script_prevent .= "\techo \"Please install i2c-tools package before continue.\";\n";
+	$script_prevent .= "\texit;\n";
+	$script_prevent .= "fi\n\n";
+
+	$script_prevent .= "modprobe i2c-dev\n";
+	$script_prevent .= "if [ \$\? != \"0\" ]; then\n";
+	$script_prevent .= "\techo \"Can't load i2c-dev module.\";\n";
+	$script_prevent .= "\texit;\n";
+	$script_prevent .= "fi\n\n";
+
+	$script_prevent .= "if [ \$\? != \"0\" ]; then\n";
+	$script_prevent .= "\techo \"Can't locate eeprom in $argv.\";\n";
+	$script_prevent .= "\texit;\n";
+	$script_prevent .= "fi\n\n";
+
+	$script_prevent .= "clear;\n";
+	$script_prevent .= "echo \"";
+	$script_prevent .=  "\n\033[1;31m#     #     #     ######   #     #  ###  #     #   #####\n";
+	$script_prevent .= "#  #  #    # #    #     #  ##    #   #   ##    #  #     #\n";  
+	$script_prevent .= "#  #  #   #   #   #     #  # #   #   #   # #   #  #      \n"; 
+	$script_prevent .= "#  #  #  #     #  ######   #  #  #   #   #  #  #  #  ####\n";  
+	$script_prevent .= "#  #  #  #######  #   #    #   # #   #   #   # #  #     #\n";  
+	$script_prevent .= "#  #  #  #     #  #    #   #    ##   #   #    ##  #     #\n";  
+	$script_prevent .= " ## ##   #     #  #     #  #     #  ###  #     #   #####\033[0m\n\n";   
+
+
+	$script_prevent .= "This tool is *ONLY RECOMMENDED* in cases of: LOST or CORRUPTED EEPROM data.\n";
+	$script_prevent .= "Otherwise:\n\nYOU MAY *LOST* THE CURRENT EEPROM FROM YOUR DEVICE AND IT WILL MAKE YOUR BOARD ";
+	$script_prevent .= "*DO NOT WORK* UNTIL YOU SET THE RIGHT EEPROM AGAIN!\n\n";
+	$script_prevent .= "If you have *any doubt*, BEFORE run it contact people from: linux-media\@vger.kernel.org\n";
+
+	$script_prevent .= "Are you \033[1;31mABSOLUTELY SURE\033[0m to continue? (yes or not)\";\n\n";
+	$script_prevent .= "read confirmation;\n";
+	$script_prevent .= "if \[ \"\$confirmation\" != \"yes\" \]; then\n";	
+	$script_prevent .= "\techo \"process aborted\";\n";
+	$script_prevent .= "\texit;\n";
+	$script_prevent .= "fi\n\n";
+
+	$script_prevent .= "lsmod | egrep -e \"$modules\" &> /dev/null\n";
+	$script_prevent .= "if [ \$\? != \"0\" ]; then\n";
+	$script_prevent .= "\techo \"Aborting script.. None of the supported driver $modules_str are loaded. Did you forget to connect the device?\";\n";
+	$script_prevent .= "\texit;";
+	$script_prevent .= "\nfi\n\n";
+
+	print $script_prevent;
+}
+
 sub check_user
 {
 	if ($>) { 
 		die "You must run this program as root\n"; 
 	}
-	my $st = system("modprobe i2c-dev");
-	if ($st < 0) {
-		die("Can't load i2c-dev module.");
-	}
-}
-sub check_pkg
-{
-	my $st = system("i2cset &> /dev/null");
-	if ($st < 0) {
-		die "Please install i2c-tools package before continue.";
-	}
+
 }
 
 sub get_bus_and_addr
 {
 
 	my $lines = 0;
+	my $output = "#!/bin/bash\n";
 
-	system("i2cdetect -l | egrep -e \"$modules\" | cut -b 5 > $file_bus"); 
+	system("i2cdetect -l | egrep -e \"$modules\" | cut -b 5 > $file_bus 2> /dev/null"); 
 
 	# Checking number if lines 
 	open(FILE, $file_bus) or die "Can't open `$file_bus': $!";
@@ -135,54 +202,77 @@
 
 	switch ($lines) {
 		case 0 {
-			print "Could not detect i2c bus from any device!\n";
-			exit();
+			$output .= "echo \"Could not detect i2c bus from any device! Did you connect the device?\";\n";
+			$output .= "echo \"Modules supported: $modules_str\";\n";
+			$output .= "exit;";
+			print $output;
 		}
 		case 1 {
 			# Starting script
-			print "#!/bin/bash\n";
-			print "#\n";
-			print "# Note:\n";
-			print "# - i2c_dev module should be loaded (check if lsmod)\n";
-			print "# - v4l driver should be loaded \n\n";
+			&build_script;
 		}
 		else {
-			print "humm, I got too many bus, please connect or plug just a hardware peer time!\n";
-			print "Read a note inside the script!\n";
-			exit();
+			$output .= "humm, I got too many bus, please connect or plug just a hardware peer time!\n";
+			$output .= "Read a note inside the script!\n";
+			$output .= "exit;";
+			print $output;
 		}
 	}
 
 	# Reading BUS from temporary file
 	open (FILE, $file_bus);
 	while (<FILE>) {
-		chomp;
 		$businfo = "$_";
+		chomp($businfo);
 	}
 	close FILE;
 
-	system("i2cdetect -y $businfo > $file_i2c_tmp");
+	system("i2cdetect -y $businfo > $file_i2c_tmp 2> /dev/null");
 	system("awk \'NR==7\' $file_i2c_tmp | cut -d \' \' -f 2 > $file_addr");
+
 	# Reading BUS from temporary file
 	open (FILE, $file_addr);
 	while (<FILE>) {
-		chomp;
 		$addrinfo = "$_";
+		chomp($addrinfo);
 	}
 
 	if($addrinfo eq "--") {
-		print("echo \"\n**** Failed to recognize bus address!\n**** Aborted..\n\"");
-		exit();
+		print "\necho \"**** Failed to recognize bus address! Run again rewrite_eeprom tool with the right dump file, aborted!\";\n";
+		print "exit;";
 	}
+
+	# Double check
+	$bkp_eeprom  = "i2cdetect -y $businfo > $file_i2c_tmp 2> /dev/null;\n";
+	$bkp_eeprom .= "BUSCHECK=\`awk \'NR==7\' $file_i2c_tmp | cut -d \' \' -f 2\`;\n";
+	$bkp_eeprom .= "rm -f $file_i2c_tmp;\n";
+	$bkp_eeprom .= "if [ \$BUSCHECK == \"--\" ]; then\n";
+	$bkp_eeprom .= "\t echo \"Aborting script.. I cannot make backup of your current eeprom.. It's not safe to continue!\";\n"; 
+	$bkp_eeprom .= "\t exit;\n";
+	$bkp_eeprom .= "fi\n\n";
+
+	# Backup 
+	$bkp_eeprom .= "\nDATE=\`/bin/date +%Y%m%d_%I%M%S\`\n";
+	$bkp_eeprom .= "echo \"\nMaking backup of current eeprom - dir [$eeprom_backup_dir/eeprom-\$DATE]\";\n";
+	$bkp_eeprom .= "mkdir -p $eeprom_backup_dir\n";
+	$bkp_eeprom .= "i2cdump -y $businfo 0x$addrinfo > $eeprom_backup_dir/eeprom-\$DATE 2> /dev/null\n";
+	$bkp_eeprom .= "if [ \$\? != \"0\" ]; then\n";
+	$bkp_eeprom .= "\t echo \"Aborting script.. I cannot make backup of your current eeprom.. It's not safe to continue!\";\n"; 
+	$bkp_eeprom .= "\t exit;";
+	$bkp_eeprom .= "\nfi\n";
+	print $bkp_eeprom;
+
 	close FILE;
 }
 
-sub print_script 
+sub print_i2c 
 {
-	open (INPUT, "$file_eeprom") or die "Can't open data file: $!";
+	$cmd = "cat $argv | egrep \"eeprom [0-9a-f]\"| sed -e \"s/.*eeprom/eeprom/\" | cut -d ' ' -f 3-22 > $file_eeprom";
+	system($cmd);
 
-	# Reading eeprom dump argument
+	open (INPUT, "$file_eeprom") or die "Can't open data file: $!";
 
+	# Reading dump 
 	@eeprom = "";
 	my $eeprom_pos = 0;
 	while (!eof(INPUT)) {
@@ -195,11 +285,14 @@
 	close INPUT;
 
 	if ($NR_EEPROM_REGS == 0) {
-		print("echo \"\n**** Failed to recognize any dump in: $argv!\n**** Aborted..\n\"");
-		exit();
+		print "\necho \"**** Failed to recognize any dump in: $argv! Run again rewrite_eeprom tool with the right dump file, aborted..\";\n";
+		print "exit;";
 	}
 
-	printf("\necho \"\nStarting recording new eeprom... \n\n[DO NOT REMOVE YOUR DEVICE UNTIL THE UPDATE IS FINISHED]\n\";\n");
+	print "\n\necho \"\033[1;31m\n[DO NOT REMOVE YOUR DEVICE UNTIL THE UPDATE IS FINISHED]\033[0m\";\n";
+	print "echo \"Press ENTER to start\";\n";
+	print "read\n";
+
 	for ($i=0; $i < $NR_EEPROM_REGS; $i++) {
 		printf("i2cset -y $businfo 0x$addrinfo 0x%02x $eeprom[$i] b\n", $i);
 	}
@@ -207,10 +300,21 @@
 	printf("\necho \"\nDone! Remove and re-insert your device to see if it will be properly detected again. :-)\n\";\n");
 }
 
-# Main 
+################
+# Main         #
+################
+
 &check_user;
 
 if (@ARGV <= 0) {
+
+	my $em28xx_note = "\n\033[1;31m\nNOTES\033[0m:\n\t Not tested against newer em28xx chipsets like the em2874 and em2884\n";
+
+	print "\033[1;31m\nWARNING\033[0m:\n \t This script can *\033[1;31mDAMAGE\033[0m* your board, if you are not sure how to use it, *DO NOT* run it\n"; 
+	print "\t Current modules supported: $modules_str *\033[1;31mONLY\033[0m*";
+	print $em28xx_note;
+	print "\t If you have *any doubt*, \033[1;31mBEFORE run it\033[0m contact people from: linux-media\@vger.kernel.org\n";
+
 	print "\nUsage:\n";
 	print "\tshell>./rewrite_eeprom ./dmesg-dump-eeprom > eeprom_script.sh\n";
 	print "\tshell>sh ./eeprom_script.sh\n\n";
@@ -223,21 +327,10 @@
 }
 
 # Calling sub routines
-&check_pkg;
 &get_bus_and_addr;
+&print_i2c;
 
-# Building body of script 
-
-print "if [ ! \"\$UID\" = \"0\" ]; then\n\techo \"You must run this script as root\";\n\texit;\nfi\n\n";
-
-$cmd = "cat $argv | egrep \"eeprom [0-9a-f]\"| sed -e \"s/.*eeprom/eeprom/\" | cut -d ' ' -f 3-22 > $file_eeprom";
-system($cmd);
-
-# Printing i2c commands
-&print_script;
-
-# Removing old files
-
+# Removing tmp files
 system("rm -f $file_bus");
 system("rm -f $file_addr");
 system("rm -f $file_i2c_tmp");

