#!/usr/bin/env python

from systematic.shell import Script
from seine.dns.tld import TLDCache, DNSCacheError

USAGE = """
Update a copy valid TLD names on local disk and query validity of TLD names.
You can give multiple names to check on command line.

Note: besides updating local cache, this is just a simple demonstration of the
TLD cache code. It should be normally used from other modules."""

script = Script(description=USAGE)
script.add_argument('-u', '--update', action='store_true', help='Download latest version')
script.add_argument('-f', '--cache-file', help='TLD cache file')
script.add_argument('names', nargs='*', help='TLD names to validate')
args = script.parse_args()

try:
    cache = TLDCache(path=args.cache_file)
    cache.load()
    if args.update:
        cache.update()

except DNSCacheError, emsg:
    script.exit(1, emsg)

for name in args.names:
    name = name.strip('.')
    if name in cache:
        print 'Valid TLD: %s' % name
    else:
        print 'Invalid TLD: %s' % name

