#!/usr/bin/env ruby

# echo-set-up: Script for echo-icon-theme artists to set up local git repository
#
# Copyright (C) 2008, Martin Sourada
# 
# This script is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this script; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

require "ftools"

branches = ['0.3.x', '0.3.2.x']
giturl = "ssh://git.fedorahosted.org/git/echo-icon-theme"

def run(cmd)
  puts "\n$ #{cmd}"
  return system(cmd)
end

gitdir = "#{ARGV[0]}"
if gitdir == ""
  gitdir ="~/Projects/echo-icon-theme"
end
gitdir = File.expand_path gitdir

while File.exist?(gitdir)
  puts "Directory #{gitdir} already exists."
  puts "Please set another (empty for ~/Projects/echo-icon-theme):"
  gitdir = $stdin.gets.delete "\n"
  if gitdir == ""
    gitdir = "~/Projects/echo-icon-theme"
  end
  gitdir = File.expand_path gitdir
end

if run("git clone #{giturl} #{gitdir}")
  branches.each do |branch|
    if !run("cd #{gitdir} && git branch #{branch} origin/#{branch}") 
      $stderr.puts "\n[E] Failed to create local branches. Consult above messages for more info."
    end
  end

  puts "\nSet an e-mail address to be associated with yout commits:"
  puts "(If you leave it empty, current setting will be shown instead)"
  email = $stdin.gets.delete "\n"
  run "cd #{gitdir} && git config user.email #{email}"

  puts "\nSet a name to be associated with your commits:"
  puts "(If you leave it empty, current setting will be shown instead)"
  name = $stdin.gets.delete "\n"
  if name == ""
    cmd = "cd #{gitdir} && git config user.name"
  else
    cmd = "cd #{gitdir} && git config user.name \"#{name}\""
  end
  run(cmd)
  
  run "cd #{gitdir} && git checkout master"

  puts "\nCreated new echo-icon-theme git repository in\n#{gitdir}"

  puts "\nE-mail address used for commits:"
  system "cd #{gitdir} && git config user.email"

  puts "\nName used for commits:"
  system "cd #{gitdir} && git config user.name"

  puts "\nLocal branches:"
  system "cd #{gitdir} && git branch"
else
  $stderr.puts "\n[E] Failed to clone repository. Consult above messages for more info."
end
