#!/usr/bin/ruby

# optional argument for source commit, if not specified use master
# create_maintenance.rb # creates branch for master
# create_maintenance.rb abcdef # creates branch from commit abcdef

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

# constants for tuning script to create desired branch
BRANCH_NAME="openSUSE-13_2"
USE_IBS=false
DEVEL_PROJECT="YaST:openSUSE:13.2"
TARGET_PROJECT="openSUSE:13.2:Update"
BUILD_TARGET="openSUSE_13.2"

# start of non-configuration part
CONF_OPTIONS = {
  "obs_project"    => DEVEL_PROJECT,
  "obs_sr_project" => TARGET_PROJECT,
  "obs_target"     => BUILD_TARGET,
  "obs_api"        => USE_IBS ? "https://api.suse.de/" : "https://api.opensuse.org"
}

require "cheetah"

# 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")
  conf_line = lines.grep(/Yast::Tasks.configuration do/).first
  if conf_line
    conf_var = conf_line[/do \|\s*(\S+)\s*\|/, 1]
    line_index = lines.index(conf_line)
  else
    line_index = 2
    conf_var = "conf"
    conf_line = "Yast::Tasks.configuration do |#{conf_var}|\n"
    lines.insert(line_index, conf_line, "end\n")
  end

  CONF_OPTIONS.each do |key, value|
    config_line = lines.grep(/#{conf_var}\.#{key}\s*=/).first
    new_line = "  #{conf_var}.#{key} = #{value.inspect}\n"
    if config_line
      lines[lines.index(config_line)] = new_line
    else
      lines.insert(line_index + 1, new_line)
    end
  end

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

check_real_upstream

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

# allow to branch from non master
source_commit = ARGV.empty? ? "origin/master" : ARGV.first

#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"
