The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w

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

use strict;
use Getopt::Long;
use Data::Dumper;
use Pod::Usage;
use Safe;

use Embedix::DB;
use Embedix::ECD;

#_______________________________________
sub help {
    pod2usage(-verbose => 1);
}

#_______________________________________
sub new {
    my $edb = shift;
    if (-d "./EBX") {
        print STDERR "a project already exists here\n";
    } else {
        mkdir("EBX", 0775) or die($!);
    }
    die("not done yet");
}

#_______________________________________
sub add {
    my $edb = shift;
    foreach (@ARGV) {
        my $ecd = Embedix::ECD->newFromFile($_);
        $edb->updateDistro(ecd => $ecd);
    }
}

#_______________________________________
sub ls {
    my $edb = shift;
    my $cl  = $edb->getComponentList;
    my $cat;

    foreach $cat (@$cl) {
        print $cat->[0], "\n";
        foreach my $item (@{$cat->[1]}) {
            print "  $item->[1]\n";
        }
    }
}

# this is a wrapper for distro manipulation methods
#_______________________________________
sub admin {
    my $edb     = shift;
    local @ARGV = @_;

    my %sub_for_admin_command = (
        addDistro    => undef,
        cloneDistro  => undef,
        updateDistro => undef,
    );

    my $c = shift(@ARGV) || pod2usage(-verbose => 0);
    my $sub;

    if (defined($sub = $sub_for_admin_command{$c})) {
        $sub->($edb, @ARGV);
    } else {
        warn("'$c' is not a valid command.\n");
    }
}

#_________________________________________________________________________main

# read config
my $config_file = "$ENV{HOME}/.embedix/embedixrc";
if (-e $config_file) {
    # eval ebxrc
    open(EBXRC, $config_file) || die($!);
    my $ebxrc = join('', <EBXRC>);
    close(EBXRC);
    eval($ebxrc);
} else {
    # create ebxrc if it doesn't already exist;
    mkdir("$ENV{HOME}/.embedix", 0777)  || die($!);
    open(EBXRC, "> $config_file")       || die($!);
    close(EBXRC);
    chmod(0600, $config_file)
}

# setup function table
my %sub_for_command = (
    '--help'        => \&help,
    '-h'            => \&help,
    add             => \&add,
    admin           => \&admin,
    build           => undef,
    dep             => undef,
    enable          => undef,
    ls              => \&ls,
    new             => \&new,
);

# connect to edb
my $edb = Embedix::DB->new(
    backend => 'Pg',
    source  => [
        'dbi:Pg:dbname=embedix', undef, undef,
        { AutoCommit => 0 }
    ],
);

# cheat
$edb->workOnDistro(name => 'Embedix 2.0', board => 'i386');

# werk
my $c = shift(@ARGV) || pod2usage(-verbose => 0);
my $sub;

if (defined($sub = $sub_for_command{$c})) {
    $sub->($edb, @ARGV);
} else {
    warn("'$c' is not a valid command.\n");
}

exit;

__END__

=head1 NAME

ebx - manage Embedix distributions

=head1 SYNOPSIS

=over 4

=item B<ebx>

[ARGUMENT] [OPTION]...

=back

=head1 DESCRIPTION

This is a command-line tool for managing and configuring Embedix distributions.
The command is invoked similar to the way cvs(1) is invoked in that one has a
choice of many subcommands to invoke.

Remember:  EBX rhymes with CVS.

=head1 ARGUMENTS

=head2 Project-related Commands

=over 8

=item new

initialize a new instance of a distro

It goes like this...

=item build

build the current instance of the distro.

=item dep

check the dependencies of a node

=item enable

enable a node

=item ls

list packages

=back

=head2 Distribution-related Commands

=over 8

=item addDistro

add a distribution to the database

=item cloneDistro

clone an existing distro

=item updateDistro

add information from an ECD to a distro

=back

=head1 FILES

=over 4

=item $ENV{HOME}/.embedix/ebxrc

=back

=head1 DIAGNOSTICS

error messages

=head1 REQUIRES

=over 4

=item Embedix::ECD

=item Embedix::DB

=item Embedix::Config

=item Embedix::ConfigServer

=item Getopt::Long

for command-line options

=back

=head1 SEE ALSO

stuff that relates to me

=head1 COPYRIGHT

Copyright (c) 2000,2001 John BEPPU.  All rights reserved.  This program is
free software; you can redistribute it and/or modify it under the same
terms as Perl itself.

=head1 AUTHOR

John BEPPU <beppu@lineo.com>

=cut

# $Id: ebx,v 1.8 2001/03/09 13:53:51 beppu Exp $