#!/usr/bin/python
# PYTHON_ARGCOMPLETE_OK
"""
fetch is a tool for performing common operations with Fetch and Freight.

Copyright 2015 Fetch Robotics Inc.
Author: Alex Henning
"""

import argcomplete
import argparse
import os
import re
import sys

from fetch_tools.commands import run, push, pull, create_account, lint, workspace_status, debug_snapshot

commands = [
    create_account,
    push,
    pull,
    run,
    lint,
    workspace_status,
    debug_snapshot,
]

root = argparse.ArgumentParser(description="Fetch Tool")

command_parser = root.add_subparsers(
    title="Call `fetch COMMAND -h` for help on each command listed below",
    dest="command",
    metavar="COMMAND",
)
command_parser.required = True

for command in commands:
    parser = command_parser.add_parser(command.name, help=command.help_text)
    command.add_arguments(parser)
    parser.set_defaults(main=command.main)

argcomplete.autocomplete(root)
args = root.parse_args()

# Load custom default values for some variables

# If user isn't specified, default to $FETCH_USER if set. Else, use $USER
if "user" in args and args.user is None:
    args.user = os.getenv("FETCH_USER")
    if args.user is None:
        args.user = os.getenv("USER")

# If robot isn't specified, default to $FETCH_ROBOT if set.  Else,
# infer from $ROS_MASTER_URI
if "robot" in args and args.robot is None:
    args.robot = os.getenv("FETCH_ROBOT")
    if args.robot is None:
        args.robot = re.findall("//(.*?):", os.getenv("ROS_MASTER_URI"))[0]
        if args.robot == "localhost":
            print("ERROR: Must specify robot with --robot ROBOT or set "
                  "$ROS_MASTER_URI to point at the desired robot.")
            sys.exit(1)
if "robot" in args and args.robot in [None, ""]:
    print("ERROR: Must specify robot with --robot ROBOT or set "
          "$ROS_MASTER_URI to point at the desired robot.")
    sys.exit(1)

# If workspace isn't specified, default to $FETCH_WORKSPACE if
# set. Else, infer from $HOME and $ROS_DISTRO.
if "workspace" in args and args.workspace is None:
    args.workspace = os.getenv("FETCH_WORKSPACE")
    if args.workspace is None:
        args.workspace = os.getenv("HOME")+"/"+os.getenv("ROS_DISTRO")
    if args.workspace.endswith("/"):
        args.workspace = args.workspace[:-1]
if "remote_workspace" in args and args.remote_workspace is None:
    args.remote_workspace = os.getenv("FETCH_REMOTE_WORKSPACE")
    if args.remote_workspace is None and args.workspace is not None:
        args.remote_workspace = args.workspace.replace(os.getenv("HOME"), "~")
    if args.remote_workspace.endswith("/"):
        args.workspace = args.remote_workspace[:-1]

# Run the command
args.main(args)
