for fetching inserted keys without pg_oid_status, look up the actual sequence name...
[freeside.git] / FS / bin / freeside-setup
1 #!/usr/bin/perl -Tw
2
3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
5
6 use strict;
7 use vars qw($opt_s);
8 use Getopt::Std;
9 use Locale::Country;
10 use Locale::SubCountry;
11 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
12 use FS::Schema qw( dbdef_dist reload_dbdef );
13 use FS::Record;
14 use FS::cust_main_county;
15 #use FS::raddb;
16 use FS::part_bill_event;
17
18 die "Not running uid freeside!" unless checkeuid();
19
20 #my %attrib2db =
21 #  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
22
23 getopts("s");
24 my $user = shift or die &usage;
25 getsecrets($user);
26
27 #needs to match FS::Record
28 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
29
30 ###
31
32 #print "\nEnter the maximum username length: ";
33 #my($username_len)=&getvalue;
34 my $username_len = 32; #usernamemax config file
35
36 #print "\n\n", <<END, ":";
37 #Freeside tracks the RADIUS User-Name, check attribute Password and
38 #reply attribute Framed-IP-Address for each user.  You can specify additional
39 #check and reply attributes (or you can add them later with the
40 #fs-radius-add-check and fs-radius-add-reply programs).
41 #
42 #First enter any additional RADIUS check attributes you need to track for each 
43 #user, separated by whitespace.
44 #END
45 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
46 #                         split(" ",&getvalue);
47 #
48 #print "\n\n", <<END, ":";
49 #Now enter any additional reply attributes you need to track for each user,
50 #separated by whitespace.
51 #END
52 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
53 #                   split(" ",&getvalue);
54 #
55 #print "\n\n", <<END, ":";
56 #Do you wish to enable the tracking of a second, separate shipping/service
57 #address?
58 #END
59 #my $ship = &_yesno;
60 #
61 #sub getvalue {
62 #  my($x)=scalar(<STDIN>);
63 #  chop $x;
64 #  $x;
65 #}
66 #
67 #sub _yesno {
68 #  print " [y/N]:";
69 #  my $x = scalar(<STDIN>);
70 #  $x =~ /^y/i;
71 #}
72
73 #my @check_attributes = (); #add later
74 #my @attributes = (); #add later
75 #my $ship = $opt_s;
76
77 ###
78 # create a dbdef object from the old data structure
79 ###
80
81 my $dbdef = dbdef_dist;
82
83 #important
84 $dbdef->save($dbdef_file);
85 &FS::Schema::reload_dbdef($dbdef_file);
86
87 ###
88 # create 'em
89 ###
90
91 my $dbh = adminsuidsetup $user;
92
93 #create tables
94 $|=1;
95
96 foreach my $statement ( $dbdef->sql($dbh) ) {
97   $dbh->do( $statement )
98     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
99 }
100
101 #now go back and reverse engineer the db
102 #so we pick up the correct column DEFAULTs for #oidless inserts
103 dbdef_create($dbh, $dbdef_file);
104 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
105 reload_dbdef($dbdef_file);
106
107 #cust_main_county
108 foreach my $country ( sort map uc($_), all_country_codes ) {
109
110   my $subcountry = eval { new Locale::SubCountry($country) };
111   my @states = $subcountry ? $subcountry->all_codes : undef;
112
113   if ( !scalar(@states) || ( scalar(@states) == 1 && !defined($states[0]) ) ) {
114
115     my $cust_main_county = new FS::cust_main_county({
116       'tax'   => 0,
117       'country' => $country,
118     });  
119     my $error = $cust_main_county->insert;
120     die $error if $error;
121
122   } else {
123
124     if ( $states[0] =~ /^(\d+|\w)$/ ) {
125       @states = map $subcountry->full_name($_), @states
126     }
127
128     foreach my $state ( @states ) {
129
130       my $cust_main_county = new FS::cust_main_county({
131         'state' => $state,
132         'tax'   => 0,
133         'country' => $country,
134       });  
135       my $error = $cust_main_county->insert;
136       die $error if $error;
137
138     }
139   
140   }
141 }
142
143 #billing events
144 foreach my $aref ( 
145   #[ 'COMP', 'Comp invoice', '$cust_bill->comp();', 30, 'comp' ],
146   [ 'CARD', 'Batch card', '$cust_bill->batch_card();', 40, 'batch-card' ],
147   [ 'BILL', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
148   [ 'DCRD', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
149   [ 'DCHK', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
150 ) {
151
152   my $part_bill_event = new FS::part_bill_event({
153     'payby' => $aref->[0],
154     'event' => $aref->[1],
155     'eventcode' => $aref->[2],
156     'seconds' => 0,
157     'weight' => $aref->[3],
158     'plan' => $aref->[4],
159   });
160   my($error);
161   $error=$part_bill_event->insert;
162   die $error if $error;
163
164 }
165
166 $dbh->commit or die $dbh->errstr;
167 $dbh->disconnect or die $dbh->errstr;
168
169 #print "Freeside database initialized sucessfully\n";
170
171 sub dbdef_create { # reverse engineer the schema from the DB and save to file
172   my( $dbh, $file ) = @_;
173   my $dbdef = new_native DBIx::DBSchema $dbh;
174   $dbdef->save($file);
175 }
176
177 sub usage {
178   die "Usage:\n  freeside-setup user\n"; 
179 }
180
181 1;
182