#!/usr/bin/perl -w
# SL Source Importer $Date$ $Rev$
#
# 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) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


use strict;
use File::Basename qw(basename);

# Root directory where the source will be extracted (in a linden subdir)
my $root = "$ENV{HOME}/src";

# Subversion root directory. MUST be the whole repository, not a branch.
# Checkout this: http://svn.daleglass.net/secondlife/
my $svn  = "$ENV{HOME}/src/sl";

# Font to use for the viewer. This is here because svn_load_dirs doesn't
# like a broken symlink, so we're going to either make it link to something,
# or remove it. Set this to a .ttf file to change the link to the file,
# or leave it undefined to remove the link.
#
my $font = "$root/Vera.ttf";

##########################################################################
# Code
##########################################################################

$| = 1;
my $verbose = 1;

sub run_command {
	my @args = @_;
	my $cmd = join(' ', @args);
	
	system(@args);
	
	if ( $? == -1 ) {
		die "Failed to run command '$cmd': $!";
	} elsif ($? & 127) {
		die "Command '$cmd' died with signal " . ($? & 127) .
		    ", " . (($? & 128) ? 'with' : 'without') . " coredump";
	} elsif ( $? >> 8 != 0 ) {
		die "Command '$cmd' failed, return value " . $? >> 8;;
	}
}

sub info($) {
	if ( $verbose ) {
		print STDERR shift;
	}
}

my $path = $ARGV[0] or die "Syntax: $0 <path>";

info "Root directory: $root\n";
info "SVN directory:  $svn\n";
info "Font file:      $font\n";
info "\n";

info "Checking directories... ";

my @files = glob("$path/*");
my $release;
my $url;

# Make sure settings are sane and all required directories
# exist.
for my $d ($svn, "${svn}/vendor", "${svn}/vendor/current", $root, $path) {
	die "$d not found" unless ( -d $d );
}
info "ok.\n";

# Update svn
# This is needed for making sure the code wasn't already processed.
info "Updating svn...\n";
run_command("svn", "up", $svn);

info "Getting repository path... ";
my @lines = `svn info \"${svn}/vendor\"`;
foreach my $l (@lines) {
	if ( $l =~ /^URL: (.*)$/ ) {
		$url = $1;
	}
}

if ( defined $url ) {
	info "$url, ok.\n";
} else {
	die "Failed to find repository url";
}

# Verify that all the files in the specified directory have
# the right name, and at the same time, extract the version
# number.
info "Verifying... ";

my $filecount = 0;
foreach my $file (@files) {
	my $name = basename($file);
	if ( $name =~ /^slviewer-(artwork|src)-(.*?)\.(zip|tar\.gz|tar\.bz2|tgz|tbz2)$/ ) {
		if ( defined $release && $release ne $2 ) {
			die "Version mismatch: $release/$2";
		} else {
			$release=$2;
			$filecount++;
		}
	} else {
		die "Unrecognized file: $name";
	}
}

# Make sure that we've got the expected number of files
if ( $filecount != 2 ) {
	die "File count mismatch, expected 2, got $filecount";
}

info "release is $release, ok.\n";

if (-d "${svn}/vendor/$release") {
	die "Release already in repository, aborting";
}

if (-d "${root}/linden") {
	info "Removing old source... ";
	run_command("rm", "-rf", "${root}/linden");
	info "ok.\n";
}

# Extract all files
info "Extracting...\n";
foreach my $file (@files) {
	info "\tExtracting $file ";
	chdir($root) or die "Can't chdir to $root: $!";

	my $ret;
	if ( $file =~ m/\.(tar\.gz|tgz)$/ ) {
		info "(tgz)... ";
		run_command("tar","-xzf", $file);
	} elsif ( $file =~ m/\.(tar\.bz2|tbz2)$/ ) {
		info "(tbz2)... ";
		run_command("tar","-xjf", $file);
	} elsif ( $file =~ /\.zip$/ ) {
		info "(zip)... ";
		run_command("unzip","-q", $file);
	}

	info "ok!\n";
}

# See explanation on top of file on why this is needed
info "Fixing font... ";
my $link = "${root}/linden/indra/newview/linux_tools/unicode.ttf";
if ( -l $link ) {
	chdir("${root}/linden")  or die "Can't chdir to ${root}/linden: $!";

	if ( defined $font && -f $font ) {
		info "setting to $font... ";
		my $base = basename($font);
		my $tmp = "${root}/linden/indra/newview/${base}";

		unlink($link);

		# Try to put the file right into the repository
		if (link($font, $tmp)) {
			info "link ok, done.\n";
			symlink("../${base}", $link);
		} else {
			info "done.\n";
			symlink($font, $link);
		}
	} else {
		info "no font found or configured, removing link.\n";
		unlink($link);
	}
} else {
	info "that's strange, no link found.\n";
}

# Finally, run svn_load_dirs
info "Loading directories into repository...\n";
chdir($root) or die "Can't chdir to $root: $!";

run_command("svn_load_dirs", "-no_user_input",
            "-t", $release,
            "-wc", "${svn}/vendor/current",
            $url, "current", "linden");

info "All done!\n";



