require "juicer"
require "juicer/install/base"
require "zip/zip"
require "openssl"

module Juicer
  module Install
    #
    # Install and uninstall routines for the YUI Compressor.
    # Installation downloads the YUI Compressor distribution, unzips it and
    # storesthe jar file on disk along with the license.
    #
    class YuiCompressorInstaller < Base
      def initialize(install_dir = Juicer.home)
        super(install_dir)
        @latest = nil
        @href = nil
        @website = 'https://github.com/yui/yuicompressor/downloads'
        @cdn = 'https://github.com'
      end

      #
      # Install the Yui Compressor. Downloads the distribution and keeps the jar
      # file inside PATH/yui_compressor/bin and the README and CHANGELOG in
      # PATH/yui_compressor/x.y.z/ where x.y.z is the version, most recent if
      # not specified otherwise.
      #
      # Path defaults to environment variable $JUICER_HOME or default Juicer
      # home
      #
      def install(version = nil)
        version = super(version)
        base = "yuicompressor-#{version}"
        filename = download(@cdn+@href)
        target = File.join(@install_dir, path)

        Zip::ZipFile.open(filename) do |file|
          file.extract("#{base}/doc/README", File.join(target, version, "README"))
          file.extract("#{base}/doc/CHANGELOG", File.join(target, version, "CHANGELOG"))
          file.extract("#{base}/build/#{base}.jar", File.join(target, "bin", "#{base}.jar"))
        end
      end

      #
      # Uninstalls the given version of YUI Compressor. If no location is
      # provided the environment variable $JUICER_HOME or Juicers default home
      # directory is used.
      #
      # If no version is provided the most recent version is assumed.
      #
      # If there are no more files left in INSTALLATION_PATH/yui_compressor, the
      # whole directory is removed.
      #
      def uninstall(version = nil)
        super(version) do |dir, version|
          File.delete(File.join(dir, "bin/yuicompressor-#{version}.jar"))
        end
      end

      #
      # Check which version is the most recent
      #
      def latest
        return @latest if @latest
        webpage = Nokogiri::HTML(open(@website, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read)
        a = webpage.css('#manual_downloads h4 a').first
        @href = a['href']
        @latest = a.text.match(/\d\.\d\.\d/)[0]
      end
    end
  end
end
