#!/usr/bin/ruby.ruby2.5 
# encoding: UTF-8

# Copyright 2014 Shawn Neal <sneal@sneal.net>
#
# 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.

# rubocop:disable all

$LOAD_PATH.push File.expand_path('../../lib', __FILE__)

require 'readline'
require 'io/console'
require 'winrm'

def help_msg
  puts 'Usage: rwinrm user@host'
  puts ''
end

def parse_options
  options = {}
  fail 'Missing required options' unless ARGV.length == 1

  m = /^(?<user>[a-z0-9\.\!\$ _-]+)@{1}(?<host>[a-z0-9\.\-]+)(?<port>:[0-9]+)?/i.match(ARGV[0])
  fail "#{ARGV[0]} is an invalid host" unless m
  options[:user] = m[:user]
  options[:endpoint] = "http://#{m[:host]}#{m[:port] || ':5985'}/wsman"

  # Get the password
  print 'Password: '
  options[:pass] = STDIN.noecho(&:gets).chomp
  puts

  # Set some defaults required by WinRM WS
  options[:auth_type] = :plaintext
  options[:basic_auth_only] = true

  options
rescue StandardError => e
  puts e.message
  help_msg
  exit 1
end

def repl(options)
  client = WinRM::WinRMWebService.new(
    options[:endpoint],
    options[:auth_type].to_sym,
    options)

  client.set_timeout(3600)
  shell_id = client.open_shell
  command_id = client.run_command(shell_id, 'cmd', "/K prompt [#{ARGV[0]}]$P$G")

  read_thread = Thread.new do
    client.get_command_output(shell_id, command_id) do |stdout, stderr|
      STDOUT.write stdout
      STDERR.write stderr
    end
  end
  read_thread.abort_on_exception = true

  while (buf = Readline.readline('', true))
    if buf =~ /^exit/
      read_thread.exit
      client.cleanup_command(shell_id, command_id)
      client.close_shell(shell_id)
      exit 0
    else
      client.write_stdin(shell_id, command_id, "#{buf}\r\n")
    end
  end
rescue Interrupt
  puts 'exiting'
  # ctrl-c
rescue WinRM::WinRMAuthorizationError
  puts 'Authentication failed, bad user name or password'
  exit 1
rescue StandardError => e
  puts e.message
  exit 1
end

repl(parse_options)

# rubocop:enable all
