#! /usr/bin/perl -w

#--------------------------------------------------------------------------
# Check registration of domain names
#--------------------------------------------------------------------------

use strict;
use File::Basename;
use Getopt::Long;


# Normal TLD that anybody can use
use constant NORMAL => 'N';

# Restricted TLD reserved for certain types of organizations
use constant RESTRICTED => 'R';

# Government TLD that are impossible to acquire, and also seemingly
# difficult to even whois query
use constant IMPOSSIBLE => 'I';


my @tld;
while (<DATA>) {
    my ($status, $tld, $desc) = /(\S*)\s*(\S*)\s*(.*)/;
    push @tld, { tld => $tld, status => $status, desc => $desc };
}

my @normalTld = map {$_->{status} eq NORMAL ? $_->{tld} : ()} @tld;
my @specialTld = map {$_->{status} eq RESTRICTED ? $_->{tld} : ()} @tld;

my ($prefix, @domains, %opts);
my $basename = basename $0;
my $usage = <<USAGE;
$basename - Check registration of domain names

usage:
    $basename [-a] PREFIX
    $basename -t TLD1,TLD2,... PREFIX
    $basename -f DOMAIN
    $basename -l
    $basename -h

options:
    -a, --all      Check the prefix against all NORMAL and RESTRICTED TLDs
    -f, --full     Treat the arg as a full domain name and check it alone
    -l, --list     List the TLDs and their descriptions
    -H, --help     This help information
    -t, --tlds     Check this explicit list of TLDs
    -V, --version  Output version info and exit

Default is to check through all NORMAL TLDs, which are:
.biz, .com, .info, .name, .net, .org, .us
USAGE
my $version = <<VER;
$basename 1.0.2
Written by Dino Morelli  < dino\@ui3.info >
VER


GetOptions(\%opts,
    '--all',
    '--full',
    '--list',
    '--tlds=s',
    '--help|H',
    '--version|V',
) or die "$usage\n";

die "$usage\n" if $opts{help};
die "$version\n" if $opts{version};

if ($opts{list}) {
    print <<INFO;
Partial listing of Top Level Domains
N (normal) means you can register this. R (restricted) means only certain
organizations are allowed. I is not only impossible to get, it's apparently
also not searchable with whois.

INFO

    printf "%-3s%-10s%-s\n", $_->{status}, $_->{tld}, $_->{desc}
        for (@tld);

    print "\nMore information can be found here: ";
    print "http://en.wikipedia.org/wiki/TLD\n";

    exit;
}

$prefix = shift;
die "Must supply a prefix or domain\n\n$usage\n" unless $prefix;

if ($opts{full}) {
    push @domains, $prefix;
}
elsif ($opts{tlds}) {
    my @tld = split /,/, $opts{tlds};
    @domains = map { $prefix . "." . $_ } @tld;
}
else {
    my @tld = @normalTld;
    push @tld, @specialTld if $opts{all};
    @domains = map { $prefix . $_ } @tld;
}

for my $domain (@domains) {
    printf "%2sregistered: %s\n", 
        ((`whois "$domain"` =~ /Domain Name:/) ?  "" : "UN"),
        $domain;
}

__DATA__
R   .aero   air-transport industry
R   .arpa   address and routing parameter area
N   .biz    business
N   .com    commercial
R   .coop   cooperatives
I   .edu    educational
I   .gov    United States government
N   .info   information
R   .int    international organizations
I   .mil    United States military
R   .museum museums
N   .name   individuals, by name
N   .net    network
N   .org    organization
R   .pro    professions
R   .travel travel and travel agency related
N   .us     United States of America
