merge in changes to rt-setup-database
[freeside.git] / fs_webdemo / registerd.Pg
index a4fd055..f166846 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 #
-# $Id: registerd.Pg,v 1.1 2001-04-23 12:40:30 ivan Exp $
+# $Id: registerd.Pg,v 1.11 2001-10-24 15:29:30 ivan Exp $
 
 use strict;
 use vars qw(
@@ -50,7 +50,7 @@ die "not running as root user"
 $dbh = DBI->connect( $datasrc, $user, $pass, {
        'AutoCommit' => 'true',
 } ) or die "DBI->connect error: $DBI::errstr\n";
-$x = $DBI::errstr; #silly; to avoid "used only once" warning
+#$x = $DBI::errstr; #silly; to avoid "used only once" warning
 
 while ( 1 ) {
 
@@ -65,13 +65,13 @@ while ( 1 ) {
   $sth->execute or die $sth->errstr;
 
   $sth = $dbh->prepare(
-    'SELECT * FROM mysql_auth WHERE status = "unconfigured" OR status = "reconfigure"'
+    'SELECT * FROM mysql_auth WHERE status = "unconfigured"'
   );
   $sth->execute or die $sth->errstr;
   my $pending = $sth->fetchall_arrayref( {} );
 
   $sth = $dbh->prepare(
-    'UPDATE mysql_auth SET status = "locked" WHERE status = "unconfigured" OR status = "reconfigure"'
+    'UPDATE mysql_auth SET status = "locked" WHERE status = "unconfigured"'
   );
   $sth->execute or die $sth->errstr;
 
@@ -84,11 +84,29 @@ while ( 1 ) {
 
     my $username = $row->{'username'};
     my $email = $row->{'passwd'};
-    my $status = $row->{'status'};
 
-    system("/usr/bin/createdb --username freeside demo_$username >/dev/null");
+    my $pdbh = DBI->connect( 'DBI:Pg:host=localhost;dbname=demo_template', 'freeside', 'maelcolm' )
+      or do { &myerr("$username: ". $DBI::errstr); next; };
 
-    system "psql -U freeside demo_$username </usr/local/etc/freeside/demo_template.Pg";
+    my $psth = $pdbh->prepare("CREATE DATABASE demo_$username")
+      or do { &myerr("$username: ". $pdbh->errstr); next; };
+    $psth->execute()
+      or do { &myerr("$username: ". $psth->errstr); next; };
+
+    $pdbh->disconnect
+      or do { &myerr("fatal: $DBI::errstr"); die; };
+
+    open(PSQL,"|psql -U freeside demo_$username")
+      or do { &myerr("|psql -U freeside demo_$username: $!"); next; };
+    open(PSQLDATA, "</usr/local/etc/freeside/demo_template.Pg")
+      or do { &myerr("/usr/local/etc/freeside/demo_template.Pg: $!"); next; };
+    while(<PSQLDATA>) {
+      print PSQL $_;
+    }
+    close PSQLDATA
+      or do { &myerr("/usr/local/etc/freeside/demo_template.Pg: $!"); next; };
+    close PSQL
+      or do { &myerr("|psql -U freeside demo_$username: $!"); next; };
 
     mkdir "${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username", 0755;    
     system "cp -pr ${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_template/* ".
@@ -113,15 +131,15 @@ while ( 1 ) {
     print LPR "mail $email";
     close LPR;
 
-    open(FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from")
-      or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from: $!";
-    print FROM "$email\n";
-    close FROM;
-
-    open(TO, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to")
-      or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to: $!";
-    print TO "$email\n";
-    close TO;
+#    open(FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from")
+#      or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from: $!";
+#    print FROM "$email\n";
+#    close FROM;
+#
+#    open(TO, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to")
+#      or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to: $!";
+#    print TO "$email\n";
+#    close TO;
 
     open(SECRETS, ">${freeside_conf}secrets.demo_$username")
       or die "Can\'t open ${freeside_conf}secrets.demo_$username: $!";
@@ -145,41 +163,40 @@ while ( 1 ) {
     );
     $sth->execute or die $sth->errstr;
 
-    if ( $status eq "unconfigured" ) {
-
-      #$ENV{SMTPHOSTS} = "localhost";
-      $ENV{SMTPHOSTS} = "192.168.1.1";
-      $ENV{MAILADDRESS} = 'ivan-fsreg@sisd.com';
-      $ENV{TZ} = "PST8PDT";
-      $header = Mail::Header->new( [
-        'From: ivan-fsreg@sisd.com',
-        "To: $email",
-        'Bcc: ivan-fsreg_bcc@sisd.com',
-        'Sender: ivan-fsreg@sisd.com',
-        'Reply-To: ivan-fsreg@sisd.com',
-        #'Date: '. time2str("%a, %d %b %Y %X %z", time ),
-        'Date: '. time2str("%a, %d %b %Y %X ", time ). "-0800",
-        'Subject: Freeside demo information',
-      ] );
-      $msg = Mail::Internet->new(
-        'Header' => $header,
-        'Body' => [
-      "Hello,\n",
-      "\n",
-      "Your sample Freeside database has been setup.\n",
-      "\n",
-      "Point your web browswer at http://freeside.sisd.com/ and use the following\n",
-      "authentication information:\n",
-      "\n",
-      "Username: $username\n",
-      "Password: $user_pw\n",
-      "\n",
-      "-- \n",
-      "ivan\n",
-                  ]
-      );
-      $msg->smtpsend or die "Can\'t send registration email!";
-    }
+    #$ENV{SMTPHOSTS} = "localhost";
+    $ENV{SMTPHOSTS} = "192.168.1.1";
+    $ENV{MAILADDRESS} = 'ivan-fsreg@sisd.com';
+    $ENV{TZ} = "PST8PDT";
+    $header = Mail::Header->new( [
+      'From: ivan-fsreg@sisd.com',
+      "To: $email",
+      'Bcc: ivan-fsreg_bcc@sisd.com',
+      'Sender: ivan-fsreg@sisd.com',
+      'Reply-To: ivan-fsreg@sisd.com',
+      #'Date: '. time2str("%a, %d %b %Y %X %z", time ),
+      'Date: '. time2str("%a, %d %b %Y %X ", time ). "-0800",
+      'Subject: Freeside demo information',
+    ] );
+    $msg = Mail::Internet->new(
+      'Header' => $header,
+      'Body' => [
+    "Hello,\n",
+    "\n",
+    "Your sample Freeside database has been setup.\n",
+    "\n",
+    "Your login and database will be automatically deleted in 1-2 months.\n",
+    "\n",        
+    "Point your web browswer at http://freeside.sisd.com/ and use the following\n",
+    "authentication information:\n",
+    "\n",
+    "Username: $username\n",
+    "Password: $user_pw\n",
+    "\n",
+    "-- \n",
+    "ivan\n",
+                ]
+    );
+    $msg->smtpsend or die "Can\'t send registration email!";
 
   }
 
@@ -194,3 +211,11 @@ while ( 1 ) {
 
 }
 
+sub myerr {
+  my $msg = shift;
+  open(MAIL,"|mail ivan-fsdemoerr\@420.am");
+  print MAIL $msg, "\n\n";
+  print MAIL $msg, "\n\n";
+  close MAIL;
+};
+