view bin/old-cek @ 466:8e194cfa2f5f

[bashrc] Private paste alias
author Edho Arief <edho@myconan.net>
date Tue, 14 May 2013 16:04:09 +0900
parents 5bafb912837e
children
line wrap: on
line source

#!/usr/bin/perl

use warnings;
use strict;
use String::CRC32;
use File::Basename;

unless($#ARGV+1>0) {
	print("Usage: ", basename($0), " file1 ... fileN\n");
	exit(1);
}

my @files = @ARGV;
#or print("Usage: ", basename($0), " file1 ... fileN\n") && exit(1);
    # print usage message if called without arguments

foreach (@files) {
	my $filename = $_;
	my ($input, $name_checksum, $real_checksum, $checksum);
	unless((-r $filename) && (-f $filename)) {
		print(qq("$filename" is not a file or unreadable, skipping\n));
		next();
	}
	#print(qq(Could not find file "$filename", skipping\n)) and next() unless -r $filename;
	#print(qq("$filename" is not a file, skipping\n)) and next() unless -f $filename;

	open(FILE,$filename);
	$real_checksum = sprintf("%08X",crc32(*FILE));
	close(FILE);

	if ( /(\[|\()([0-9A-F]{8})(\]|\))/i ) { # does it have a checksum?
		$name_checksum = $2;
		if (lc($real_checksum) eq lc($name_checksum)) {
			print("${filename}: $real_checksum - OK!\n");
			next();
		}
        else {
            print("${filename}: $real_checksum - NOT OK! Should be ${name_checksum}!\n");
            next();
        }
    }
    else { # can't find checksum in filename, just print filename + generated checksum and let the user do the thinking
        print("${filename}: $real_checksum\n");
        next();
	}
}

exit(0);