fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / bin / freeside-setup
index 14c5a84..155c74a 100755 (executable)
@@ -1,37 +1,45 @@
-#!/usr/bin/perl -Tw
+#!/usr/bin/perl -w
 
 #to delay loading dbdef until we're ready
 BEGIN { $FS::Schema::setup_hack = 1; }
 
+#to allow initial insert
+use FS::part_pkg;
+$FS::part_pkg::setup_hack = 1;
+$FS::part_pkg::setup_hack = 1;
+
 use strict;
-use vars qw($opt_s);
+use vars qw($opt_u $opt_d $opt_v $opt_q);
 use Getopt::Std;
-use Locale::Country;
-use Locale::SubCountry;
 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
-use FS::Schema qw( dbdef_dist );
-use FS::Record;
-use FS::cust_main_county;
+use FS::CurrentUser;
+use FS::Schema qw( dbdef_dist reload_dbdef );
+use FS::Record qw( qsearch );
 #use FS::raddb;
-use FS::part_bill_event;
+use FS::Setup qw(create_initial_data);
+use FS::Conf;
 
 die "Not running uid freeside!" unless checkeuid();
 
 #my %attrib2db =
 #  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
 
-getopts("s");
-my $user = shift or die &usage;
-getsecrets($user);
+getopts("u:vqd:");
+$opt_v = 1 unless $opt_q; #verbose by default now
+
+my $config_dir = shift || '%%%DIST_CONF%%%' ;
+$config_dir =~ /^([\w.:=\/]+)$/
+  or die "unacceptable configuration directory name";
+$config_dir = $1;
+
+getsecrets($opt_u);
 
 #needs to match FS::Record
-my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
+my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
 
 ###
 
-#print "\nEnter the maximum username length: ";
-#my($username_len)=&getvalue;
-my $username_len = 32; #usernamemax config file
+my $username_len = 32;
 
 #print "\n\n", <<END, ":";
 #Freeside tracks the RADIUS User-Name, check attribute Password and
@@ -78,7 +86,9 @@ my $username_len = 32; #usernamemax config file
 # create a dbdef object from the old data structure
 ###
 
-my $dbdef = dbdef_dist;
+warn "Loading schema objects\n" if $opt_v;
+
+my $dbdef = dbdef_dist(datasrc);
 
 #important
 $dbdef->save($dbdef_file);
@@ -88,83 +98,70 @@ $dbdef->save($dbdef_file);
 # create 'em
 ###
 
-my $dbh = adminsuidsetup $user;
+warn "Connecting to database\n" if $opt_v;
+
+$FS::CurrentUser::upgrade_hack = 1;
+$FS::UID::callback_hack = 1;
+my $dbh = adminsuidsetup $opt_u; #$user;
+$FS::UID::callback_hack = 0;
 
 #create tables
 $|=1;
 
+warn "Creating tables and indices\n" if $opt_v;
+
 foreach my $statement ( $dbdef->sql($dbh) ) {
   $dbh->do( $statement )
     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
 }
 
-#cust_main_county
-foreach my $country ( sort map uc($_), all_country_codes ) {
-
-  my $subcountry = eval { new Locale::SubCountry($country) };
-  my @states = $subcountry ? $subcountry->all_codes : undef;
-
-  if ( !scalar(@states) || ( scalar(@states) == 1 && !defined($states[0]) ) ) {
-
-    my $cust_main_county = new FS::cust_main_county({
-      'tax'   => 0,
-      'country' => $country,
-    });  
-    my $error = $cust_main_county->insert;
-    die $error if $error;
-
-  } else {
+#now go back and reverse engineer the db
+#so we pick up the correct column DEFAULTs for #oidless inserts
+dbdef_create($dbh, $dbdef_file);
+delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
+reload_dbdef($dbdef_file);
 
-    if ( $states[0] =~ /^(\d+|\w)$/ ) {
-      @states = map $subcountry->full_name($_), @states
-    }
-
-    foreach my $state ( @states ) {
-
-      my $cust_main_county = new FS::cust_main_county({
-        'state' => $state,
-        'tax'   => 0,
-        'country' => $country,
-      });  
-      my $error = $cust_main_county->insert;
-      die $error if $error;
-
-    }
-  
+warn "Tables and indices created - commiting transaction\n" if $opt_v;
+$dbh->commit or die $dbh->errstr;
+$dbh->disconnect or die $dbh->errstr;
+warn "Database schema committed successfully\n" if $opt_v;
+
+warn "Initializing configuration\n" if $opt_v;
+$FS::UID::callback_hack = 1;
+$dbh = adminsuidsetup $opt_u;
+$FS::UID::callback_hack = 0;
+if (!scalar(qsearch('conf', {}))) {
+  my $error = FS::Conf::init_config($config_dir);
+  if ($error) {
+    $dbh->rollback or die $dbh->errstr;
+    die $error;
   }
 }
 
-#billing events
-foreach my $aref ( 
-  #[ 'COMP', 'Comp invoice', '$cust_bill->comp();', 30, 'comp' ],
-  [ 'CARD', 'Batch card', '$cust_bill->batch_card();', 40, 'batch-card' ],
-  [ 'BILL', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
-  [ 'DCRD', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
-  [ 'DCHK', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
-) {
-
-  my $part_bill_event = new FS::part_bill_event({
-    'payby' => $aref->[0],
-    'event' => $aref->[1],
-    'eventcode' => $aref->[2],
-    'seconds' => 0,
-    'weight' => $aref->[3],
-    'plan' => $aref->[4],
-  });
-  my($error);
-  $error=$part_bill_event->insert;
-  die $error if $error;
+warn "Configuration initialized - commiting transaction\n" if $opt_v;
+$dbh->commit or die $dbh->errstr;
+$dbh->disconnect or die $dbh->errstr;
+warn "Configuration committed successfully\n" if $opt_v;
 
-}
+$dbh = adminsuidsetup $opt_u;
+create_initial_data('domain' => $opt_d);
 
+warn "Database initialized - commiting transaction\n" if $opt_v;
 $dbh->commit or die $dbh->errstr;
 $dbh->disconnect or die $dbh->errstr;
+warn "Database initialization committed successfully\n" if $opt_v;
 
-#print "Freeside database initialized sucessfully\n";
+sub dbdef_create { # reverse engineer the schema from the DB and save to file
+  my( $dbh, $file ) = @_;
+  my $dbdef = new_native DBIx::DBSchema $dbh;
+  $dbdef->save($file);
+}
 
 sub usage {
-  die "Usage:\n  freeside-setup user\n"; 
+  die "Usage:\n  freeside-setup -d domain.name [ -q ] [ config/dir ]\n"
+  # [ -u user ] for devel/multi-db installs
 }
 
 1;
 
+