#!/usr/bin/ensim-python
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
#
# This function adds a virtual domain
#

import getopt
import pdb
import sys
import traceback

from vh3 import virthost

import ensimapplpath
import be_vherrordisp

if __name__ == '__main__':
    # checks to see if we are in maintenance mode
    virthost.checkMaintenance()

    status = be_vherrordisp.CLIError.SUCCESS
    status_list = []
    
    domain, errs = virthost.create_new_domain()
    status = virthost.determine_action_severity(errs)
    if status != virthost.ERROR:
        sys.argv.append(domain)

        status_list.extend(errs)
        
        try:
            args = virthost.parse_args(sys.argv[1:],virthost.ADD_VIRT_DOMAIN)
        except virthost.ParseArgsExc, e:
            virthost.delete_virtual_domain(domain)
            virthost.unlock_domain(domain)
            sys.exit(e.code)
        conf_dicts = args[domain]
        try:
            for service in virthost.get_ordered_service_list():
                errs = virthost.write_conf_file(domain, service,
                                                conf_dicts[service],
                                                conf_dicts)
                status_list.extend(errs)

            status = virthost.cli_display_status_list(status_list)

            if status == be_vherrordisp.CLIError.ERROR:
                errlist = virthost.del_new_conf_files(domain)
                virthost.cli_display_status_list(errlist)                
                delstat = virthost.delete_virtual_domain(domain)
                virthost.cli_display_status_list(delstat)
            else:
                editstat = virthost.add_virtual_domain(domain)
                status = virthost.cli_display_status_list(editstat)
                if status == be_vherrordisp.CLIError.ERROR:
                    delstat = virthost.delete_virtual_domain(domain)
                    virthost.cli_display_status_list(delstat)
        except:
            try:
                status = be_vherrordisp.CLIError.ERROR
                edisp = be_vherrordisp.CLIError()
                edisp.push_traceback(sys.exc_info()[0])
                status = virthost.cli_display_status(edisp)

                delstat = virthost.delete_virtual_domain(domain)
                virthost.cli_display_status_list(delstat)
            except:
                traceback.print_exc()
        virthost.unlock_domain(domain)
    else:
        status = virthost.cli_display_status_list(errs)
    sys.exit(status)
