--- /dev/null
+#!/usr/bin/perl\r
+#\r
+# Copyright (C) 2008 Luke Reeves\r
+#\r
+# This program is free software; you can redistribute it and/or modify\r
+# it under the terms of the GNU General Public License as published by\r
+# the Free Software Foundation; either version 2 of the License, or\r
+# (at your option) any later version.\r
+#\r
+# This program is distributed in the hope that it will be useful,\r
+# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+# GNU General Public License for more details.\r
+#\r
+# You should have received a copy of the GNU General Public License\r
+# along with this program; if not, write to the Free Software\r
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307\r
+# USA\r
+#\r
+\r
+package Insipid::DBInstall;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+use Insipid::Config;\r
+use Insipid::Schemas;\r
+\r
+use DBI qw/:sql_types/;;\r
+use vars qw($version);\r
+\r
+use Exporter ();\r
+our (@ISA, @EXPORT);\r
+       \r
+@ISA = qw(Exporter);\r
+@EXPORT = qw(install);\r
+\r
+sub install {\r
+       my ($sth, $dbname, $dbuser, $dbpass, $dbtype, $dsn, $dbh, @creates);\r
+\r
+       $dbname = getconfig('dbname');\r
+       $dbuser = getconfig('dbuser');\r
+       $dbpass = getconfig('dbpass');\r
+\r
+       if(defined(getconfig('dbtype'))) {\r
+               $dbtype = getconfig('dbtype');\r
+       } else {\r
+               $dbtype = 'mysql';\r
+       }\r
+\r
+       $dsn = "DBI:$dbtype:dbname=$dbname;host=localhost";\r
+       $dbh = DBI->connect($dsn, $dbuser, $dbpass, { 'RaiseError' => 0}) or die $DBI::errstr;\r
+\r
+       print "Content-Type: text/html\r\n\r\n";\r
+       print "<html><head><title>Insipid Installation</title></head><body>";\r
+\r
+       print "<p>Creating tables...";\r
+\r
+       if($dbtype eq 'mysql') {\r
+               @creates = split(/\;/, $createMySQL);\r
+       } else {\r
+               @creates = split(/\;/, $createPostgres);\r
+       }\r
+\r
+       foreach(@creates) {\r
+               my $sql = $_;\r
+               if(length($sql) > 2) {\r
+                       $sth = $dbh->prepare($sql);\r
+                       $sth->execute() or print "<br />Error executing \"$sql\" - $DBI::errstr<br />";\r
+               }\r
+       }\r
+       print " done!</p>";\r
+\r
+       print "<p>Insipid's database has been installed.  You can reload this " .\r
+               "page to start using Insipid.</p>";\r
+       \r
+       print "</body></html>";\r
+\r
+}\r
+\r
+1;\r
+__END__\r
 
 }\r
 \r
 sub dbupgrade {\r
-       print STDERR "Upgrading Insipid database...\n";\r
-\r
        my $sql = "update $tbl_options set value = ? where (name = ?)";\r
        my $sth = $dbh->prepare($sql);\r
        $sth->execute($version, 'version');\r
                while(my $hr = $sth->fetchrow_hashref) {\r
                        $options{$hr->{'name'}} = $hr->{'value'};\r
                }\r
-               $sth->close();\r
        }\r
 \r
        my ($name) = (@_);\r
        return $options{$name};\r
 }\r
 \r
-sub install {\r
-       my ($sth, @creates);\r
-\r
-       print STDERR "Performing database installation.\n";\r
-       \r
-       print "Content-Type: text/html\r\n\r\n";\r
-       print "<html><head><title>Insipid Installation</title></head><body>";\r
-\r
-       print "<p>Creating tables...";\r
-\r
-       if($dbtype eq 'mysql') {\r
-               @creates = split(/\;/, $createMySQL);\r
-       } else {\r
-               @creates = split(/\;/, $createPostgres);\r
-       }\r
-\r
-       foreach(@creates) {\r
-               my $sql = $_;\r
-               if(length($sql) > 2) {\r
-                       $sth = $dbh->prepare($sql);\r
-                       $sth->execute() or print "<br />Error executing \"$sql\" - $DBI::errstr<br />";\r
-               }\r
-       }\r
-       print " done!</p>";\r
-\r
-       print "<p>Insipid's database has been installed.  You can reload this " .\r
-               "page to start using Insipid.</p>";\r
-       \r
-       print "</body></html>";\r
-\r
-}\r
-\r
 # This configures the URLs in the application to support mod_rewrite or\r
 # a webserver sans mod_rewrite.\r
 if(get_option('use_rewrite') eq 'yes') {\r