#!/usr/bin/perl -w
#
# Joshua D. Abraham < jabra @ spl0it.org > 
# GPLv2 
# 
# Simple script to lookup the useragent and print the description 
#
# UserAgent database: http://techpatterns.com/downloads/firefox/useragentswitcher.xml
#
# Example:
# $ wget http://techpatterns.com/downloads/firefox/useragentswitcher.xml
# $ ua_lookup.pl "w3m/0.5.1"
# w3m 0.5.1 (Linux)
#
use strict;
use XML::LibXML;

# grab a local version of the User Agent DB and access it.
my $file = 'useragentswitcher.xml';

# If you want to use the url, that is fine too. 
# Just comment the line above and uncomment the next line.
#my $file = 'http://techpatterns.com/downloads/firefox/useragentswitcher.xml';

my $parser = XML::LibXML->new();

my $doc = $parser->parse_file($file) or die "Error: can't open $file\n";
my $ua = $ARGV[0] or die "Usage: $0 [user agent]\n";

my $xpc = XML::LibXML::XPathContext->new($doc);
my @nodes = $xpc->findnodes('//useragentswitcher/useragent[@useragent=' . "'" . $ua . "'" . ']');
if (scalar(@nodes) > 0) {
    print $nodes[0]->getAttribute('description') . "\n";
}

