#!/usr/bin/env ruby
#
# Ruby script for generating tarball releases from SVN
#
# (c) 2005 Mark Kretschmann <markey@web.de>
# modified by Carsten Niehaus
# modified by Anne-Marie Mahfouf 2007
# Some parts of this code taken from cvs2dist
# License: GPL V2

#this script can run in any dir: it gets the app sources from
#KDE anonymous svn and packs it in a tarball
require 'getoptlong'

opts = GetoptLong.new(
	[ "--nogpg", "-g",	GetoptLong::NO_ARGUMENT ],
	[ "--libkdeedu", "-l",	GetoptLong::NO_ARGUMENT ],
	[ "--widgets", "-w",	GetoptLong::NO_ARGUMENT ],
	[ "--appname", "-a",	GetoptLong::REQUIRED_ARGUMENT  ],
	[ "--help", "-h",	GetoptLong::NO_ARGUMENT ]
)

dogpg = "yes"
packlibkdeedu = "no"
packwidgets = "no"
name = ""

#check if there are arguments
opts.each do |opt, arg|
	if opt == "--nogpg"
		dogpg = "no"
	elsif opt == "--libkdeedu"
		packlibkdeedu = "yes"
	elsif opt == "--widgets"
		packwidgets = "yes"
	elsif opt == "--appname"
		name = arg
	elsif opt == "--help"
		puts "Usage:\n
		Optional:\n
		--noi18n to disable the packaging of translations and documentation\n
		--nogpg to disable signing with GnuPG\n
		--libkdeedu to enable packing libkdeedu\n
		--widgets to enable packing the KDEEDU-Widgets"
		exit()
	end
end

if name.empty?
	name     = `kdialog --combobox "Select an application:"  Step KAlgebra`.chomp!.downcase
end

if name.empty?
	exit()
end

version     = `kdialog --title "Version number" --inputbox "Enter the version number"`.chomp!
if version.empty?
	version = "preview"
end

puts "Start packing #{name}".center(40, " - ")

folder   = "#{name}-#{version}"

# Some helper methods
def svn( command, dir )
    `svn #{command} svn://anonsvn.kde.org/home/kde/#{dir}`
end

# Remove old folder, if exists
`rm -rf #{folder} 2> /dev/null`
`rm -rf folder.tar.bz2 2> /dev/null`

Dir.mkdir( folder )
Dir.chdir( folder )

if packlibkdeedu == "yes"
        svn( "co -N", "/trunk/KDE/kdeedu/" )
        Dir.chdir( "kdeedu")
	puts "Checking out libkdeedu".center(40, " - ")
	`svn up libkdeedu`
end
if  packwidgets == "yes"
        svn( "co -N", "/trunk/playground/edu/" )
        Dir.chdir( "playground/edu")
	puts "Checking out the kdeedu-widgets".center(40, " - ")
	svn( "co", "/trunk/playground/edu/widgets/")
end

puts "Checking out #{name}".center(40," - ")
`svn co svn://anonsvn.kde.org/home/kde/trunk/playground/edu/#{name}`

puts "\n"

puts "Removing svn-history files"
`find -name ".svn" | xargs rm -rf`

Dir.chdir( "#{name}" )

puts "Compressing".center(40, " - ")
#`mv * ..`
Dir.chdir( ".." ) 
`cp -R #{name} #{folder}`
#Dir.chdir( ".." ) # root folder
`tar -cf #{folder}.tar #{folder}`
`bzip2 #{folder}.tar`
`mv #{folder}.tar.bz2 ..`
Dir.chdir( ".." ) # root folder
`rm -rf #{folder}`
puts "done.\n"

if dogpg == "yes"
	puts "Signing with your GnuPG-key".center(40, " - ")
	`gpg --detach-sign #{folder}.tar.bz2`
end


puts "\n"
puts "====================================================="
puts "Congratulations :) #{name} #{version} tarball generated.\n"
puts "\n"
puts "MD5 checksum: " + `md5sum #{folder}.tar.bz2`
if dogpg == "yes"
	puts "The user can verify this package with "
	puts "\n"
	puts "gpg --verify #{folder}.tar.bz2.sig #{folder}.tar.bz2"
end
puts "\n"
puts "\n"

