#!/usr/cisco/bin/perl
#use Getopt::Long ;

our($url, $singleQuote, $inputFile, $inHelp) ;

$url         = q(http://standards.ieee.org/develop/regauth/oui/oui.txt) ;
$inputFile   = q(/etc/device-update/oui.txt) ;
$singleQuote = "\047"; 

sub showUsage (){
    print <<END_USAGE ;
Usage: $0 [ --input=<file> ]

Notes:
  1. If --input is not used, oui.txt will be downloaded from $url
  2. transformed output will be printed on STDOUT
  
END_USAGE
    exit(127) ;
}

## MAIN
#&GetOptions("input|i=s" => \$inputFile, "help|h" => \$inHelp) or die("Error: getopt() failed") ;

&showUsage() if(defined($inHelp)) ;

print "# -*- coding: utf-8 -*-" ;

if(-f $inputFile){
    print "## Using \"$inputFile\" for input...\n" ;
}else{
    print "## Downloading OUI data...\n" ;
    print "## NOTE:\n" ;
    print "## NOTE: Use $0 --input=<file> to use custom input file\n" ;
    print "## NOTE:\n" ;
    die("Error: Unable to download \"$url\"; $!")
        unless(system("/bin/rm -f oui.txt 2>/dev/null; wget $url >/dev/null 2>&1") == 0) ;
    $inputFile = "./oui.txt" ;    
}

open(TEXT, "< $inputFile") or die("Error: Unable to open oui.txt for READ") ;
printf "oui_dict = [\n";
while(<TEXT>){
  if(/([0-9A-F]+)\s*\(base 16\)\s*(.+)$/){ 
    ($hex, $str) = ($1, $2); 
    chomp($str);
    next if($str =~ /^\s*$/) ; #ignore empty strings
    $hex = sprintf("0x%06s", $hex) ;
    $hex =~ s/'/\\\'/g ;  
    $hex =~ s/"/\\\"/g ;  
    $str =~ s/'/\\\'/g ;  
    $str =~ s/"/\\\"/g ;  
    $str =~ s/\n//g ;
    $str =~ s/\r//g ;

    printf "(u\"%s\", %s%s%s),\n", $str, $singleQuote, $hex, $singleQuote ;
  }
}
printf "]";
close(TEXT) ;
exit(0) ;
