#!/usr/bin/env ruby

# echo-update: Script for echo-icon-theme artists to update 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 = ['master', '0.3.x', '0.3.2.x']

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

if (File.exists?("#{gitdir}/.git/config"))
  if (File.exists?("#{gitdir}/sources/generate-status-table/generate-status-table.c"))
    branches.each do |branch|
      if !run("cd #{gitdir} && git checkout #{branch}")
        puts "[W] Branch #{branch} does not exist. Creating..."
        run "cd #{gitdir} && git branch #{branch} origin/#{branch}"
      end
      run "cd #{gitdir} && git pull origin"
    end
    run "cd #{gitdir} && git checkout master"
    puts "Update complete. Git set on master branch."
  else
    $stderr.puts "[E] The given directory does not look like correct echo-icon-theme git\nrepository."
  end
else
  $stderr.puts "[E] There is no git repository in #{gitdir}"
end

