#! /bin/sh /usr/share/dpatch/dpatch-run
## 30_dont_overwrite_files.dpatch by Timo Schneider <timo.schneider@s2004.tu-chemnitz.de>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: A patch which prevents overwriting files

@DPATCH@

--- a/crip
+++ b/crip
@@ -532,24 +532,30 @@
 use CDDB_get qw( get_cddb get_discids );
 
 # Check for existing *.wav, *.tag, *.ogg, *.flac or filenames.txt files
-$out = `ls *.wav *.ogg *.flac *.tag 2>/dev/null`;
-if ($out ne "") {
-	print "You currently have *.wav, *.ogg, *.flac, or *.tag files in the current\n";
-	print "working directory.  These are subject to being overwritten.\n\n";
-	print "Press <enter> to proceed (or ctrl-C to abort): ";
-	$inp = <STDIN>;
-}
-if (-e "filenames.txt") {
-	print "You currently have a filenames.txt file in the current working\n";
-	print "working directory.  This will be deleted!\n\n";
-	print "Press <enter> to proceed (or ctrl-C to abort): ";
-	$inp = <STDIN>;
-	# Go ahead and delete the file now so that if the script is stopped and
-	#  restarted this question won't keep bugging the user.
-	print "Deleting filenames.txt\n\n";
-	`rm filenames.txt`;
+
+
+#  We don't need this any more, even if there are *.wav, *.tag ... files, they won't be overwritten :-)
+#
+#
+#$out = `ls *.wav *.ogg *.flac *.tag 2>/dev/null`;
+#if ($out ne "") {
+#	print "You currently have *.wav, *.ogg, *.flac, or *.tag files in the current\n";
+#	print "working directory.  These are subject to being overwritten.\n\n";
+#	print "Press <enter> to proceed (or ctrl-C to abort): ";
+#	$inp = <STDIN>;
+#}
+#
+
+# check if "filenames.txt" already exists, if so, take filenames.1.txt...
+
+$titlefile = "filenames.txt";
+my $counter=1;
+while (-e $titlefile) {
+	$titlefile=~s/(filenames).*(txt)/$1.$counter.$2/;
+	$counter++;
 }
 
+
 # End of check components section.
 
 
@@ -1095,7 +1101,7 @@
 if ($editnames eq "on") {
 	print "Here are the computer-chosen filenames for the tracks that you've picked:\n";
 	# Create the filenames.txt file now
-	open(NAMEFILE, ">filenames.txt");
+	open(NAMEFILE, ">$titlefile");
 	$listnum = 0;
 	while ($listnum <= $#list) {
 		$index = 0;
@@ -1112,7 +1118,7 @@
 	do {
 		# Print out the filenames.txt file to the screen
 		print "-------------------------------------------------------------------------------\n";
-		open(NAMEFILE, "filenames.txt");
+		open(NAMEFILE, "$titlefile");
 		while ($line = <NAMEFILE>) {
 			# Process $line just like the filenames would be before showing them
 			chop $line;  $line =~ s/^\s*//;  $line =~ s/\s*$//;
@@ -1138,13 +1144,13 @@
 		if ($inp eq "") { $inp = $default; }
 		$inp =~ s/\s//g;
 		if ($inp =~ m/^\s*y/i) {
-			system "$editor filenames.txt";
+			system "$editor $titlefile";
 			$firstpass = 0;
 		}
 		# Process filenames.txt into $oname[][] and $name[][]
 		$broken = 0;	# Will set this to 1 if the file is discovered to be broken
 		$warning = 0;
-		open(NAMEFILE, "filenames.txt");
+		open(NAMEFILE, $titlefile);
 		$listnum = 0;
 		while ($listnum <= $#list) {
 			$index = 0;
@@ -1387,7 +1393,7 @@
 if ($editnames eq "both") {
 	print "\nHere are the filenames you put:\n";
 	# Create the filenames.txt file now
-	open(NAMEFILE, ">filenames.txt");
+	open(NAMEFILE, ">$titlefile");
 	$listnum = 0;
 	while ($listnum <= $#list) {
 		$index = 0;
@@ -1404,7 +1410,7 @@
 	do {
 		# Print out the filenames.txt file to the screen
 		print "-------------------------------------------------------------------------------\n";
-		open(NAMEFILE, "filenames.txt");
+		open(NAMEFILE, $titlefile);
 		while ($line = <NAMEFILE>) {
 			# Process $line just like the filenames would be before showing them
 			chop $line;  $line =~ s/^\s*//;  $line =~ s/\s*$//;
@@ -1430,13 +1436,13 @@
 		if ($inp eq "") { $inp = $default; }
 		$inp =~ s/\s//g;
 		if ($inp =~ m/^\s*y/i) {
-			system "$editor filenames.txt";
+			system "$editor $titlefile";
 			$firstpass = 0;
 		}
 		# Process filenames.txt into $oname[][] and $name[][]
 		$broken = 0;	# Will set this to 1 if the file is discovered to be broken
 		$warning = 0;
-		open(NAMEFILE, "filenames.txt");
+		open(NAMEFILE, $titlefile);
 		$listnum = 0;
 		while ($listnum <= $#list) {
 			$index = 0;
@@ -1484,6 +1490,57 @@
 
 print "\n\n";
 
+# At this point the user has definetly chosen the filenames he wants
+# for the tracks. We have to check if they are OK or if older files
+# exist which have the same name. They would be overwritten. This is a
+# very bad thing, so we change the "dangerous" track/filenames "foo"
+# into "foo.n" where n is the smallest possible natural positive number
+# to let the filename become unique (so that there is no old file with
+# the same name in the current working directory). But we also have to
+# take care that all the new files we create have unique names.
+# Another problem is the cdda.wav file cdparanoia creates as a default
+# output file. We don't want to mess up an older cdda.wav file lying 
+# around.
+
+# Perls -e doesn't support wildcards, but we need them for the filetest 
+# because a track named foo may bring up a bunch of files during ripping:
+# foo.wav, foo.rev.wav, foo.ogg etc.
+# And whe have to check that all the track titles on the list are unique. 
+# So we just write our own -e:
+
+sub dash_e {
+	@i = glob $_[0];
+	return 1 if (scalar @i || -e $_[0]);
+	$_[0]=~m/^(.*)\.\*/;
+	return 1 if ($unique{$1} >= 1);
+	return 0; 
+}
+%unique = ();
+$listnum = 0;
+while ($listnum <= $#list) {
+	$index = 0;
+	while ($index <= $#{$list[$listnum]}) {
+		if (dash_e("$name[$listnum][$index].*")) {
+			my $i = 1;
+			while (dash_e("$name[$listnum][$index].$i.*")) { $i++; }
+			$name[$listnum][$index] = "$name[$listnum][$index].$i";
+			$oname[$listnum][$index] = "$oname[$listnum][$index].$i";
+			$unique{"$name[$listnum][$index].$i"}++;
+		} else { $unique{"$name[$listnum][$index]"}++; }
+		$index++;
+	}
+	$listnum++;
+}
+
+# Let's take care of the cdparanoia outfile:
+
+$cdparanoia_outfile="cdda";
+if (-e "$cdparanoia_outfile.wav") {
+	$i=1;
+	while (-e "$cdparanoia_outfile.$i.wav") {$i++;}
+	$cdparanoia_outfile = "$cdparanoia_outfile.$i.wav";
+}
+
 
 # Note:  $oname[][] (original name) is the same as $name[][] with the
 #  exception that $name includes an escape "\" on characters used to
@@ -1809,7 +1866,7 @@
 			do {
 				print "\n---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n";
 				print "Ripping track ", $list[$listnum][$index], "...\n\n";
-				$cdpcmd="cdparanoia $tmpcdflags -d $cddevice $list[$listnum][$index]";
+				$cdpcmd="cdparanoia $tmpcdflags -d $cddevice $list[$listnum][$index] $cdparanoia_outfile";
 				print "Command: $cdpcmd\n";
 				$cdpout = system "$cdpcmd";
 				if ($cdpout != 0) {
@@ -1860,8 +1917,8 @@
 			# ...but there is currently no way to do it!
 
 			# Rename cdda.wav to $name[][].wav
-			print "Moving cdda.wav to $name[$listnum][$index].wav\n";
-			`mv -- cdda.wav $name[$listnum][$index].wav`;
+			print "Moving $cdparanoia_outfile to $name[$listnum][$index].wav\n";
+			`mv -- $cdparanoia_outfile $name[$listnum][$index].wav`;
 		} # End if ($skiprip eq "on)
 
 	    $index++;
@@ -2204,12 +2261,17 @@
 
 # Delete the filenames.txt file
 if ($delfiles eq "on") {
-	print "Deleting filenames.txt\n";
-	`rm filenames.txt`;
-	if (-e "filenames.txt~") {
-		print "Deleting filenames.txt~\n";
-		`rm filenames.txt~`;
-	}
+	print "Deleting $titlefile\n";
+	`rm $titlefile`;
+
+# Why are we doing this? We didn't create any *~ files, so we won't delete them.
+# If there are editors which create such files without deleting them afterwards,
+# it would be better to fix the editor than providing a dangerous workaraound.
+
+#	if (-e "$titlefile~") {
+#		print "Deleting $titlefile~\n";
+#		`rm $titlefile~`;
+#	}
 }
 
 
