#!/usr/bin/env python3

# Copyright (c) 2020, G.A. vd. Hoorn
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# author: G.A. vd. Hoorn


"""
CLI tools for interacting with a Fanuc Robotics controller.

Usage: dominh [--no-upload] [--version] [--help] <command> [<args>...]

Options:
  -h --help       Show this screen.
  -n --no-upload  Don't upload the helpers (for instance because they have
                  already been uploaded by a previous invocation).
  --version       Print version and exit.

Available commands:

   reset          Attempt to reset the controller
   read           Read from supported IO ports
   write          Write to supported IO ports
   get            Retrieve the value of a scalar system variable

See 'dominh <command> --help' for more information on a specific command.

"""

from docopt import docopt
import sys

import dominh


if __name__ == '__main__':
    args = docopt(__doc__, help=True, options_first=True,
        version=f'dominh {dominh.__version__}')
    argv = [args['<command>']] + args['<args>']

    # print(args)

    skip_upload = args['--no-upload']

    if args['<command>'] == 'reset':
        from dominh.tool import dominh_reset
        dominh_reset.main(argv=argv)

    elif args['<command>'] == 'read':
        from dominh.tool import dominh_read
        dominh_read.main(argv=argv, skip_upload=skip_upload)

    elif args['<command>'] == 'write':
        from dominh.tool import dominh_write
        dominh_write.main(argv=argv, skip_upload=skip_upload)

    elif args['<command>'] == 'get':
        from dominh.tool import dominh_get
        dominh_get.main(argv=argv, skip_upload=skip_upload)

    else:
        sys.stderr.write(f"'{args['<command>']}' is not a dominh command.\n{__doc__}")
