import argparse
import socket
import time
from pathlib import Path

from clickclick import Action, info


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('from')
    parser.add_argument('to')
    args = parser.parse_args()

    hosts_file = Path('/etc/hosts')

    with hosts_file.open() as fd:
        old_contents = fd.read()

    backup_file = hosts_file.with_suffix('.local-cname-backup')
    with backup_file.open('w') as fd:
        fd.write(old_contents)

    try:
        while True:
            entries = []

            with Action('Resolving {} ..'.format(args.to)):
                results = socket.getaddrinfo(args.to, 80, type=socket.SOCK_STREAM)
                for result in results:
                    family, type, proto, canonname, sockaddr = result
                    if family in (socket.AF_INET, socket.AF_INET6):
                        ip = sockaddr[0]
                        entries.append((getattr(args, 'from'), ip))

            info('Current entries:')
            for hostname, ip in entries:
                info('{} -> {}'.format(hostname, ip))

            with Action('Writing {} ..'.format(hosts_file)):
                with hosts_file.open('w') as fd:
                    fd.write(old_contents)
                    fd.write('#### Start of entries generated by local-cname\n')
                    for hostname, ip in entries:
                        fd.write('{} {}\n'.format(ip, hostname))

            time.sleep(60)
    except KeyboardInterrupt:
        # ignore, do not print stacktrace
        pass
    finally:
        backup_file.rename(hosts_file)
