#!/usr/bin/perl -w

# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

use strict;

use Getopt::Long;
use Carp qw(confess);
GetOptions("dryrun", \my($dryrun), "name:s", \my($name), "sql", \my($sqlonly));

use lib 'extlib';
use lib 'lib';

use MT;
use MT::Upgrade;

my $mt = new MT(Config => 'mt.cfg') or die MT->errstr;

MT->add_callback('MT::Upgrade::SQL', 1, undef, \&sql_cb) if $sqlonly;

$dryrun = 1 if $sqlonly;

if (!$sqlonly) {
    print "upgrade -- A command line tool for upgrading the schema for Movable Type.\n";
    print "(Non-destructive mode)\n" if $dryrun;
}

my $install;
my $driver = MT::Object->driver;
if (!$driver || !$driver->table_exists('MT::Author')) {
    $install = 1;
}

unless ($install || $name) {
    print "Please set username to set superuser at upgrading.  cf: upgrade --name Melody\n";
    exit;
}

my $author_id;
if (!$install && $name) {
   require MT::BasicAuthor;
   my $a = MT::BasicAuthor->load({name => $name});
   die "Not found user $name:" . MT::BasicAuthor->errstr if !$a;
   $author_id = $a->id || 0;
}

my $updated = MT::Upgrade->do_upgrade(App => 'main', 
                                      DryRun => $dryrun,
                                      Install => $install,
                                      SuperUser => $author_id,
                                      CLI => 1,
                                      );

if ($install) {
    print "Installation complete.\n";
} else {
    print "Upgrade complete!\n" if !$dryrun && $updated;
    print "Your schema is up to date already.\n" if defined $updated && !$updated;
}

sub progress {
    my $pkg = shift;
    my $msg = shift;
    print "\t* " . $msg . "\n" unless $sqlonly;
}
sub error {
    my $pkg = shift;
    my $err = shift;
    confess $err;
}
sub sql_cb {
    my $cb = shift;
    my ($upgrade, $stmt) = @_;
    print "$stmt\n";
}
