#!/usr/bin/env python
# Copyright (c) 2007, Secure64 Software Corporation
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 
#
#
#	Convert a BIND named.conf file to an NSD nsd.conf file
#

#-- imports
import os
import os.path
import sys

if os.path.exists('../bind2nsd/Config.py'):
   sys.path.append('../bind2nsd')
   from Config import *
   from NamedConf import *
   from NsdConf import *
   from Utils import *
else:
   from bind2nsd.Config import *
   from bind2nsd.NamedConf import *
   from bind2nsd.NsdConf import *
   from bind2nsd.Utils import *

#-- globals
conf = Config()
DEBUG = conf.getValue('DEBUG')

#-- utility functions
def usage():
   print 'bind2nsd %s -- Copyright (c) 2007 Secure64 Software Corporation.' % \
         (conf.getValue('version'))
   print 'usage: bind2nsd'
   print '   all options are controlled by the config file'
   return

#-- main starts here ----------------------------------------------
if len(sys.argv) > 2:
   usage()
   sys.exit(1)
elif len(sys.argv) == 2 and sys.argv[1] == '-v':
   set_verbosity(True)

#-- build an in-core representation of the named.conf file
named_root  = conf.getValue('named_root')
named_fname = conf.getValue('named_conf')
report_info('=> parsing named.conf file \"%s\"...' % (named_fname))

pwd = os.getcwd()
if os.path.exists(named_root) and os.path.isdir(named_root):
   os.chdir(named_root)
else:
   bail('? er, cannot find the named root directory "%s"' % (named_root))
named = NamedConf(named_fname)
if DEBUG:
   named.dump()
os.chdir(pwd)

#-- open the nsd.conf file and write out the translated version, including
#   all of the zone files needed.  note that we're stashing everything in
#   the tmpdir as if it were a chroot dir (it simplifies the copy to our
#   server later on.
#
#   FIXME: this is not multi-user safe -- if someone runs two copies
#   with the same tmpdir, we're hosed
#
pwd = os.getcwd()
tmpdir = conf.getValue('tmpdir')
if not os.path.exists(tmpdir):
   os.makedirs(tmpdir)
os.chdir(tmpdir)

nsd_fname = conf.getValue('nsd_conf')
report_info('=> writing translated configuration to \"%s\"...' % (nsd_fname))

nsd = NsdConf(conf)
nsd.populate(named)
if DEBUG:
   nsd.dump()
nsd.write_conf()

report_info('=> writing zone files to \"%s\"...' % (tmpdir))
nsd.write_zone_files()

os.chdir(pwd)

#-- all done
sys.exit(0)

