#!/usr/sbin/perl

@result=`/twc/bin/db_exp -s dif /twc/dif/wxl_dif | grep movie_00`;
###print @result;
####  "c_MM1073_movie_00",0,0,"/movies/MM1073.mv",0
####  "c_MM1074_movie_00",0,0,"/movies/MM1074.mv",0


## Loop through and obtain the token/filename

for ($i=0; $i<=$#result; $i++){
	$line = $result[$i];
	$line =~ s:"c_:: ;
	$line =~ s:_.*0,":!:;
	$line =~ s:".*::;

## Save token/filename in an array
	push (@tokfil, $line);
#ADDED
}
for ($k=0; $k<=$#tokfil; $k++){
# Strip "/" and remove any trailing space from the filename

	($token, $file) = split (/!/, $tokfil[$k]);
	chop $file;
###	print "token and files are $token, $file\n";

### Check and make sure the file doesn't exist
### if it does, delete from database
### Obtain the info for the missing file from the database
### Loop through and obtain the new tokens
### Save them in an array
### Check and see if the second new token matches the old token
### if it does, then clear them from the database
#
	if (!(-e "$file")) {
###		print "FILE $file not found\n";
###		print " Deleting!!!! CALL UPDATE CS_DELETE_MV $token\n";
		`echo \"CALL UPDATE\" | \./fire_str`;
		`echo \"CALL UPDATE CS_DELETE_MV $token\" | \./fire_str`;
###		print "Fisrt Token = $token for file  $file\n";
		

		@new=`/twc/bin/db_exp -s dif /twc/dif/wxl_dif | grep \"_product_\[0-9][0-9]" | grep $token`;
		for ($j=0 ; $j<=$#new; $j++) {
			$line = $new[$j];
			$line =~ s:"c_:: ;
			$line =~ s:_.*0,":/:;
			$line =~ s:".*::;
			push (@newtok, $line);
		}
###			print "NEW $#newtok\n";
			for ($m = 0; $m<=$#newtok; $m++) {
				($newtoken1, $newtoken2) = split(/\//, $newtok[$m]); 
				chop $newtoken2; 
			
				if ("$newtoken2" eq "$token"){
###					print "NEWTOK2 $newtoken2, TOK $token  ";
###					print "Clearing!!! CALL UPDATE CS_CLEAR $newtoken1 ,$newtoken2, and $token\n";
					`echo \"CALL UPDATE CS_CLEAR $newtoken1\" | \./fire_str`;
				} 
				else {
###					print "NO MATCH--->SECOND Token = $newtoken1, $newtoken2 and  $token\n";
				}			
			}
#				
		}
}
#####
