# -*- Mode: cperl; cperl-indent-level: 4 -*-

use strict;
use warnings;

use base qw(HTTP::Server::Simple::CGI);

use SVN::Web;
use Getopt::Long;

=head1 NAME

svnweb-server - simple webserver for SVN::Web

=head1 SYNOPSIS

  svnweb-server [--root DIR] [--port PORT] [--net-server Net::Server]

=head1 DESCRIPTION

svnweb-server is a webserver that runs SVN::Web.  It's an easy way to
use SVN::Web without having to set up Apache (or any other web
server).  The tradeoff is that the server is very simple.  But for
light use then it may be perfectly acceptable.

=head1 OPTIONS

=over

=item --root DIR

The directory in which you ran C<svnweb-install>.

Optional, defaults to C<.>.

=item --port PORT

The port the server should listen on.

Options, defaults to C<8080>.

=item --net-server Net::Server

The Net::Server subclass to use to create the server.  A single,
non-forking server is created by default.  Other types of server can be
created by giving the correct Net::Server subclass.  For example, to
use a server that forks to handle each request, use C<--net-server
Net::Server::Fork>.

=back

=cut

my %options = ('root'   => '.',
	       'port'   => '8080',
	       'net_server' => undef);

GetOptions('root=s'       => \$options{root},
	   'port=i'       => \$options{port},
	   'net-server=s' => \$options{net_server},
	  );

sub handle_request {
    my($self, $cgi) = @_;

    if($cgi->path_info() =~ m{^/css/}) { # Pass /css requests straight through
        my $file = $cgi->path_info();
        $file =~ s/^\///g;
        open(FILE, $file) or die "Can't open() $file: $!\n";
        local $/ = undef;
        print <FILE>;
        close FILE;
    } else {
        $ENV{SCRIPT_NAME} = 'http://' . $self->host() . ':' . $self->port();
	print "HTTP/1.1 200 OK\n";
        SVN::Web::run_cgi();
    }
}

use CGI::Carp qw(fatalsToBrowser);

if(! -f "$options{root}/config.yaml") {
    print <<EOM;
Can't find $options{root}/config.yaml.  Make sure you've run svnweb-install
in $options{root} before running this server.
EOM
    exit;
}

chdir($options{root});
my $server = __PACKAGE__->new($options{port});
$server->net_server($options{net_server}) if defined $options{net_server};
$server->run();

exit;

=head1 SEE ALSO

L<SVN::Web>

=head1 BUGS

This is a simple server.  Accordingly, a few features do not work properly
at the moment.

=over

=item Redirects

L<SVN::Web::Browse> will redirect the client if the URL is missing a trailing
C</>.  That redirection does not work with this server.  That's not an issue
for any URLs generated by L<SVN::Web>, but may be an issue for URLs that
are entered by hand.

This will also affect L<SVN::Web::List>'s C<redirect_to_browse_when_one_repo>
option.

=back

Please report any bugs or feature requests to
C<bug-svn-web@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SVN-Web>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.

=head1 AUTHOR

The current maintainer is Nik Clayton, <nikc@cpan.org>.

=head1 COPYRIGHT AND LICENSE

Copyright 2006 Nik Clayton.  All Rights Reserved.
