#!/usr/bin/perl5
#
# catscan.pl -- by Andrew Stellman <roo@razorwire.com>
#
# Quick and dirty script to deocode cuecat barcode scans
# <http://www.cuecat.com/>
#
# From http://uscan.sourceforge.net/upc.txt:
#
# divide the UPC into 4 groups of three digits
# use the scheme below to translate each digit into its output
# 
#     1  2  3
# 
# 0   C3 n  Z  
# 1   CN j  Y  
# 2   Cx f  X  
# 3   Ch b  W  
# 4   D3 D  3  
# 5   DN z  2  
# 6   Dx v  1  
# 7   Dh r  0  
# 8   E3 T  7  
# 9   EN P  6 
#
# See http://www.jounce.net/~maarken/ for more details about the encoding
# scheme used and for more source.


%translation = ('C3', 0, 'n', 0, 'Z', 0, 'CN', 1, 'j', 1, 'Y', 1, 'Cx', 2, 
		'f', 2, 'X', 2, 'Ch', 3, 'b', 3, 'W', 3, 'D3', 4, 
		'D', 4, '3', 4, 'DN', 5, 'z', 5, '2', 5, 'Dx', 6, 'v', 
		6, '1', 6, 'Dh', 7, 'r', 7, '0', 7, 'E3', 8, 'T', 8, 
		'7', 8, 'EN', 9, 'P', 9, '6', 9);

%types = ('cGf2', 'ISBN', 'cGen', 'ISBN', 'fHmg', 'UPC');

while(<>) {
    
    exit if $_ eq "\n";

    if (!/^\.(.*)\.(....)\.(.*)\.$/) {
	print "invalid code\n";
	exit;
    }


    $id = $1;
    $type = $2;
    $code = $3;

    $output = "";
    while ($code =~ /^(..)(.)(.)(.*)/) {
	$output = $output . $translation{$1};
	$output = $output . $translation{$2};
	$output = $output . $translation{$3};
	$code = $4;
    }


    if ($types{$type} ne "") {
	print "$types{$type} ";
    }

    print "$output\n";

}
