#!/usr/local/bin/perl -w
#-*- perl -*-
#
# Copyright (C) 2000-2001 Ken'ichi Fukamachi
#          All rights reserved. 
#
# $Id: loader,v 1.12 2001/02/17 14:39:16 fukachan Exp $ 
# $FML$
#

eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
        if $running_under_some_shell;

use vars qw($MainCF $running_under_some_shell);
use strict;
use Carp;

# main configuration file for directory switch
$MainCF = '/etc/fml/main.cf';

eval { &Bootstrap();};
if ($@) { print STDERR "Error: ", $@, "\n"; exit 1;}

exit 0;


# Firstly we load Standalone.pm which has main.cf parser().
BEGIN {
    use File::Basename;
    unshift(@INC, dirname($0));
    require Standalone;
}


# Descriptions: top lebel bootstrap program
#               which load a dispather program (process_switch) 
#               for process switch. The flow of execution follows:
#               libexec/loader -> 
#                  libexec/process_switch -> 
#                      FML::Process::Something
#    Arguments: none
#               XXX this program sees $0
#                   (program name, == argv[0] of C language) 
# Side Effects: switch to the real process
# Return Value: none
sub Bootstrap
{
    # 1. main.cf exists and I can open it?
    -f $MainCF   || die("cannot find ${MainCF}");
    my $fh = new FileHandle $MainCF;
    defined($fh) || die("cannot find ${MainCF}");

    # parse command line options (preliminary)
    my $main_cf_file = $MainCF;
    my $params;
    for (my $i = 0; $i <= $#ARGV; $i++) {
	if ($ARGV[ $i ] =~ /^\-c$/) { $main_cf_file = $ARGV[$i + 1];}
	if ($ARGV[ $i ] =~ /^\-\-params$/) { $params = $ARGV[$i + 1];}
    }

    # 2.1 o.k. try to load main.cf (1st pass) to resolve $libexec_dir
    my $main_cf     = Standalone::load_cf($main_cf_file, $params);
    my $libexec_dir = $main_cf->{ libexec_dir };

    # set up @INC
    push(@INC, split(/\s+/, $main_cf->{ lib_dir }));

    # 2.2 load $version depended process_switch
    eval {
	require FML::Process::Switch;
    };
    unless ($@) {
	&Bootstrap2($main_cf_file);
    }
    else {
	croak("cannot load $libexec_dir/process_switch\n");
    }
}


=head1 NAME

loader -- the wrapper which executes a real fml5 program.

=head1 SYNOPSIS

loader C<[-c main.cf]> C<[--debug]> C<[--ctladdr]> [ml_home_dir]

=head1 DESCRIPTION

The loader resolves fml version dependence.
It checks /etc/fml/main.cf and prepare @INC for some fml version.
The real process switch routine is dependent on fml version.

C<libexec/fml/loader> switches itself to a program specified by $0.
C<libexec/fml/$VERSION/process_switch> analyzes several conditions
specified by main.cf, @INC and $0 et. al.
The swithing is done in libexec/fml/$VERSION/process_switch.
See C<libexec/fml/$VERSION/process_switch> for more details on process
switching.

C<-c main.cf>
    main.cf alternative

C<--debug>
    debug on.

C<--ctladdr> 
    execute fml/libexec/command.

C<--params> 
    --params 'key1=value1 key2=value2 ...'

=cut


=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2001 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself. 

=head1 HISTORY

libexec/loader appeared in fml5 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;
