#!/usr/bin/perl -w
use strict;

# this is a wrapper around rt-setup-database to
# get it to run as the regular RT user (from RT_SiteConfig.pm)
# without exposing the password on the command line

# we feed the password into the rt-setup-database stdin
# when it looks like it's asking for it

# everything rt-setup-database says is tee'd to the RT logger
# so that the administrator can find the real output in the
# syslog when this is run from the Asset Tracker maintainer
# scripts

use Getopt::Std;
use IPC::Open3;
use IO::Select;
use Symbol;
use POSIX ":sys_wait_h";

my %opts;

getopts('v:', \%opts) or usage();

sub usage {
    print STDERR <<EOF;
Usage: $0 [ -v ( 3.4 | 3.6 | 3.8 | 4 )] [-- <options for rt-setup-database>]
EOF
    exit 1;
}

my $rt_version = "4";
if (exists $opts{v}) {
    if ($opts{v} =~ /^(3\.[468]|4)$/) {
        $rt_version = $1;
    } else {
        usage();
    }
}
 
unshift @INC, "/usr/local/share/request-tracker${rt_version}/lib";
unshift @INC, "/usr/share/request-tracker${rt_version}/lib";

require RT;

RT::LoadConfig();

RT::InitLogging();

my $t;
$t = $RT::DatabaseUser;
$t = $RT::DatabasePassword;
$t = $RT::Logger;

my ($child_out, $child_in, $child_err) = ("","",gensym);

my @cmd = (
    "/usr/sbin/rt-setup-database-${rt_version}", 
    "--dba", 
    $RT::DatabaseUser,
    "-p",
    @ARGV,
);
my $cmd = join(" ", @cmd);

$RT::Logger->notice("running $cmd\n");

my $pid = open3($child_in, $child_out, $child_err, @cmd);


my $s = IO::Select->new;
$s->add($child_out);
$s->add($child_err);

my $password_sent=0;
while ($s->handles and my @ready = $s->can_read(10)) {
    for my $fh (@ready) {
        my $ret = sysread($fh, my $buf, 1024);
        if (not defined $ret) {
            die("sysread failed: $!");
        }
        if ($ret == 0) {
            $s->remove($fh);
            next;
        }
        print STDERR $buf if $fh eq $child_err;

        $RT::Logger->notice("rt-setup-database-${rt_version}: $buf")
            if $fh eq $child_err or $password_sent;

        if ($fh == $child_out) {
            if ($buf =~ /Password:/ && !$password_sent++) {
                print $child_in $RT::DatabasePassword ."\n" 
            }
        }
    }
}

my $i = 0;
do {
    if (waitpid($pid, WNOHANG) > 0) {
        my $ret = $?;
        exit $ret >> 8;
    }
    sleep 1;
} while ($i++ < 5);
