1 | #!/bin/env perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use File::Temp; |
---|
9 | |
---|
10 | GetOptions( |
---|
11 | 'c|config=s' => \my $config, |
---|
12 | 'b|base=s' => \my $base, |
---|
13 | 'o|object=s' => \my $otype, |
---|
14 | 'help' => sub { pod2usage(0) }, |
---|
15 | ) or pod2usage(); |
---|
16 | |
---|
17 | $otype ||= 'user'; |
---|
18 | |
---|
19 | my $LA = LATMOS::Accounts->new($config); |
---|
20 | my $labase = $base ? $LA->base($base) : $LA->default_base; |
---|
21 | $labase && $labase->load or die "Cannot load base"; |
---|
22 | |
---|
23 | $labase->is_supported_object($otype) or die "$otype object unsupported\n"; |
---|
24 | |
---|
25 | my @fields = $labase->list_canonicals_fields($otype, 'w'); |
---|
26 | my %attr; |
---|
27 | |
---|
28 | while (1) { |
---|
29 | my ($fh, $filename) = File::Temp::tempfile(); |
---|
30 | printf $fh "## new object %s: %s\n", $otype, $ARGV[0]; |
---|
31 | foreach (@fields) { |
---|
32 | print $fh "$_:" . ($attr{$_} || '') . "\n"; |
---|
33 | } |
---|
34 | close($fh); |
---|
35 | my @stat = stat($filename); |
---|
36 | system('vi', $filename, '+ 1'); |
---|
37 | if ((stat($filename))[9] == $stat[9]) { |
---|
38 | warn "No change, aborting\n"; |
---|
39 | unlink($filename); |
---|
40 | last; |
---|
41 | } else { |
---|
42 | open($fh, '<', $filename); |
---|
43 | while (my $line = <$fh>) { |
---|
44 | chomp($line); |
---|
45 | $line =~ s/#.*//; |
---|
46 | my ($fi, $val) = $line =~ /^(\w+)\s*:\s*(.*)/ or next; |
---|
47 | $attr{$fi} = $val; |
---|
48 | } |
---|
49 | close($fh); |
---|
50 | unlink($filename); |
---|
51 | $labase->create_c_object($otype, $ARGV[0], %attr) or do { |
---|
52 | warn "Cannot create object: Redit ?\n"; |
---|
53 | my $reply = <STDIN>; |
---|
54 | $reply =~ /^[yY]/ or exit(1); |
---|
55 | next; |
---|
56 | }; |
---|
57 | $labase->commit; |
---|
58 | |
---|
59 | warn $LA->default_synchro; |
---|
60 | if (!$base) { |
---|
61 | if (my $sync = $LA->default_synchro) { |
---|
62 | for (1) { |
---|
63 | $sync->load_dest and do { |
---|
64 | warn "Cannot load dest"; |
---|
65 | last; |
---|
66 | }; |
---|
67 | $sync->process(); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | last; |
---|
72 | } |
---|
73 | } |
---|