correct realtime_bop cvv handling
[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_u $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("u:vd:");
22 #my $user = shift or die &usage;
23
24 getsecrets($opt_u); #$user);
25
26 #needs to match FS::Record
27 my($dbdef_file) = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
28
29 ###
30
31 my $username_len = 32;
32
33 #print "\n\n", <<END, ":";
34 #Freeside tracks the RADIUS User-Name, check attribute Password and
35 #reply attribute Framed-IP-Address for each user.  You can specify additional
36 #check and reply attributes (or you can add them later with the
37 #fs-radius-add-check and fs-radius-add-reply programs).
38 #
39 #First enter any additional RADIUS check attributes you need to track for each 
40 #user, separated by whitespace.
41 #END
42 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
43 #                         split(" ",&getvalue);
44 #
45 #print "\n\n", <<END, ":";
46 #Now enter any additional reply attributes you need to track for each user,
47 #separated by whitespace.
48 #END
49 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
50 #                   split(" ",&getvalue);
51 #
52 #print "\n\n", <<END, ":";
53 #Do you wish to enable the tracking of a second, separate shipping/service
54 #address?
55 #END
56 #my $ship = &_yesno;
57 #
58 #sub getvalue {
59 #  my($x)=scalar(<STDIN>);
60 #  chop $x;
61 #  $x;
62 #}
63 #
64 #sub _yesno {
65 #  print " [y/N]:";
66 #  my $x = scalar(<STDIN>);
67 #  $x =~ /^y/i;
68 #}
69
70 #my @check_attributes = (); #add later
71 #my @attributes = (); #add later
72 #my $ship = $opt_s;
73
74 ###
75 # create a dbdef object from the old data structure
76 ###
77
78 my $dbdef = dbdef_dist(datasrc);
79
80 #important
81 $dbdef->save($dbdef_file);
82 &FS::Schema::reload_dbdef($dbdef_file);
83
84 ###
85 # create 'em
86 ###
87
88 $FS::CurrentUser::upgrade_hack = 1;
89 my $dbh = adminsuidsetup $opt_u; #$user;
90
91 #create tables
92 $|=1;
93
94 foreach my $statement ( $dbdef->sql($dbh) ) {
95   $dbh->do( $statement )
96     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
97 }
98
99 #now go back and reverse engineer the db
100 #so we pick up the correct column DEFAULTs for #oidless inserts
101 dbdef_create($dbh, $dbdef_file);
102 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
103 reload_dbdef($dbdef_file);
104
105 create_initial_data('domain' => $opt_d);
106
107 warn "Freeside database initialized - commiting transaction\n" if $opt_v;
108
109 $dbh->commit or die $dbh->errstr;
110 $dbh->disconnect or die $dbh->errstr;
111
112 warn "Database initialization committed successfully\n" if $opt_v;
113
114 sub dbdef_create { # reverse engineer the schema from the DB and save to file
115   my( $dbh, $file ) = @_;
116   my $dbdef = new_native DBIx::DBSchema $dbh;
117   $dbdef->save($file);
118 }
119
120 sub usage {
121   die "Usage:\n  freeside-setup -d domain.name [ -v ]\n"
122   # [ -u user ] for devel/multi-db installs
123 }
124
125 1;
126
127