--- extra/svn/reposman.rb.orig	2010-10-07 14:12:50.000000000 +0200
+++ extra/svn/reposman.rb	2010-10-07 14:12:37.000000000 +0200
@@ -15,6 +15,7 @@
 # == Arguments (mandatory)
 #
 #   -s, --svn-dir=DIR         use DIR as base directory for svn repositories
+#   -a, --archive-dir=DIR     Archive repositories to DIR before deleting them 
 #   -r, --redmine-host=HOST   assume Redmine is hosted on HOST. Examples:
 #                             -r redmine.example.net
 #                             -r http://redmine.example.net
@@ -29,6 +30,7 @@
 #                             share repositories through Redmine.pm, you need
 #                             to use the apache owner.
 #   -g, --group=GROUP         group of the repository. (default: root)
+#   --with-tar=/path/to/tar   Path to the tar command (default: /bin/tar)
 #   --scm=SCM                 the kind of SCM repository you want to create (and
 #                             register) in Redmine (default: Subversion).
 #                             reposman is able to create Git and Subversion
@@ -50,6 +52,7 @@
 #                             and subversion.
 #   -f, --force               force repository creation even if the project
 #                             repository is already declared in Redmine
+#   -d, --delete	      Delete repositories that doesn't exist in redmine anymore
 #   -t, --test                only show what should be done
 #   -h, --help                show help and exit
 #   -v, --verbose             verbose
@@ -65,6 +68,7 @@
 require 'rdoc/usage'
 require 'find'
 require 'etc'
+require 'fileutils'
 
 Version = "1.3"
 SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem )
@@ -80,10 +84,13 @@
                       ['--scm',                GetoptLong::REQUIRED_ARGUMENT],
                       ['--test',         '-t', GetoptLong::NO_ARGUMENT],
                       ['--force',        '-f', GetoptLong::NO_ARGUMENT],
+                      ['--delete',       '-d', GetoptLong::NO_ARGUMENT],
                       ['--verbose',      '-v', GetoptLong::NO_ARGUMENT],
                       ['--version',      '-V', GetoptLong::NO_ARGUMENT],
                       ['--help'   ,      '-h', GetoptLong::NO_ARGUMENT],
-                      ['--quiet'  ,      '-q', GetoptLong::NO_ARGUMENT]
+                      ['--archive-dir',  '-a', GetoptLong::REQUIRED_ARGUMENT],
+		      ['--with-tar',           GetoptLong::REQUIRED_ARGUMENT],
+		      ['--quiet'  ,      '-q', GetoptLong::NO_ARGUMENT]
                       )
 
 $verbose      = 0
@@ -96,7 +103,10 @@
 $svn_url      = false
 $test         = false
 $force        = false
+$delete       = false
+$archive      = ''
 $scm          = 'Subversion'
+$tarcmd	      = '/bin/tar'
 
 def log(text, options={})
   level = options[:level] || 0
@@ -132,16 +142,19 @@
   opts.each do |opt, arg|
     case opt
     when '--svn-dir';        $repos_base   = arg.dup
+    when '--archive-dir';    $archive      = arg.dup
     when '--redmine-host';   $redmine_host = arg.dup
     when '--key';            $api_key      = arg.dup
     when '--owner';          $svn_owner    = arg.dup; $use_groupid = false;
     when '--group';          $svn_group    = arg.dup; $use_groupid = false;
     when '--url';            $svn_url      = arg.dup
+    when '--with-tar';       $tarcmd       = arg.dup	
     when '--scm';            $scm          = arg.dup.capitalize; log("Invalid SCM: #{$scm}", :exit => true) unless SUPPORTED_SCM.include?($scm)
     when '--command';        $command =      arg.dup
     when '--verbose';        $verbose += 1
     when '--test';           $test = true
     when '--force';          $force = true
+    when '--delete';         $delete = true
     when '--version';        puts Version; exit
     when '--help';           RDoc::usage
     when '--quiet';          $quiet = true
@@ -230,6 +243,49 @@
   (RUBY_PLATFORM =~ /(:?mswin|mingw)/) || (RUBY_PLATFORM == 'java' && (ENV['OS'] || ENV['os']) =~ /windows/i)
 end
 
+if $delete == true
+	log("checking for svn directories to delete", :level => 1)
+	svnprojects = Dir.glob("#{$repos_base}/*/")
+	svnprojects.each do |svnproject|
+		matches = svnproject.match(/\/([a-z0-9-]+)\/$/)
+		svnname = matches[1]
+
+		if svnname.match(/^\./)
+			next
+		end
+
+		$found = false
+		projects.each do |project|
+			if project.identifier.match(svnname)
+				$found = true	
+			end
+		end
+
+		if $found == false 
+			log("\tsvn project #{svnproject} doesn't exist in redmine anymore")
+			if (!($archive.empty?))
+				time = Time.new
+				$dateoftheday = time.strftime("%Y-%m-%d")
+				$prjname = File.basename(svnproject)
+				log("\tarchiving project #{$prjname} to #{$archive}")
+				if $test == false
+				  	if File.exists?($archive) && File.directory?($archive)
+						$archivename=$archive+"/"+$prjname+"-"+$dateoftheday+".tgz"
+						$archivesrc="./"+$prjname
+						system("#{$tarcmd} zcf #{$archivename} -C #{$repos_base} #{$archivesrc}")	
+					else
+						log("\tERROR : #{$archive} does not exists or is not a valid directory")
+					end
+				end
+			end
+			log("\tdeleting svn project #{svnproject}")
+			if $test == false
+				FileUtils.rm_r(svnproject)
+			end
+		end
+	end
+end
+
 projects.each do |project|
   log("treating project #{project.name}", :level => 1)
 
