All pastes #1774162 Raw Edit

update-userguide.pl

public text v1 · immutable
#1774162 ·published 2010-02-01 09:23 UTC
rendered paste body
#!perl -w

use strict;
use Cwd 'abs_path';
use File::Basename;
use File::Copy;
use File::Path;

my @root_items = ('userguide', 'welcome');

my $dest_dir = shift or print_usage();
$dest_dir = abs_path($dest_dir);

chdir $dest_dir or die "Unable to chdir to $dest_dir: $!";

print "-> Getting original filelist\n";
my @orig_list = ();
@orig_list = (@orig_list, get_filelist($_)) foreach (@root_items);
my %orig_items;
$orig_items{$_} = 1 foreach(@orig_list);

chdir "/tmp" or die "Unable to chdir to /tmp: $!";
print "-> Downloading userguide archive\n";
unlink "userguide.zip";
!system("wget http://userguide.haikuzone.net/userguide.zip") or die;

print "-> Uncompressing archive\n";
!system("unzip -o userguide.zip > /dev/null") or die;

print "-> Getting new filelist\n";
chdir "userguide" or die "Unable to chdir to userguide: $!";
my @new_list = ();
@new_list = (@new_list, get_filelist($_)) foreach (@root_items);

print "-> Updating files\n";
foreach my $new_file (@new_list) {
	my $needs_adding = !exists($orig_items{$new_file});
	my $path = "$dest_dir/$new_file";
	
	create_path(dirname($path)) if ($needs_adding);
	
	move($new_file, $path) or die "Unable to mv $new_file to $path: $!";
	
	!system("svn add $path") or die "svn add $path failed"
		if ($needs_adding);
	
	delete($orig_items{$new_file});
}

print "-> Removing obsolete files\n";
foreach my $item (keys %orig_items) {
	!system("svn del $dest_dir/$item")
		or die "svn del $dest_dir/$item failed";
}

print "-> Cleaning up temporary files\n";
chdir "/tmp" or die "Unable to chdir to /tmp: $!";
unlink "userguide.zip";
rmtree "userguide";

sub get_filelist {
	my $base_path = shift;
	
	die "$base_path does not exists" unless -e $base_path;

	return ($base_path) unless (-d $base_path);
	
	my @files = ();
	
	opendir DIR, $base_path or die "Unable to open directory $base_path: $!";
	my @items = readdir DIR;
	closedir DIR;
	
	foreach my $item (@items) {
		next if $item =~ /^\./;
	
		my $path = "$base_path/$item";
		
		if (-d $path) {
			my @sub_files = get_filelist($path);
			@files = (@files, @sub_files);
		} else {
			@files = (@files, $path);
		}
	}
	
	return @files;
}

sub create_path {
	my $path = shift;
	
	if (!-e $path) {
		create_path(dirname($path));
		mkdir $path or die("Unable to create directory $path: $!");
		!system("svn add $path") or die "svn add $path failed";
	} elsif (!-d $path) {
		die("create_path: $path should be a directory");
	}
}

sub print_usage {
	print "Usage: $0 <docs directory>\n";
	exit 2;
}