#!/usr/bin/env ruby

# - Create maintenance branch
# - Edit Rakefile to build and submit to corresponding projects
# - Commit and push

require "cheetah"

if ARGV.size < 2
  STDERR.puts <<EOS
Bad arguments

First argument: name of the branch
Second argument: name of the target as recognized by yast-rake, see
                 https://github.com/yast/yast-rake/blob/master/data/targets.yml
Third argument (optional): source commit, if not specified use master

Examples:
#{File.basename($0)} SLE-12-SP1 sle12sp1 # creates branch for master
#{File.basename($0)} SLE-12-SP1 sle12sp1 abcdef # creates branch from commit abcdef
EOS
  exit(1)
end

BRANCH_NAME = ARGV[0]
TARGET = ARGV[1].delete(":")
SOURCE_COMMIT = ARGV[2] || "origin/master"

# by default pass output of commands to stdout and stderr
Cheetah.default_options = { :stdout => STDOUT, :stderr => STDERR }

def check_real_upstream
  res = Cheetah.run "git", "remote", "-v", :stdout => :capture
  if res.lines.grep(/origin\s*git@github.com:yast/).empty?
    raise "This script can work only on upstream clone, where upstream remote is marked as origin"
  end
end

def already_exists?
  res = Cheetah.run "git", "branch", "-r", :stdout => :capture
  res = res.lines
  return !res.grep(/origin\/#{BRANCH_NAME}/).empty?
end

def modify_rakefile
  raise "Cannot find Rakefile in pwd" unless File.exist?("Rakefile")

  lines = File.readlines("Rakefile")
  submit_to = "Yast::Tasks.submit_to"

  new_line = "#{submit_to} :#{TARGET}\n"
  line_index = lines.index {|l| l =~ /#{submit_to}/ }
  if line_index
    lines[line_index] = new_line
  else
    lines.insert(2, new_line, "\n")
  end

  File.write("Rakefile", lines.join(""))
end

check_real_upstream

if already_exists?
  puts "already exists, skipping"
  exit 0
end

#switch to master branch
Cheetah.run "git", "checkout", "master"

#create new branch ( ensure we use the latest non modified pushed version )
Cheetah.run "git", "fetch", "origin"
Cheetah.run "git", "branch", BRANCH_NAME, SOURCE_COMMIT
Cheetah.run "git", "checkout", BRANCH_NAME

modify_rakefile

commit_msg = "adapt Rakefile to submit to correct build service project in maintenance branch #{BRANCH_NAME}"

Cheetah.run "git", "commit", "-m", commit_msg, "Rakefile"

Cheetah.run "git", "push", "--set-upstream", "origin", "#{BRANCH_NAME}:#{BRANCH_NAME}"

puts "Maintenance branch properly created"
