#!/usr/bin/env python3

import json
import os
import sys
import config as cfg

import v1
import v2
import fs

def print_help():
  print("""
Help: %s

Arguments: <path/to/dir>

Options:
  -c, --config: Specify the configuration file to use
                If specified, it will create a file .lego-spec in the directory refering to this one.
                If not specified, it expects to find a file .lego-spec in the target directory    
  -h, --help: Prints this message
""" % (os.path.basename(sys.argv[0])))

def parse_args(args):
  i = 0
  options = {}
  params = []
  while i < len(args):
    arg = args[i]
    if arg == '--':
      i += 1
      break
    if arg == '-h' or arg == '--help':
      options['help'] = True
    elif arg == '-c' or arg == '--config':
      i += 1
      options['config'] = args[i]
    elif arg[0] == '-':
      raise ValueError(f"Unrecognized option {arg}")
    else:
      params.append(arg)

    i += 1
  
  params += args[i:]

  return [params, options]

[args, options] = parse_args(sys.argv)

if 'help' in options:
  print_help()
  exit(0)

def get_version(config):
  if 'version' in config:
    return config['version']
  else:
    return 1

virtual_dir_path = args[1]
if 'config' in options:
  v2.create_redirection(options['config'], virtual_dir_path, fs.SYSTEM)

config = cfg.read_default(virtual_dir_path, fs.SYSTEM)

version = get_version(config)
if version == 1:
  v1.process(virtual_dir_path, config, fs.SYSTEM)
elif version == 2:
  v2.process(virtual_dir_path, config, fs.SYSTEM)
else:
  raise RuntimeError(f"Unsupported version {version}")
