#!@PYTHON@

# Copyright (C) 2010  Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import sys; sys.path.append ('@@PYTHONPATH@@')
import re, getopt
import isc.datasrc
import isc.util.process
from isc.datasrc.master import MasterFile
import time
import os

isc.util.process.rename()

#########################################################################
# usage: print usage note and exit
#########################################################################
def usage():
    print("Usage: %s [-d <database>] [-o <origin>] <file>" % sys.argv[0], \
          file=sys.stderr)
    exit(1)

#########################################################################
# main
#########################################################################
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "d:o:h", \
                                                ["dbfile", "origin", "help"])
    except getopt.GetoptError as e:
        print(str(e))
        usage()
        exit(2)

    dbfile = '@@LOCALSTATEDIR@@/@PACKAGE@/zone.sqlite3'
    initial_origin = ''
    for o, a in opts:
        if o in ("-d", "--dbfile"):
            dbfile = a
        elif o in ("-o", "--origin"):
            if a[-1] != '.':
                a += '.'
            initial_origin = a
        elif o in ("-h", "--help"):
            usage()
        else:
            assert False, "unhandled option"

    if len(args) != 1:
        usage()
    zonefile = args[0]
    verbose = os.isatty(sys.stdout.fileno())
    try:
        master = MasterFile(zonefile, initial_origin, verbose)
    except Exception as e:
        sys.stderr.write("Error reading zone file: %s\n" % str(e))
        exit(1)

    try:
        zone = master.zonename()
        if verbose:
            sys.stdout.write("Using SQLite3 database file %s\n" % dbfile)
            sys.stdout.write("Zone name is %s\n" % zone)
            sys.stdout.write("Loading file \"%s\"\n" % zonefile)
    except Exception as e:
        sys.stdout.write("\n")
        sys.stderr.write("Error reading zone file: %s\n" % str(e))
        exit(1)

    try:
        isc.datasrc.sqlite3_ds.load(dbfile, zone, master.zonedata)
        if verbose:
            master.closeverbose()
            sys.stdout.write("\nDone.\n")
    except Exception as e:
        sys.stdout.write("\n")
        sys.stderr.write("Error loading database: %s\n"% str(e))
        exit(1)

if __name__ == "__main__":
    main()
