#!/usr/bin/env ruby
#--
# Copyright (C) 2008-2009 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'
require 'kioclient'

@kio = KIOClient.new

def update_standards
    file = File.new("./debian/control", File::RDWR)
    str = file.read
    if not str =~ /Standards-Version: #{SV}/
        file.rewind
        file.truncate( 0 )
        file << str.gsub(/Standards-Version: .*/, "Standards-Version: #{SV}" )
        system("dch -a 'Bump Standards-Version to #{SV}'")
    end
    file.close
end

def bump_deps
    file = File.new("./debian/control", File::RDWR)
    str = file.read
    file.rewind
    file.truncate( 0 )
    str = str.gsub(/kdelibs5 \(>= ([\d\.\:]+)\)/,"kdelibs5 (>= #{UBUVER})")
    str = str.gsub(/kdelibs5-dev \(>= ([\d\.\:]+)\)/,"kdelibs5-dev (>= #{UBUVER})")
    file << str
    file.close
    system("dch -a 'Bump version requirements of kdelibs5(-dev) to #{UBUVER}'")
end

def exe(lFile)
    Dir.chdir(@langDir)

    lang, subs = name_converter(lFile, true)
    if subs
        cp(lFile, lFile.gsub(subs, lang))
        lFile = lFile.gsub(subs, lang)
        oLang = "kde-l10n-#{subs}"
    end

    isoLang   = lFile.split("-")[2]
    version   = lFile.split("-")[3].split(".")[0..2].join(".")
    if $*[0] and $*[0].include?(BD)
        uVersion  = version + "-0ubuntu1~intrepid1~ppa1"
    else
        uVersion  = version + "-0ubuntu1"
    end
    lang  = lFile.split("-")[0..2].join("-")
    oLang = lang unless oLang
    dir   = oLang + "-" + version

    file        = lang + "_" + uVersion
    dDiff       = "deb.diff"
    dscFile     = file + ".dsc"
    gzDDiff     = dDiff + ".gz"
    diffFile    = file + ".diff.gz"
    origFile    = lang + "_" + version + ".orig.tar.gz"
    changesFile = file + "_source.changes"

    rm_rf(isoLang)

    Dir.mkdir(isoLang)
    cp(lFile, isoLang + "/")
    Dir.chdir(isoLang)
    system("tar -xf #{lFile}")
    OrigBuilder.new(lFile).convert

    unless get_prev(lang,TD,true)
        @missing += [isoLang]
        return
    end

    pDir = Dir.glob("**/debian")[0]

    Dir.chdir("..")

    cp_r("prev/#{pDir}", dir + "/")

    Dir.chdir(dir)
    if $*[0] and $*[0].include?("backport")
        system("dch -b -D #{BD} -v 4:#{uVersion} '#{BD.upcase} PPA Build'")
    else
        system("dch -v 4:#{uVersion} #{if $*[0]; $*[0]; else; "New upstream release"; end}")
        update_standards
        bump_deps
        system("make -f debian/rules get-desktop")
    end
    debuild

    Dir.chdir("..")
    system("debdiff #{dscFile} prev/#{lang}_*.dsc > #{dDiff}")
    system("gzip -9 #{dDiff}")

    # copy files to upload directory
    upDir = @upDir + "/" + isoLang
    Dir.mkdir(upDir)
    cp(dscFile, upDir)
    cp(diffFile, upDir)
    cp(changesFile, upDir)
    cp(origFile, upDir)
    cp(gzDDiff, upDir)

    Dir.chdir("../") # langs
    @kio.trash(lFile) # move to trash rather than delete... in case something goes like wrong
end

@missing = []

@langDir = Dir.pwd + "/langs"
@upDir = Dir.pwd + "/uploads"
rm_rf(@upDir)
Dir.mkdir(@upDir)

Dir.foreach("langs") {|lang|
    next unless lang.include?(".tar.bz2")

    puts(lang)
    exe(lang)
}

for i in @missing
    wrn("#{i} not yet in the archives - please package manually!!!!!!!!!!!!!")
end
