pass the -p/-path correctly to the import engine
[bse.git] / site / util / bsexlsprod.pl
CommitLineData
97469012
TC
1#!perl -w
2use strict;
3use Getopt::Long;
4use FindBin;
5use lib "$FindBin::Bin/../cgi-bin/modules";
6use BSE::Cfg;
d2473dc2 7use BSE::API qw(bse_init bse_cfg bse_make_product bse_encoding);
3709451d 8use BSE::Importer;
97469012 9use Carp qw(confess);
e868dbfc 10use Config;
97469012 11
d2473dc2 12bse_init("../cgi-bin");
97469012
TC
13
14my $verbose;
15my $delete;
bf87e97c 16my @file_path;
97469012 17GetOptions("v", \$verbose,
bf87e97c
TC
18 "d", \$delete,
19 "path|p=s", \@file_path);
97469012
TC
20$verbose = defined $verbose;
21
22my $cfg = bse_cfg();
23
24my $profile = shift;
25my $filename = shift
26 or die "Usage: $0 profile filename\n";
27
97469012
TC
28my $callback;
29$verbose
30 and $callback = sub { print "@_\n" };
31
3709451d
TC
32my $importer = BSE::Importer->new
33 (
34 cfg => $cfg,
35 profile => $profile,
e868dbfc 36 file_path => join($Config{pathsep}, @file_path),
3709451d
TC
37 callback => $callback,
38 );
39
40$importer->process($filename);
97469012
TC
41
42if ($delete) {
3709451d
TC
43 my @products = $importer->leaves;
44 my @catalogs = $importer->parents;
97469012
TC
45 for my $product (@products) {
46 print "Removing product $product->{id}: $product->{title}\n";
47 $product->remove($cfg);
48 }
49 require BSE::Permissions;
50 my $perms = BSE::Permissions->new($cfg);
51 for my $catalog (reverse @catalogs) {
52 my $msg;
53 if ($perms->check_edit_delete_article({}, $catalog, '', \$msg)) {
54 print "Removing catalog $catalog->{id}: $catalog->{title}\n";
55 $catalog->remove($cfg);
56 }
57 else {
58 print "Cannot remove $catalog->{id}: $msg\n";
59 }
60 }
61}
62
63my @errors = $importer->errors;
64unless ($verbose) { # unless we already reported them
65 print STDERR $_, "\n" for @errors;
66}
67@errors
68 and exit 1;
69
70exit;