#!/usr/bin/ruby
if ARGV.include? '--help'
  puts "usage: ./configure [--prefix <dir>] [--no-strip] [--release]"
  exit
end

cwd = File.dirname( __FILE__ )
require "#{cwd}/admin/platform.rb"
require "#{cwd}/admin/which_qmake.rb"
require "#{cwd}/admin/utils.rb"

begin
  h1 'Welcome to liblastfm configure'
  $qmake = which_qmake
  puts 'Using '+`which #{$qmake}` unless Platform::IMPL == :mswin

  h2 'Determining installation prefix' do
    if ARGV.include? '--prefix'
      n=ARGV.index '--prefix'
      ENV['LFM_PREFIX'] = ARGV[n+1]
    end
    ENV['LFM_PREFIX'] = '/usr/local' if ENV['LFM_PREFIX'].nil?
    if File.exists? ENV['LFM_PREFIX'] and !File.directory? ENV['LFM_PREFIX']
      abort "Installation prefix exists but isn't a directory: "+ENV['LFM_PREFIX']
    end
    puts "Will install to: "+ENV['LFM_PREFIX']
  end

  h1 'Generating Build System'

  h2 'Generating .qmake.env' do
    f = File.new('.qmake.env', 'w')
    f.write qmake_env('CC', 'QMAKE_CC')
    f.write qmake_env('CXX', 'QMAKE_CXX')
    f.write qmake_env('LDFLAGS', 'QMAKE_LFLAGS_RELEASE')
    f.write qmake_env(['CFLAGS', 'CPPFLAGS'], 'QMAKE_CFLAGS_RELEASE')
    f.write qmake_env(['CXXFLAGS', 'CPPFLAGS'], 'QMAKE_CXXFLAGS_RELEASE')
    f.write "QMAKE_CXXFLAGS_RELEASE = -O3 -fomit-frame-pointer\n" if `whoami` == "mxcl\n" #OHAI!
    f.close
  end unless Platform::IMPL == :mswin

  h2 "Running qpp..." do
    Dir.chdir('src') { `ruby ../admin/qpp lastfm.pro` }
    Dir.chdir('src/fingerprint') { `ruby ../../admin/qpp fingerprint.pro` }
  end

  h2 "Configuring qmake..." do
    args=Array.new
    if ARGV.include? '--release'
      args << '-config release'
      args << '"CONFIG += app_bundle"' if Platform::IMPL == :macosx and ARGV.include? '--bundle'
    else
      args << '-config debug'
    end
    if ARGV.include? '--no-strip'
      args << '"CONFIG += nostrip"'
    end
    ENV['LFM_QMAKE'] = "#{$qmake} #{args.join(' ')}"
  end

  h2 "Generating Makefile..." do
    hs = Array.new
    hs << 'global.h'
    hs << 'core/UrlBuilder.h' << 'core/XmlQuery.h' << 'core/misc.h'
    hs << 'fingerprint/Fingerprint.h' << 'fingerprint/FingerprintableSource.h'
    hs << 'radio/RadioStation.h' << 'radio/RadioTuner.h'
    hs << 'scrobble/Audioscrobbler.h' << 'scrobble/Scrobble.h' << 'scrobble/ScrobblePoint.h'
    hs << 'types/Track.h types/Mbid.h' << 'types/Artist.h' << 'types/Album.h' << 'types/FingerprintId.h' << 'types/Playlist.h' << 'types/Tag.h' << 'types/User.h types/Xspf.h'
    hs << 'ws/ws.h' << 'ws/InternetConnectionMonitor.h' << 'ws/NetworkAccessManager.h'
    
    File.new('Makefile', 'w').write `ruby admin/Makefile.rb #{hs.join(' ')}`
  end

  puts
  puts 'Good, your configure is finished! Now type: make'
  
rescue QMakeTooOld
	puts <<-sput
  Your version of Qt seems to be too old, we require Qt 4.4 or above.

  It is possible you have Qt3 and Qt4 both installed. Locate your Qt4
  installation and ensure it is placed first in the path, eg:

  	PATH=/opt/qt4/bin:\$PATH ./configure

  sput
	exit 1
rescue QMakeNotFound
  puts "Sorry, qmake was not found, is Qt4 installed?"
  exit 2
end
