#!/usr/bin/python

import os
import sys
import argparse
import configobj

# parse command line options
parser = argparse.ArgumentParser(
        description='Hosts builder for py-i2phosts.',
        epilog='Report bugs to https://github.com/i2phosts/py-i2phosts/issues')
parser.add_argument('-c', '--config', default='/etc/py-i2phosts/builder.conf', dest='config_file',
        help='config file to use')
parser.add_argument('-f', '--file',
        help='write hosts into specified file')
parser.add_argument('-d', '--debug', action='store_true',
        help='write debug messages to stdout')
args = parser.parse_args()

# read config
spec = '''
    hostsfile = string(default=None)
    '''
spec = spec.split('\n')
config = configobj.ConfigObj(args.config_file, configspec=spec, file_error=True)
if 'include' in config:
    config_included = configobj.ConfigObj(config['include'])
    config.merge(config_included)

# django setup
DJANGO_SETTINGS_MODULE = 'pyi2phosts.settings'
if 'DJANGO_PROJECT_PATH' in config:
    DJANGO_PROJECT_PATH = config['DJANGO_PROJECT_PATH']
else:
    DJANGO_PROJECT_PATH = os.path.dirname(sys.argv[0]) + '/..'
sys.path.insert(1, DJANGO_PROJECT_PATH)
os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
import django
django.setup()
from pyi2phosts.postkey.models import i2phost
from pyi2phosts.lib.utils import validate_config

# validate config
validate_config(config)

# result hosts.txt
if args.file:
    hostsfile = args.file
elif config['hostsfile'] != None:
    hostsfile = config['hostsfile']
else:
    sys.stderr.write('Please specify "-f" or define "hostsfile" in config\n')
    sys.exit(1)

f = open(hostsfile, 'w')
# get activated hosts
qs = i2phost.objects.filter(activated=True)
# select theirs name and base64 hash
l = qs.values('name', 'b64hash')
# write final hosts.txt-format file
for entry in l:
    f.write(entry['name'] + '=' + entry['b64hash'] + '\n')
f.close()