#!/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 edits a service plan
#
# Usage:
#
# Undefined
#
# Example:
#
# Undefined

# XXX NOTE THIS CLI DOES NOT SUPPORT MANAGEMENT RESELLER LEVEL PLANS

import getopt
import pdb
import sys
import traceback
from vh3 import virthost
import ensimapplpath
import be_vherrordisp

if __name__ == '__main__':
    status = be_vherrordisp.CLIError.SUCCESS
    basedir = None
    try:
        args = virthost.parse_args(sys.argv[1:],virthost.EDIT_PLAN)
    except virthost.ParseArgsExc, e:
        sys.exit(e.code)

    for plan in args.keys():
        virthost.lock_plan(plan,None)
        conf_dicts = args[plan]
        status_list = []
        try:
            if virthost.plan_exists(plan,None):
                for service in virthost.get_ordered_service_list():
                    errs = virthost.write_plan_file(plan, service,
                                                    conf_dicts[service],
                                                    conf_dicts,
                                                    None)
                    status_list.extend(errs)
               
                status = virthost.cli_display_status_list(status_list)

                if status != be_vherrordisp.CLIError.ERROR:
                    editstat = virthost.edit_plan(plan,basedir)
                    virthost.cli_display_status_list(editstat)
        except:
            status = be_vherrordisp.CLIError.ERROR
            try:
                edisp = be_vherrordisp.CLIError()
                edisp.push_traceback(sys.exc_info()[0])
                status = virthost.cli_display_status(edisp)
            except:
                traceback.print_exc()
        virthost.unlock_plan(plan,None)
    sys.exit(status)
