#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

=head1 NAME

bric_ftpd - Bricolage virtual FTP server

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate: 2005-03-23 08:22:07 +0100 (Wed, 23 Mar 2005) $

=head1 SYNOPSIS

  su nobody -c "bric_ftpd -S"

=head1 DESCRIPTION

This program controls the Bricolage vitual FTP server, which can be used to
allow access to Bricolage templates. This may provide a more comfortable
development environment for programmers working on templates. Many editors
provide FTP modes that make editing files via FTP almost as easy as editing
files locally (ange-ftp for Emacs, for example).

There are security risks to consider - FTP sends passwords unencrypted over
the network. If you are running Bricolage behind a firewall this might not be
a problem.

To enable the FTP server you'll need to install the Net::FTPServer Perl
module. After that you can configure the FTP server with the following
F<bricolage.conf> directives:

=over

=item *

B<ENABLE_FTP_SERVER:> Set this to 1 if you want to use the FTP
server. Defaults to 0.

=item *

B<FTP_PORT:> The port that the FTP server will run on. Users will need to know
this port to connect to the FTP server. Since you won't be running the server
as root you'll need to choose a port above 1024. Defaults to 2121.

=item *

B<FTP_ADDRESS:> Normally the FTP server will bind to all the available IP
addresses for your machine. If you set this variable then the server will only
bind to the specified address.

=item *

B<FTP_LOG:> Set this to the file where you want to FTP logs written
to. Defaults to C<$BRICOLAGE_ROOT/ftp.log> if not set.

=item *

B<FTP_PID_FILE:> Set this to the file where you want to the FTP daemon PID
stored. Defaults to C<$BRICOLAGE_ROOT/ftp.pid> if not set.

=item *

B<FTP_DEBUG:> If you set this to 1 you'll see a lot of useful tracing
information in the FTP logfile. This can help diagnose errors but is probably
only of interest to developers. Defaults to 0.

=back

Next you'll need to start the FTP server as the same user that Apache runs as.
For example, if you run Apache as "nobody" you would use this command to start
the FTP server:

  su nobody -c "bric_ftpd -S"

The C<-S> option to ftpd runs the FTP server in the background. For other
possibilities see the documentation for Net::FTPServer.

Once the server is up and running connect to it with an FTP client and start
editing templates. For more information see
L<Bric::Util::FTP::Server|Bric::Util::FTP::Server>.

To kill the FTP server in daemon mode, use the C<-k> option:

  su nobody -c bric_ftpd -k"

=head1 AUTHOR

Sam Tregar <stregar@about-inc.com>

=head1 SEE ALSO

L<Bric::Admin|Bric::Admin>, L<Net::FTPServer|Net::FTPServer>,
L<Bric::Util::FTP::Server|Bric::Util::FTP::Server>.

=cut

use strict;

use File::Spec::Functions qw(catdir);

BEGIN {
    # $BRICOLAGE_ROOT defaults to /usr/local/share/bricolage
    $ENV{BRICOLAGE_ROOT} ||= "/usr/local/share/bricolage";

    # use $BRICOLAGE_ROOT/lib if exists
    my $lib = catdir($ENV{BRICOLAGE_ROOT}, "lib");
    if (-e $lib) {
        $ENV{PERL5LIB} = defined $ENV{PERL5LIB} ?
          "$ENV{PERL5LIB}:$lib" : $lib;
        unshift @INC, $lib;
    }

    # make sure Bric is found
    eval { require Bric };
    die <<"END" if $@;
######################################################################

   Cannot load Bricolage libraries. Please set the environment
   variable BRICOLAGE_ROOT to the location of your Bricolage
   installation or set the environment variable PERL5LIB to the
   directory where Bricolage's libraries are installed.

   The specific error encountered was as follows:

   $@

######################################################################
END
}

use Bric::Config qw(:ftp :sys_user);

if ($> == 0) {
    # Switch from root to the system user (set in bricolage.conf).
    $) = SYS_GROUP;
    die "Unable to set effective group id ". SYS_GROUP . "\n"
      unless $) == SYS_GROUP;
    $> = SYS_USER;
    die "Unable to set effective user id ". SYS_USER . "\n"
      unless $> == SYS_USER;
}

# Kill the server if -k is passed.
if (grep { $_ eq '-k' } @ARGV) {
    unless (-e FTP_PID_FILE) {
        print "\nCannot find ", FTP_PID_FILE, "; no bric_ftpd running?\n\n";
        exit 1
    }
    open PID, FTP_PID_FILE or die "Cannot open ", FTP_PID_FILE, ": $!\n";
    my $pid = <PID>;
    chomp $pid;
    close PID;
    system "kill", $pid and exit;
    unlink FTP_PID_FILE;
    exit;
}

# make sure FTP is enabled
BEGIN {
  unless (ENABLE_FTP_SERVER) {
    print "\nENABLE_FTP_SERVER is off!  Please read Bric::Admin " .
      "for instructions on how to setup the FTP server.\n";
    exit 1;
  }
}

# Make sure there isn't a server running already.
if (-e FTP_PID_FILE) {
    print "\nFile '", FTP_PID_FILE,
      "' exits. Is another bric_ftpd already running?\n";
    exit 1;
}

# load the server
use Bric::Util::FTP::Server;

# setup parameters

# no conf file used
push(@ARGV, '-C', '/dev/null');

# set port and address if specified
push(@ARGV, '-p', FTP_PORT);
if (FTP_ADDRESS() ne "") {
  push(@ARGV, '-o', 'local address=' . FTP_ADDRESS);
}

# setup log and PID file.
push @ARGV,
  '-o', 'xfer logging=' . FTP_LOG,
  '-o', 'error log=' . FTP_LOG,
  '-P', FTP_PID_FILE;

# set debug mode
if (FTP_DEBUG) {
  push(@ARGV, '-d');
}

Bric::Util::FTP::Server->run;
