namespace :gitflow do

desc 'Gitflow setup for use with tampon'
task :prepare do
   if (`git branch -r --no-color` =~ /develop/).nil?
    #doesn't exist remotely
     puts "Creating Develop Branch"
    `git push origin origin:refs/heads/develop`
    `git fetch origin`
    `git checkout --track -b develop origin/develop`
   else
     if (`git branch --no-color` =~ /develop/).nil?
       puts "Tracking Remote Develop Branch"
       #doesn't exist locally but exists remotely
       `git checkout --track -B develop origin/develop`
     end
   end
  `git flow init -df`
  `git config gitflow.branch.develop "develop"`
  `git config gitflow.branch.master "master"`
  `git config gitflow.prefix.versiontag "v"`
  `git config gitflow.prefix.feature "feature/"`
  `git config gitflow.prefix.release "release/"`
  `git config gitflow.prefix.hotfix "hotfix/"`
  `git config gitflow.prefix.support "support/"`

end

end

namespace :tampon do

desc "Show available releases"
task :releases do 
  puts `git tag`.split("\n").compact.collect{|version| Versionomy.parse(version)}.sort.reverse
  exit
end

task :configuration do
  puts "Gitflow"
  Tampon::Configuration.ablerc.stub.generate :local
  exit
end

desc "Create Hotfix"
namespace :hotfix do
  task :start do
    Tampon::Version.base_dir = Dir.pwd
    version = Tampon::Version.load_version
    `git flow hotfix start #{version.bump(:tiny).to_s.gsub('v','')} #{version}`
    Rake::Task["version:bump:patch"].invoke
  end

  task :finish do
    Tampon::Version.base_dir = Dir.pwd
    version = Tampon::Version.load_version
    `git flow hotfix finish #{version.to_s.gsub('v')}`
  end
end

end

# from github.com/technicalpickles/jeweler/blob/master/lib/jeweler/tasks.rb namespace :version do

desc "Writes out an explicit version. Respects the following environment variables, or defaults to 0: MAJOR, MINOR, PATCH. Also recognizes BUILD, which defaults to nil"
task :write do
  version = Tampon::Version.write_version( :major => ENV['MAJOR'].to_i, :minor => ENV['MINOR'].to_i, :tiny => ENV['PATCH'].to_i, :build => (ENV['BUILD'] || nil ) )
  $stdout.puts "Updated version: #{version.to_s}"
end

desc "Show the current version"
task :current do
  Tampon::Version.base_dir = Dir.pwd
  version = Tampon::Version.load_version
  $stdout.puts "Current Version: #{version.to_s}"
end

desc "Show the next version"
task :next do
  Tampon::Version.base_dir = Dir.pwd
  version = Tampon::Version.load_version.bump :tiny
  $stdout.puts "Updated version: #{version.to_s}"
end

namespace :bump do
  desc "Bump the major version by 1"
  task :major do
    Tampon::Version.base_dir = Dir.pwd
    version = Tampon::Version.load_version.bump :major
    Tampon::Version.write_version version
    `git add VERSION`
    `git commit -m 'Version bump to #{version}'`
    $stdout.puts "Updated version: #{version}"
  end

  desc "Bump the a minor version by 1"
  task :minor do
    Tampon::Version.base_dir = Dir.pwd
    version = Tampon::Version.load_version.bump :minor
    Tampon::Version.write_version version
    `git add VERSION`
    `git commit -m 'Version bump to #{version}'`
    $stdout.puts "Updated version: #{version}"
  end

  desc "Bump the patch version by 1"
  task :patch do
    Tampon::Version.base_dir = Dir.pwd
    version = Tampon::Version.load_version.bump :tiny
    Tampon::Version.write_version version
    `git add VERSION`
    `git commit -m 'Version bump to #{version}'`
    $stdout.puts "Updated version: #{version}"
  end
end

end