#!/usr/bin/perl -w

use strict;
use Encode; 
require Encode::Detect; #additionaly installed

if(@ARGV != 1)
{
    print "Usage: perl $0 <file to analyze>\n";
    exit;
}

my @files = glob($ARGV[0]);

foreach my $file (@files)
{
    open(FILE,$file);
    my $content = do { local $/; <FILE>};
    if($content) {
	my $enc= Encode::Detect::Detector::detect($content);
		if($enc) {
			print $enc,"\n";
		}
		else {
		     $content=~s/\n//g;
		     if ($content =~ /charset=['\"]?(\S*)['\"]/i) {
	             print "[charset] $file:\t",$1,"\n";
	         }
	         else {
	         print "Encoding of file $file can't be guessed\n";
            }
		}
	}
    else {
		print "Cannot read from file $file\n";
    }
}

close(FILE);