#!/usr/bin/perl
use strict;
use FindBin qw($Bin);
use lib $Bin;
use onserver_star;
use File::Path

setup();

my $dir = "/mit/$USER/scripts-gallery2/$sqldb";
if (-e $dir) {
    print STDERR "ERROR: The directory $dir already exists.\nTry selecting a different name.";
    exit 1;
}
mkpath($dir);

system("patch", "install/steps/AuthenticateStep.class",
       "/mit/scripts/deploy$scriptsdev/gallery2.patch");

$ua->cookie_jar({file => '.cookies'});

fetch_uri('install/index.php');
fetch_uri('install/index.php', {step => 1},
	  {language => 'en_US'});
fetch_uri('install/index.php', {step => 2}, {});
fetch_uri('install/index.php', {step => 3}, {});

my $post4 = {isMultisite => 0,
	    dir => $dir,
	    action => 'save'};
fetch_uri('install/index.php', {step => 4}, $post4);
fetch_uri('install/index.php', {step => 4}, $post4);

my $post5 = {type => 'mysql',
	     hostname => $sqlhost,
	     action => 'save',
	     confirmReuseTables => '',
	     confirmCleanInstall => '',
	     username => $sqluser,
	     password => $sqlpass,
	     database => $sqldb,
	     tablePrefix => 'g2_',
	     columnPrefix => 'g_'};
fetch_uri('install/index.php', {step => 5}, $post5);
fetch_uri('install/index.php', {step => 5}, $post5);

fetch_uri('install/index.php', {step => 6},
	  {adminName => $admin_username,
	   passwordA => $admin_password,
	   action => 'create',
	   passwordB => $admin_password,
	   email => $email,
	   fullName => $USER});
fetch_uri('install/index.php', {step => 7}, {});
fetch_uri('install/index.php', {step => 8}, {});
fetch_uri('install/index.php', {step => 9},
	  {'module[imagemagick]' => 'on',
	   activate => 1});
fetch_uri('install/index.php', {step => 10}, {});
fetch_uri('install/index.php', {step => 11}, {});

unlink '.cookies';
