require 'mxx_ru/abstract_target'
require 'mxx_ru/ex'

require File.join( File.dirname( __FILE__ ), 'paths' )

module ACE_wrappers

class ACELibUnpackerTarget < MxxRu::AbstractTarget
  include Paths

  class InvalidDistribEx < MxxRu::Ex
  end

  def initialize( prj_alias )
    super( prj_alias )

    check_and_unpack
  end

  def build
    MxxRu::TargetState.new( MxxRu::TargetState::EXISTS )
  end

  def clean
    # Nothing to do!
  end

protected
  def check_and_unpack
    MxxRu::show_brief( "checking for #{RELATIVE_ACE_ROOT} existence" )

    if !File.directory?( RELATIVE_ACE_ROOT )
      distribs = Dir[ 'ace_lib_distrib/ACE-*-lib.tar.bz2' ]
      raise InvalidDistribEx.new(
          "Must be only one ACE-*-lib.tar.bz2 archive in 'ace_lib_distrib' " +
          "subdir. Found #{distribs.size} archives" ) if
          1 != distribs.size

      Dir.chdir( 'ace' ) do
        archive_to_unpack = File.join( '..', distribs[ 0 ] )
        MxxRu::AbstractTarget.run(
            [ "tar jxf #{archive_to_unpack}" ], [],
            "unpacking #{archive_to_unpack}" )
      end
    end
  end
end

end # module ACE_wrappers

# vim:ts=2:sts=2:sw=2:expandtab

