text: #! python
  import argparse
  from pathlib import Path
  import json
  from bids.grabbids import BIDSLayout
  import subprocess
  import os
  import shutil
  
  parser = argparse.ArgumentParser(description='Autogenerate mindcontrol settings from a BIDS directory')
  parser.add_argument('--server', action = 'store_true', 
                      help='Start a simple file server in the bids directory.')
  parser.add_argument('--meteor', action = 'store_true', 
                      help='Start the meteor server that will serve mindcontrol.')
  parser.add_argument('bids_dir', help='The directory with the input dataset '
                      'formatted according to the BIDS standard.')
  
  args = parser.parse_args()
  bids_dir = args.bids_dir
  file_server = "http://127.0.0.1:3002/"
  
  # Default settings for each module
  default_module = {
  "fields": [
    {
      "function_name": "get_qc_viewer",
      "id": "name",
      "name": "Image File"
    },
    {
      "function_name": "get_qc_ave_field",
      "id": "average_vote",
      "name": "QC vote"
    },
    {
      "function_name": None,
      "id": "num_votes",
      "name": "# votes"
    },
    {
      "function_name": None,
      "id": "quality_check.notes_QC",
      "name": "Notes"
    }
  ],
  "metric_names": None,
  "graph_type": None,
  "staticURL": file_server,
  "usePeerJS": False,
  "logPainter": False,
  "logContours": False,
  "logPoints": True,
  "qc_options": {"pass": 1, "fail": 1, "needs_edits": 0, "edited": 0, "assignTo": 0, "notes": 1, "confidence": 1}}
  
  layout = BIDSLayout(bids_dir)
  
  # Loop through files, adding them to the manifest
  # add entity types to a set to be used to populate the
  # autogenerated settings
  manifest = []
  entry_types = set()
  for img in layout.get(extensions = ".nii.gz"):
      img_dict = {}
      img_dict["check_masks"] = [img.filename.replace(bids_dir,"")]
      entry_types.add(img.type)
      img_dict["entry_type"] = img.type
      img_dict["metrics"] = {}
      img_dict["name"] = os.path.split(img.filename)[1].split('.')[0]
      img_dict["subject"] = 'sub-' + img.subject
      img_dict["session"] = 'ses-' + img.session
      manifest.append(img_dict)
  
  # Populate modules list
  modules = []
  for et in entry_types:
      et_module = default_module.copy()
      et_module["name"] = et
      et_module["entry_type"] = et
      modules.append(et_module)
  
  # autogenerated settings files
  pub_set = {"startup_json": file_server+"startup.auto.json",
             "load_if_empty": True,
             "use_custom": True,
             "needs_consent": False,
             "modules": modules}
  settings = {"public": pub_set}
  
  # Write out file list to bids directory
  with open(os.path.join(bids_dir,"startup.auto.json"), "w") as h:
      json.dump(manifest,h)
  # Write out the settings file to the mindcontrol directory
  with open("settings.auto.json", "w") as h:
      json.dump(settings,h)
  
  if args.server:
      shutil.copy2("start_static_server.py",bids_dir)
      subprocess.Popen("python start_static_server.py", cwd = bids_dir, shell = True)
  if args.meteor:
      meteor = subprocess.Popen("meteor --settings settings.auto.json".split(' '))
      meteor.wait()