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