Parses list of directories ('args') and attaches to purrlogs appropriately.
    'mainwin' is a Purr.MainWindow object.

    Return value:
      * if modal=True: True if arguments parse OK and Purr.MainWindow should be shown, False on error.
      * if modal=False: True if attached to purrlog and Purr.MainWindow should be shown, False otherwise.

    If modal=False, it will keep displaying the startup dialog as a modeless dialog, and attach the
  mainwin to the specified purrlog when the dialog is closed.

    Use cases:
     $ purr <dirname> [<more dirs>]
        1. If dirname does not exist:
          * find other purrlogs in parent dir
          * pop up wizard, with the name as the default "create" option, and create selected
        2. If dirname exists:
          2a. Not a directory: report error
          2b. If dir is a purrlog, attach and add dirs
          2c. If dir contains 1 valid purrlog, attach, and add more dirs.
          2d. If dir contains 2+ purrlogs, offer choice (or create new), add more dirs
          2e. If dir contains no purrlogs, offer to create one (or to attach to one), add "more dirs" to watchlist if creating new, ignore if attaching
     $ purr
         * same as "purr ."
     $ from meqbrowser, when cd into a directory:
         * if purr dialog is visible, do same as "purr ."
     $ from meqbrowser, when purr button is pressed and purr is not yet visible
         * do "purr ."
     $ from Calico, when an MS is selected:
         * do purr MS.purrlog .

def startWizard(args, mainwin, modal=True):
    """
    Parses list of directories ('args') and attaches to purrlogs appropriately.
    'mainwin' is a Purr.MainWindow object.

    Return value:
      * if modal=True: True if arguments parse OK and Purr.MainWindow should be shown, False on error.
      * if modal=False: True if attached to purrlog and Purr.MainWindow should be shown, False otherwise.

    If modal=False, it will keep displaying the startup dialog as a modeless dialog, and attach the
  mainwin to the specified purrlog when the dialog is closed.

    Use cases:
     $ purr <dirname> [<more dirs>]
        1. If dirname does not exist:
          * find other purrlogs in parent dir
          * pop up wizard, with the name as the default "create" option, and create selected
        2. If dirname exists:
          2a. Not a directory: report error
          2b. If dir is a purrlog, attach and add dirs
          2c. If dir contains 1 valid purrlog, attach, and add more dirs.
          2d. If dir contains 2+ purrlogs, offer choice (or create new), add more dirs
          2e. If dir contains no purrlogs, offer to create one (or to attach to one), add "more dirs" to watchlist if creating new, ignore if attaching
     $ purr
         * same as "purr ."
     $ from meqbrowser, when cd into a directory:
         * if purr dialog is visible, do same as "purr ."
     $ from meqbrowser, when purr button is pressed and purr is not yet visible
         * do "purr ."
     $ from Calico, when an MS is selected:
         * do purr MS.purrlog .
    """

    args = args or [os.getcwd()]
    dirname = os.path.abspath(args[0])
    moredirs = args[1:]

    # case 1: dirname does not exist, or refers to a non-directory
    if not os.path.exists(dirname) or not os.path.isdir(dirname):
        message = """To begin with, PURR must load an existing purrlog, or start a new purrlog. <tt>%s</tt> does not look to be an existing purrlog.
            What would you like to do?""" % Kittens.utils.collapseuser(dirname)
        create = dirname
        dirname = os.getcwd()
        parent = os.path.dirname(os.path.normpath(create)) or os.getcwd()
        # if parent is valid dir, find purrlogs in parent (to offer as an option)
        if os.path.isdir(parent):
            purrlogs = list(filter(Purr.Purrer.is_purrlog, glob.glob(os.path.join(parent, "*"))))
        # else use "." as dirname, and do not offer any purrlogs
        else:
            purrlogs = []
    # case 2: dirname exists:
    else:
        create = None
        # case 2b: is a valid purrlog
        if Purr.Purrer.is_purrlog(dirname):
            mainwin.attachPurrlog(dirname, moredirs)
            mainwin.show()
            return True
        # case 2c-2e. Look for purrlogs in dirname
        purrlogs = list(filter(Purr.Purrer.is_purrlog, glob.glob(os.path.join(dirname, "*"))))
        # case 2c: exactly one purrlog. Attach without asking.
        if len(purrlogs) == 1:
            mainwin.show()
            mainwin.attachPurrlog(purrlogs[0], moredirs)
            return True
        # else setup messages
        if purrlogs:
            message = """To begin with, PURR must load an existing purrlog, or start a new purrlog. The directory <tt>%s</tt> contains
          several purrlogs. What would you like to do?""" % Kittens.utils.collapseuser(dirname)
        else:
            message = """To begin with, PURR must load an existing purrlog, or create a new purrlog. The directory <tt>%s</tt> contains
          no purrlogs. What would you like to do?""" % Kittens.utils.collapseuser(dirname)

    # case 1, 2d or 2e: make wizard dialog

    # kill old wizard, if showing
    global wizard_dialog
    if wizard_dialog:
        wizard_dialog.hide()
        dum = QWidget()
        wizard_dialog.setParent(dum)
        dum = wizard_dialog = None

    # create new wizard
    wizard_dialog = PurrStartupWizard(mainwin, dirname, purrlogs, moredirs=moredirs, create=create, message=message)

    if modal:
        if wizard_dialog.exec_() == QDialog.Rejected:
            return False
        return True;
    else:
        wizard_dialog.setModal(False)
        wizard_dialog.show()
        return False