#!/usr/bin/perl
use DBI;
my ($globals);
$globals->{'bindir'}   = "/usr/bin/";
$globals->{'etcdir'}   = "/etc/lyricue/";
$globals->{'basedir'}  = $ENV{'HOME'} . "/.lyricue/";
$globals->{'sharedir'} = "/usr/share/lyricue/";

#
# You shouldn't have to change anything after this line
#

# convenience variables for true and false
use constant FALSE => 0;
use constant TRUE  => 1;

$globals->{'version'}        = "1.9.2";
$globals->{'accessfile'}     = $globals->{'etcdir'} . "access.conf";
$globals->{'defaultconf'}    = $globals->{'etcdir'} . "default.conf";
$globals->{'configfile'}     = $globals->{'basedir'} . "config2";
$globals->{'bgpath_user'}    = $globals->{'basedir'} . "backgrounds/";
$globals->{'imgpath_user'}   = $globals->{'basedir'} . "images/";
$globals->{'bgpath_system'}  = $globals->{'sharedir'} . "backgrounds/";
$globals->{'imgpath_system'} = $globals->{'sharedir'} . "images/";
$globals->{'gladefile'}      = $globals->{'sharedir'} . "lyricue.glade";
$globals->{'host'}           = "localhost";
$globals->{'lyricdb'}        = "lyricDb";
$globals->{'mediadb'}        = "mediaDb";

my $mediaDbh =
  DBI->connect("DBI:mysql:$globals->{'mediadb'}:$globals->{'host'}", "lyric",
          "") || display_fatal($errorcodes->{'lyricdbopen'}, $DBI::errstr);

my ($query, $row, $sth, $rv);

if ($ARGV[0] eq "-h") {
	print "Usage: import_media <type> <directory>\n";
	print "       where type is bg or img\n";
	print "       and dir is the directory to import\n";
	exit;
}
$sth = $mediaDbh->prepare(q{INSERT INTO media(category, subcategory, type, format, insertedby, insertdate, description, data) VALUES (?,?,?,?,?,?,?,?)});
my ($owner) =getpwuid($<);
print $owner."\n";
if ( $owner eq "root") { $owner="general" }
my $type = $ARGV[0];
my $dir = $ARGV[1]."/";
$dir =~ s/\//\\\//g;
$dir =~ s/ /\\ /g;
my $dirlisting = `find $dir -follow -type f | sed -e 's/^$dir//g' | grep -v ".thumbnails/" | grep -v ".xvpics/" | grep -v "thumbnails/"`;
my @items = split (/\n/, $dirlisting);
my @date = localtime(time);
my $time = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $date[5]+1900,$date[4],$date[3],$date[2],$date[1],$date[0]);
foreach (@items) {
    my $filename = $_;
    my ($category,$rest) = split (/\//,$filename, 2);
    if ($rest eq "") {
	$rest=$category;
	$category="Main";
    }
    my $format = $rest;
    $format =~ s/^.*\.//g;
    my $description = $rest;
    $description =~ s/^.*\///g;
    $description =~ s/\..*?$//g;
    my $subcategory = $rest;
    $subcategory =~ s/$description//g;
    $subcategory =~ s/\.$format//g;
    $subcategory =~ s/\/$//g;
    print $filename."\n";
    #print "C#",$category,"-S#",$subcategory,"-F#",$format,"-D#",$description,"\n";
    open (MEDIA,$ARGV[1]."/".$filename);
    $data="";
    while (<MEDIA>) {
	$data .= $_;
    }
    close MEDIA;
    $rv = $sth->execute($category,$subcategory,$type,$format,$owner,$time,$description,$data);
}

#$sth = $mediaDbh->prepare("ALTER IGNORE TABLE media ADD UNIQUE INDEX(data(100),description,category,subcategory,type,format)");
#$rv = $sth->execute;


$mediaDbh->disconnect;

