#! /tools/bin/perl -w

require '/usr/www/push/sqlcommon20.pl';
require '/usr/www/push/common20.pl';
require '/usr/www/push/auth.pl';
use DBI;

my $version=shift;
my $time=`/bin/date +\"%Y%m%d\"`;
chomp($time);
my $release="www_".$version;
my $prod_dir="/o/prod/current/";
my $release_dir=$prod_dir.$release;
my $base_dir="/usr/www/push";
#my $machine="stage1";
my $machine="fed1";
##################################################
my $ssh="/tools/bin/ssh";

&auth;

if (! defined $version) {
        print "Please define the release!!! (2_4_3) \n";
        exit 1;
} # End of if

my ($tag_base,$branch_tag)=&get_base_tag_name();
print "SHORT-$tag_base BRANCH-$branch_tag\n";
my $tag_name=$tag_base . "_final_$time";

#&switch_links();
#&update_db();
&tag_it($tag_name);
&mailit($version,$tag_name);
#&merge_it($branch_tag,$tag_name,$tag_base);

##################################################################
# This will do the actual switching of links to put the code live
##################################################################

sub switch_links
{
my $cmd="$ssh bd\@$machine \"cd /o/prod/current; rm www.e2.stage; rm -rf $release_dir/guest-cache; rm -rf $release_dir/partner-cache; mkdir $release_dir/guest-cache $release_dir/partner-cache; cd $prod_dir; ln -s www_staged www.e2.stage; rm www.e2; ln -s $release_dir www.e2; cd $release_dir/www; rm wtb; ln -s /o/prod/current/wtb wtb \" ";
print "$cmd\n";
&ssh_pipe($cmd);
} # End of switch_links

##################################################################
# This will update the tags table of the database
##################################################################

sub update_db
{
my $dbh=generic_connect_to_db('push20');
my $query="select id from tags where status='www'";
print "QUERY-$query\n";
my $www_id=&query_db_one($dbh,$query);

my $update_www="update tags set status='archived', user_name='none' where id=$www_id";
print "UPDATE_WWW-$update_www\n";
&sql_exec($dbh,$update_www);

my $query2="select id from tags where status='stage'";
print "QUERY2-$query2\n";
my $stage_id=&query_db_one($dbh,$query2);

my $update_stage="update tags set status='www', user_name='cvsbot' where id=$stage_id";
print "UPDATE_STAGED-$update_stage\n";
&sql_exec($dbh,$update_stage);

} # End of update_db

##################################################################
# This will create the tag
##################################################################

sub tag_it
{
my $cmd="cd $base_dir; mv prod_branch prod_branch.old; mv qa_branch prod_branch; mkdir qa_branch; rm -rf $base_dir/prod_branch.old";
print "$cmd\n";
&ssh_pipe($cmd);
my $cmd2="cd $base_dir/prod_branch; cvs tag $tag_name";
print "$cmd2\n";
&ssh_pipe($cmd2);
} # End of tag_it

##################################################################
# This will determine the base tag name
##################################################################

sub get_base_tag_name
{
my $tag_file="$base_dir/qa_branch/www/docroot/CVS/Tag";
my $value;
my $trash;

open(FILE,$tag_file);
my @lines=<FILE>;
close(FILE);
chomp(@lines);

foreach $line (@lines) {
        ($trash,$value)=split(/T/,$line);
} # End of foreach
my $pos=rindex($value,"_");
my $tag_short=substr($value,0,$pos);
return($tag_short,$value);
} # End of get_base_tag_name

##################################################################
# This will merge anything on the branch back onto the trunk
##################################################################

sub merge_it
{
my $branch_tag=shift;
my $tag_name=shift;
my $tag_base=shift;

my $cmd="cd /usr/www/push/scripts; ./merge.pl $branch_tag $tag_name $tag_base";
print "$cmd\n";
&ssh_pipe($cmd);
} # End of merge_it

##################################################################
# This will send out email
##################################################################

sub mailit
{
my $release=shift;
my $tagname=shift;

#my $people='walt@epinions-inc,wmiller@epinions-inc.com';
my $people="push-announce\@epinions-inc.com";
my $cc='Cc: wmiller@epinions-inc.com';
my $sendmailexec="/usr/lib/sendmail -t";

open (M, "| $sendmailexec ") || die ("sendmail failed to launch correctly aborting restart of");
print "Sending out email notification...\n";
print M "To: $people\n${cc}\nSubject: Release $release is live on www...\n\n";
print M "Release $release has been pushed to production.  The static tag\n\n$tagname\n\nrepresents the snapshot tag.\n";
close (M);
} # End of mailit
