broadband_nas export, #15284
[freeside.git] / bin / fetch_and_expand_taxes
1 #!/usr/bin/perl -w
2
3 use strict;
4 use LWP::UserAgent;
5 use HTTP::Request;
6 use HTTP::Response;
7 use FS::UID qw(adminsuidsetup);
8 use FS::Conf;
9
10 my $user = shift or die &usage;
11 my $dir = shift or die &usage;
12
13
14 adminsuidsetup $user;
15
16 my $conf = new FS::Conf;
17
18 chdir $dir or die "can't change to $dir: $!\n";
19
20 die "direct download of tax data not enabled\n"
21   unless $conf->exists('taxdatadirectdownload');
22 my ( $urls, $username, $secret, $states ) =
23   $conf->config('taxdatadirectdownload');
24 die "No tax download URL provided.  ".
25     "Did you set the taxdatadirectdownload configuration value?\n"
26   unless $urls;
27
28 my $ua = new LWP::UserAgent;
29   foreach my $url (split ',', $urls) {
30   my @name = split '/', $url;  #somewhat restrictive
31   my $name = pop @name;
32   $name =~ /(.*)/; # untaint that which we trust;
33   $name = $1;
34
35   open my $taxfh, ">$name" or die "Can't open $name: $!\n";
36
37   my $res = $ua->request(
38     new HTTP::Request( GET => $url),
39     sub { #my ($data, $response_object) = @_;
40           print $taxfh $_[0] or die "Can't write to $dir.new/$name: $!\n";
41     },
42   );
43   die "download of $url failed: ". $res->status_line
44     unless $res->is_success;
45   close $taxfh;
46   $secret =~ /(.*)/; # untaint that which we trust;
47   $secret = $1;
48   system('unzip', "-P", $secret, $name) == 0
49     or die "unzip -P $secret $name failed";
50 }
51
52 sub usage {
53   die "Usage:\n\n  fetch_and_expand_taxes user dir\n";
54 }
55