#!/usr/bin/env ruby
#--
# Copyright (C) 2008 Harald Sitter <apachelogger@ubuntu.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License or (at your option) version 3 or any later version
# accepted by the membership of KDE e.V. (or its successor approved
# by the membership of KDE e.V.), which shall act as a proxy
# defined in Section 14 of version 3 of the license.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#--

require 'bat'

ary =   [
            "kdebindings", "kdelibs", "kdeaccessibility", "kdeadmin", "kdeartwork",
            "kdebase", "kdebase-runtime", "kdebase-workspace", "kdeedu", "kdegames",
            "kdegraphics", "kdemultimedia", "kdenetwork", "kdepim", "kdepimlibs",
            "kdeplasma-addons", "kdesdk", "kdetoys", "kdeutils", "kdewebdev"
        ]

extrary = ["koffice", "amarok"]

def branch(c)
    if File.exist?(c)
        wrn "Not branching #{c} ... directory already exists..."
        return
    end

    pt "Branching #{c} :-)"
    Bzr.new(k_m_branch_url_constructor(c),c).branch

    pt "Downloading tarball for #{c}"
    Dir.chdir(c)
    PullLP.new(name_converter(c,false,true)).get_tar
    Dir.chdir("..")
end

def commit(c)
    pt "Comitting #{c} :-)"
    Bzr.new(k_m_branch_url_constructor(c),c,true).commit
end

def push(c)
    pt "Pushing #{c} :-)"
    Bzr.new(k_m_branch_url_constructor(c),c,true).push
end

def merge(c)
    pt "Pulling #{c} :-)"
    Bzr.new(k_m_branch_url_constructor(c),c,true).merge
end

def list(ary)
    ary.each do | x |
        puts x
    end
end

def task(t,c)
    case t
    when "branch" then branch(c)
    when "commit" then commit(c)
    when "push" then push(c)
    when "merge" then merge(c)
    else
        err "#{t} isn't a valid operation. Not for gypsy anyway."
    end
end

# p $*
p ary.count

if $*[0]
    if $*[1] == "all"
        (Dir.mkdir("KDE") unless File.exist?("KDE")) if $*[0] == "branch"
        Dir.chdir("KDE") unless File.basename(Dir.pwd) == "KDE"

        ary.each{|c|
            task($*[0],c)
        }
    elsif $*[1]
        if ary.include?($*[1]) or extrary.include?($*[1])
            task($*[0],$*[1])
        else
            err("#{$*[1]} is not listed as supported product.")
        end
    elsif $*[0] == "list"
        list(ary)
        list(extrary)
    end
else
    pt "gypsy [branch|merge|commit|push] [PACKAGENAME|all]"
end
