#!/usr/bin/perl

use strict;
use warnings;

use File::Find;
use Term::ReadKey;
use Cwd;

my $basedir = @ARGV[0];
my $workdir = cwd();

my $username = '';
my $password = '';

if (!$username) {
    print "Enter DB username: ";
    chomp($username = ReadLine(0));
    print "Enter DB password: ";
    ReadMode('noecho');
    chomp($password = ReadLine(0));
    ReadMode('normal');
    print "\n";
}
# Actions, Conditions and templates have to go first. Before the Scrips are created
find ( sub {
  if ($File::Find::name =~ m/(Action|Condition|Template).*\.install$/i)
  {
    print "Installing from file $File::Find::name\n";
    system($basedir . "/sbin/rt-setup-database --action insert --dba=\"$username\" --dba-password=\"$password\" --datafile $File::Find::name");
  }
}, $workdir);

find ( sub {
  if ($File::Find::name =~ m/(Scrip).*\.install$/i)
  {
    print "Installing from file $File::Find::name\n";
    system($basedir . "/sbin/rt-setup-database --action insert --dba=\"$username\" --dba-password=\"$password\" --datafile $File::Find::name");
  }
}, $workdir);
