#!/usr/local/bin/perl -w
#-*- perl -*-
#
# Copyright (C) 2000 Ken'ichi Fukamachi
#          All rights reserved. 
#
# $FML$
#

package FML;

use vars qw($debug);
use strict;
use Carp;


# Descriptions: top level process switch
#               emulates "use $package" but $package is dynamically 
#               determined by e.g. $0.
#    Arguments: $args
#               XXX non OO interface
# Side Effects: process switching :-)
#               ProcessSwtich() is exported to main:: Name Space.
# Return Value: package name
sub main::ProcessSwitch
{
    my ($args) = @_;

    # Firstly, create process 
    # $pkg is a  package name, for exampl,e "FML::Process::Distribute".
    my $pkg = _module_we_use($args);
    croak("$args->{ myname } is unknown program\n") unless ($pkg);

    eval qq{ require $pkg; }; # XXX require() needs bare words.
    croak($@) if $@;
    $pkg->import();   # fake to use() method.

    return $pkg;
}


# Descriptions: determine package we need and require() it if needed.
#    Arguments: $args
#               XXX non OO interface
# Side Effects: none
# Return Value: FML::Process::SOMETHING process object
sub _module_we_use
{
    my ($args) = @_;    
    my $name   = $args->{ myname };
    my $pkg    = '';

    if (($name eq 'fml.pl' && $args->{ options }->{ ctladdr }) || 
	   $name eq 'command') {
	$pkg = 'FML::Process::Command';
    }
    elsif ($name eq 'fml.pl' || $name eq 'distribute' || $name eq 'loader') {
	$pkg = 'FML::Process::Distribute';
    }
    elsif ($name eq 'fmlserv') {
	$pkg = 'FML::Process::ListServer';
    }
    elsif ($name eq 'fmlconf' || $name eq 'makefml') {
	$pkg = 'FML::Process::Configure';
    }
    elsif ($name eq 'fmlticket') {
	$pkg = 'FML::Process::TicketSystem';
    }
    elsif ($name eq 'mead') {
	$pkg = 'FML::Process::MailErrorAnalyzer';
    }
    elsif ($name eq 'qmail-ext') {
	$pkg = 'FML::Process::QMail';
    }
    else {
	return '';
    }

    return $pkg;
}


=head1 NAME

process_switch - switch or dispacther table to execute real fml programs

=head1 SYNOPSIS

	require "$libexec_dir/process_switch";
	ProcessSwitch( 
		      {
			  fml_version    => $main_cf->{ fml_version },

			  myname         => $myname,
			  ml_home_prefix => $main_cf->{ ml_home_prefix },
			  ml_home_dir    => $ml_home_dir,

			  cf_list        => \@cf,
			  options        => \%options,
		      });

See also C<libexec/fml/loader>.

=head1 DESCRIPTION

C<libexec/loader>
(C<libexec/fml/loader>), 
the wrapper, loads this program and calls C<ProcessSwitch()>.  
C<ProcessSwitch()> emulates "use $package" for a program specified by
the arguments.

The details of each program exists in FML::Process:: class.

=head1 SEE ALSO

L<FML::Process::Distribute>,
L<FML::Process::Command>,
L<FML::Process::ListServer>,
L<FML::Process::Configure>,
L<FML::Process::TicketSystem>,
L<FML::Process::MailErrorAnalyzer>

=cut

1;
