#!/usr/bin/env ruby
require 'unirest'
require 'os'
require 'rubygems/package'
require 'open-uri'
require 'zlib'
require 'micro_install'
# This script installs micro.
#
# This script will install micro to your user bin directory ~/.local/bin
# (I don't use ''~/bin' because it muddies up 'ls')
#
# Found a bug? Report it here: https://github.com/IotaSpencer/micro-install
#
# Acknowledgments:
#   - https://getmic.ro: https://github.com/benweissmann/
#   - Micro, of course: https://micro-editor.github.io/
#   - Loosely based on the Chef curl|bash: https://docs.chef.io/install_omnibus.html
#   - ASCII art courtesy of figlet: http://www.figlet.org/
hl = HighLine.new($stdin, $stderr)
installer = MicroInstall::Installer.new
installer.latesttag
installer.get_arch
installer.download_url
installer.download_micro_tar
installer.extract_micro
installer.install_micro

puts <<-EOM
Micro has been installed to your ~/.local/bin/ directory. You can run it with:
'micro'
EOM
paths = []
path_env = ENV['PATH'].split(':').each do |path|
  paths << path if path.include? 'bin'
end
if paths.include? Pathname(Dir.home).join('.local/bin').to_path
  hl.say "Environment Variable 'PATH' already includes #{Dir.home}/.local/bin"
  hl.say Paint["You're all set!", 'green']
else
  hl.say "#{Paint['Warning', 'orange']}: #{Dir.home}/.local/bin is not in $PATH,"
  hl.say "Either add '#{Dir.home}/.local/bin' to the PATH variable, or give the full path to the executable"
end