#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
# Adapted from the Homebrew installer: http://brew.sh

SF_PREFIX = '/Library/Fonts'
SF_REPO = 'https://github.com/supermarin/YosemiteSanFranciscoFont'

module Tty extend self
  def blue; bold 34; end
  def white; bold 39; end
  def red; bold 31; end
  def reset; escape 0; end
  def bold n; escape "1;#{n}" end
  def underline n; escape "4;#{n}" end
  def escape n; "\033[#{n}m" if STDOUT.tty? end
end

class Array
  def shell_s
    cp = dup
    first = cp.shift
    cp.map{ |arg| arg.gsub " ", "\\ " }.unshift(first) * " "
  end
end

def ohai *args
  puts "#{Tty.blue}==>#{Tty.white} #{args.shell_s}#{Tty.reset}"
end

def warn warning
  puts "#{Tty.red}==>#{Tty.reset} #{warning.chomp}"
end

def system *args
  abort "Failed during: #{args.shell_s}" unless Kernel.system(*args)
end

def getc  # NOTE only tested on OS X
  system "/bin/stty raw -echo"
  if STDIN.respond_to?(:getbyte)
    STDIN.getbyte
  else
    STDIN.getc
  end
ensure
  system "/bin/stty -raw echo"
end

def wait_for_user
  puts
  ohai "Press RETURN to continue or any other key to abort."
  c = getc
  # we test for \r and \n because some stuff does \r instead
  abort unless c == 13 or c == 10
end

def wait_for_reinstall
  ohai "Press RETURN to reinstall or any other key to abort."
  c = getc
  # we test for \r and \n because some stuff does \r instead
  abort unless c == 13 or c == 10
end

module Version
  def <=>(other)
    split(".").map { |i| i.to_i } <=> other.split(".").map { |i| i.to_i }
  end
end

def macos_version
  @macos_version ||= `/usr/bin/sw_vers -productVersion`.chomp[/10\.\d+/].extend(Version)
end

#######################################################################
abort "San Francisco is only available on OS X 10.10 Yosemite." if /linux/i === RUBY_PLATFORM
abort "San Francisco is only available on OS X 10.10 Yosemite." if macos_version < "10.10"
abort "Don't run this as root!" if Process.uid == 0
abort <<-EOABORT unless `groups`.split.include? "admin"
This script requires the user #{ENV['USER']} to be an Administrator. If you
still want to use this script set your user to be an Administrator in
System Preferences or `su' to a non-root user with Administrator privileges.
EOABORT

unless Dir["#{SF_PREFIX}/System\ San\ Francisco*"].empty?
  puts
  ohai "It appears San Francisco System is already installed."
  puts
  warn "If you want to uninstall, run this command:"
  warn "sudo rm -rf #{SF_PREFIX}/System\\ San\\ Francisco*"
  puts
  wait_for_reinstall if STDIN.tty?
  system "sudo -v"
  ohai "Preparing the old version for update..."
  system "/usr/bin/sudo chown #{ENV['USER']}:staff /Library/Fonts/System\\ San\\ Francisco*"
end

if Dir["#{SF_PREFIX}/System\ San\ Francisco*"].empty?
  puts
  ohai "This script will install:"
  puts
  puts Dir.glob('*.ttf').join("\n")
  wait_for_user if STDIN.tty?
  system "/usr/bin/sudo -v"
end

Dir.chdir SF_PREFIX do
  ohai "Downloading and installing San Francisco System..."
  system "/bin/bash -o pipefail -c '/usr/bin/curl -fsSL #{SF_REPO}/tarball/master | /usr/bin/tar xz -m --strip 1 --include='*System*' --exclude='*Text*''"
  ohai "Setting file ownership..."
  system "/usr/bin/sudo chown root:wheel /Library/Fonts/System\\ San\\ Francisco*"
  ohai "Clearing OS X Font Cache..."
  system "sudo /usr/bin/atsutil databases -remove"
  ohai "Repairing Disk Permissions (Just to be safe)..."
  system "diskutil repairPermissions /"
end

ohai "Installation successful!"
puts
warn "Next steps:"
warn "Restart your computer for the changes to take effect."
warn "Need help? Visit #{SF_REPO}"
