#!/usr/bin/env ruby
require 'httparty'
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

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 or 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']
  installer.is_installed
else
  hl.say "#{Paint['Warning', 'orange']}: #{Dir.home}/.local/bin is not in $PATH,"
  installer.is_installed_but_no_bin
end
if installer.arch == 'linux-arm'
  note = <<~NOTE
  #{Paint['NOTE','orange']}: Termux, while being the only linux subsystem
  for android that micro_install supports, is a bit wonky when it
  comes to shebangs and Bash startup.
    
  1. If you haven't already, create a '.bashrc' in '~'
  2. Add the next line into your bashrc after everything else
     having to do with paths.

     export PATH="#{Dir.home}/.local/bin:$PATH"

  3. Restart your bash session, or re-source your .bashrc
  
      'source ~/.bashrc'


  NOTE
  hl.say note
end