should be able to freeside-setup without a username now
[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 $opt_d $opt_v);
8 use Getopt::Std;
9 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
10 use FS::CurrentUser;
11 use FS::Schema qw( dbdef_dist reload_dbdef );
12 use FS::Record;
13 #use FS::raddb;
14 use FS::Setup qw(create_initial_data);
15
16 die "Not running uid freeside!" unless checkeuid();
17
18 #my %attrib2db =
19 #  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
20
21 getopts("svd:");
22 #my $user = shift or die &usage;
23 getsecrets(); #$user);
24
25 #needs to match FS::Record
26 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
27
28 ###
29
30 #print "\nEnter the maximum username length: ";
31 #my($username_len)=&getvalue;
32 my $username_len = 32; #usernamemax config file
33
34 #print "\n\n", <<END, ":";
35 #Freeside tracks the RADIUS User-Name, check attribute Password and
36 #reply attribute Framed-IP-Address for each user.  You can specify additional
37 #check and reply attributes (or you can add them later with the
38 #fs-radius-add-check and fs-radius-add-reply programs).
39 #
40 #First enter any additional RADIUS check attributes you need to track for each 
41 #user, separated by whitespace.
42 #END
43 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
44 #                         split(" ",&getvalue);
45 #
46 #print "\n\n", <<END, ":";
47 #Now enter any additional reply attributes you need to track for each user,
48 #separated by whitespace.
49 #END
50 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
51 #                   split(" ",&getvalue);
52 #
53 #print "\n\n", <<END, ":";
54 #Do you wish to enable the tracking of a second, separate shipping/service
55 #address?
56 #END
57 #my $ship = &_yesno;
58 #
59 #sub getvalue {
60 #  my($x)=scalar(<STDIN>);
61 #  chop $x;
62 #  $x;
63 #}
64 #
65 #sub _yesno {
66 #  print " [y/N]:";
67 #  my $x = scalar(<STDIN>);
68 #  $x =~ /^y/i;
69 #}
70
71 #my @check_attributes = (); #add later
72 #my @attributes = (); #add later
73 #my $ship = $opt_s;
74
75 ###
76 # create a dbdef object from the old data structure
77 ###
78
79 my $dbdef = dbdef_dist;
80
81 #important
82 $dbdef->save($dbdef_file);
83 &FS::Schema::reload_dbdef($dbdef_file);
84
85 ###
86 # create 'em
87 ###
88
89 $FS::CurrentUser::upgrade_hack = 1;
90 my $dbh = adminsuidsetup; #$user;
91
92 #create tables
93 $|=1;
94
95 foreach my $statement ( $dbdef->sql($dbh) ) {
96   warn $statement if $statement =~ /TABLE cdr/;
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 create_initial_data('domain' => $opt_d);
108
109 warn "Freeside database initialized - commiting transaction\n" if $opt_v;
110
111 $dbh->commit or die $dbh->errstr;
112 $dbh->disconnect or die $dbh->errstr;
113
114 warn "Database initialization committed successfully\n" if $opt_v;
115
116 sub dbdef_create { # reverse engineer the schema from the DB and save to file
117   my( $dbh, $file ) = @_;
118   my $dbdef = new_native DBIx::DBSchema $dbh;
119   $dbdef->save($file);
120 }
121
122 sub usage {
123   die "Usage:\n  freeside-setup -d domain.name [ -v ] user\n"; 
124 }
125
126 1;
127