#! /tools/bin/perl  -w

use DBI;
use strict;
use vars qw ($dbh $sth $sth2);
my $dbh=&ConnectToDatabase("push20");
my $dir;
my $root;
my @areas=("trunk","qa_branch","prod_branch","beta_branch");
my $area;

foreach $area (@areas) {
	my $query = " select id,name from dir_names";
	print "QUERY-$query\n";
	$sth2= $dbh->prepare(qq{ $query } ) || &choke($sth->errstr);
	$sth2->execute() || &choke ($sth->errstr);
	while (my ($id,$dir) = $sth2->fetchrow_array()) {
		$root="/usr/www/push/" . "$area/";
		chomp $dir;
		print "$dir\n";
		&get_all_versions($id,$dir,$root,$area);
	} # End of while
	$sth2->finish(); #cleanup
} # End of foreach

#------------------------------------------------------------------------
# Updates the db with the latest versions.
#------------------------------------------------------------------------

sub ConnectToDatabase {
    my $database = shift ;
    my ($dsn)="DBI:mysql:$database:keefer.epinions.com";
 DBI->trace(1, "/usr/www/push/var/cvsload20.out");
        $dbh = DBI->connect($dsn, "root", "")
            || die "$DBI::errstr - Can't connect to database server: .";
} # End of ConnectToDatabase

#------------------------------------------------------------------------
# Updates the db with the latest versions.
#------------------------------------------------------------------------

sub get_all_versions
{
my $id=shift ;
my $path=shift ;
my $root=shift ;
my $area=shift ;
my $m;
my @buf;
my $full_path = $root . $path . "/CVS/Entries";
print "FULL-$full_path\n";
$full_path =~  s/\/\//\//g; # get rid of // from path
#return if !(-e $full_path);
open(STAT,"$full_path");
@buf=<STAT>;
close(STAT);

foreach $m (@buf) {
        next if ($m =~ m/^D/);
        chomp ($m);
        my ($g,$filename,$version,$rest)=split (/\//,$m,4);
	my $filepath=$path."/$filename";
	print "FILE-$filepath VER-$version\n";
	&update_db($dbh,$id,$filepath,$version,$area);
} # End of foreach
} # End of get_all_versions

#------------------------------------------------------------------------
# Updates the db with the latest versions.
#------------------------------------------------------------------------

sub choke
{
my $reason=shift;
print "Failing because: $reason !!!!\n";
exit 1;
} # End of choke

#------------------------------------------------------------------------
# Updates the db with the latest versions.
#------------------------------------------------------------------------

sub update_db
{
my $dbh=shift;
my $id=shift;
my $filepath=shift;
my $version=shift;
my $area=shift;
my $insert;

my $version_type="$area"."_version";
my $query="select id from filepaths where path='$filepath'";
print "QUERY-$query\n";
$sth= $dbh->prepare(qq{ $query } ) || &choke($sth->errstr);
$sth->execute() || &choke ($sth->errstr);
my ($fileid) = $sth->fetchrow_array();
if ($sth->rows) {
	my $update="update filepaths set $version_type='$version' where id='$fileid'";
	print "UPDATE-$update\n";
	$sth= $dbh->prepare(qq{ $update } ) || &choke($sth->errstr);
	$sth->execute() || &choke ($sth->errstr);
	$sth->finish(); #cleanup
} else {
	if ($area eq "trunk") {
		$insert="insert into filepaths values (NULL,'$id','$filepath','$version',NULL,NULL,NULL,NULL)";
	} elsif ($area eq "qa_branch") {
		$insert="insert into filepaths values (NULL,'$id','$filepath',NULL,'$version',NULL,NULL,NULL)";
	} elsif ($area eq "prod_branch") {
		$insert="insert into filepaths values (NULL,'$id','$filepath',NULL,NULL,'$version',NULL,NULL)";
	} elsif ($area eq "beta_branch") {
		$insert="insert into filepaths values (NULL,'$id','$filepath',NULL,NULL,NULL,'$version',NULL)";
	} # End of if

	print "INSERT-$insert\n";
	$sth= $dbh->prepare(qq{ $insert } ) || &choke($sth->errstr);
	$sth->execute() || &choke ($sth->errstr);
	$sth->finish(); #cleanup
} # End of if

} # End of update_db
