#!/usr/bin/perl

use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;
use Tie::IxHash;
use Digest::MD5 qw(md5_hex);
use Mail::Sender;
use DBI;

use vars qw/
%CONFIG
$template
%form
%cookie
%session
%user
%projects
$page
@subpage
%set_cookies
$dbh
$sth
@notes
/;

sub Initialize;
sub Db_Connect;
sub Db_Disconnect;
sub Do_Login;
sub Get_Session;
sub Try_Login;
sub Load_Template;
sub Output_Tmpl;
sub Login;
sub Sendmail;


sub Set_Vars {
#    $CONFIG{email_recover_pass}='/home/support/www2/tmpl/recover_pass.email';
     $CONFIG{update_news_comment}='Changes commited to CVS';
}


sub Log_Changes {

    Log("Log changes..");
    Log($ARGV[0]);

    my $file=$ARGV[0];
    $file=~s! \- Imported.*$!!gi;
    $file=~s! \- New.*$!!gi;

    $file=~s!$CONFIG{cvs_root}!!;
    $file=~s!/cvsroot/$ENV{CVS_USER}/?!!;
    $file=~s!^/!!isg;
    
    # delete cvsget cache
    if ($CONFIG{cvsget_cache_folder}=~m!/home/oc/www/tmp!i) {
        my ($to_del)=split(/\//,$file);
        my $cmd="rm -rf $CONFIG{cvsget_cache_folder}/$to_del*";
        Log($cmd);
        `$cmd`;
    }

    $sth=$dbh->prepare("delete from lintqueue where file=''");
    $sth->execute();

    my $pname=Get_Project_From_File($file);
    if ($pname) {
        # lint

        $sth=$dbh->prepare("insert into lintqueue (user,file,date,status) values (?,?,?,?)");
        $sth->execute($ENV{CVS_USER},$file,time,'QUEUED');

        # project news update

        $sth=$dbh->prepare("select comment,id from pnews where project=? order by date desc limit 1");
        $sth->execute($pname);
        my ($comment,$id)=$sth->fetchrow_array;

        $sth=$dbh->prepare("update projects set updated=? where name=?");
        $sth->execute(time,$pname);

        unless ($comment=~m!$CONFIG{update_news_comment}!i) {
            $sth=$dbh->prepare("insert into pnews(project,user,comment,date) values(?,?,?,?)");
            $sth->execute($pname,$ENV{CVS_USER},$CONFIG{update_news_comment},time);
        } else {
            $sth=$dbh->prepare("update pnews set date=? where id=?");
            $sth->execute(time,$id);
        }
    }
}

sub Main {
    do '/home/oc/www2/global.pl';
    Initialize;
    Set_Vars;
    Db_Connect;

    Log_Changes;

    Db_Disconnect;
}

Main;
