#!/usr/bin/perl -w

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

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

BEGIN {
    # $BRICOLAGE_ROOT defaults to /opt/bricolage
    $ENV{BRICOLAGE_ROOT} ||= "/opt/bricolage";

    # use $BRICOLAGE_ROOT/lib if exists 
    $_ = catdir($ENV{BRICOLAGE_ROOT}, "lib");
    unshift(@INC, $_) if -e $_;
    
    # make sure Bric is found
    eval "use Bric";
    die <<'END' if $@;
######################################################################

Cannot locate 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.

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

use Bric::Config qw(:ftp);

# 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;
  }
}

# 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
push(@ARGV, '-o', 'error log=' . FTP_LOG);

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

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