BABYL OPTIONS:
Version: 5
Labels:
Note:   This is the header of an rmail file.
Note:   If you are seeing it in rmail,
Note:    it means the file has no messages in it.

0, unseen,,
*** EOOH ***
Return-path: <value@b1.t1.valueclick.com>
Envelope-to: terry@hcoop.net
Delivery-date: Tue, 07 Dec 2004 23:44:04 +0000
Received: from Debian-exim by Abulafia.hcoop.net with spam-scanned (Exim 4.34)
	id 1Cbozv-0005vL-7L
	for terry@hcoop.net; Tue, 07 Dec 2004 23:44:04 +0000
Received: from robocop.valueclick.com ([216.34.207.2] helo=b1.t1.valueclick.com)
	by Abulafia.hcoop.net with esmtp (TLS-1.0:DHE_RSA_3DES_EDE_CBC_SHA:24)
	(Exim 4.34)
	id 1Cbozu-0005vI-Rn
	for terry@hcoop.net; Tue, 07 Dec 2004 23:43:59 +0000
Received: from b1.t1.valueclick.com (localhost.localdomain [127.0.0.1])
	by b1.t1.valueclick.com (8.12.8/8.12.8) with ESMTP id iB7NhtmM003054
	for <terry@hcoop.net>; Tue, 7 Dec 2004 15:43:55 -0800
From: value@b1.t1.valueclick.com
Received: (from value@localhost)
	by b1.t1.valueclick.com (8.12.8/8.12.8/Submit) id iB7NhtYH003050;
	Tue, 7 Dec 2004 15:43:55 -0800
Date: Tue, 7 Dec 2004 15:43:55 -0800
Message-Id: <200412072343.iB7NhtYH003050@b1.t1.valueclick.com>
To: terry@hcoop.net
Subject: i i i
X-Spam-Checker-Version: SpamAssassin 2.64 (2004-01-11) on Abulafia.hcoop.net
X-Spam-Level: 
X-Spam-Status: No, hits=0.3 required=5.0 tests=NO_REAL_NAME autolearn=no 
	version=2.64


#!/home/value/apps/perl -w
use strict;

use Carp qw(cluck);
use Getopt::Long;


use VC::DB qw(vcdb_open);

use VC::System::Utils;
use VCt::network;

use Commands::Guarded;
use Params::Validate;
use Pod::Usage;
use YAML qw(LoadFile);

my $opt_debug = 1;
my $dbh = vcdb_open("VC", {RaiseError => 1, PrintError => 1});
my $locale = new VC::Locale;
my %config;
my @errors;
my @config_order = qw(network_type network_name
		      domain ksa_domain
                      default_redirect_url banner_server
                      redirect_server content_server
                      currency
                     );

my %config_examples =
  (
   network_type     => 'vclk, clka, dyna',
   network_name     => 'ValueClick, ClickAgents, Foo Inc',
   network_code     => 'a, b, c',
   domain           => 'valueclick.com, foo.valueclick.net, ads.foo.com',
   ksa_domain       => 'user_id cookie domain, valueclick.net, foo.com',
   default_redirect_url => 'http://www.valueclick.com/, http://www.foo.com/',
   banner_server   => 'oz, ads',
   redirect_server  => 'oz, redir',
   content_server    => 'st, stclst, img',
   currency         => "USD, JPY",
);
                              

Commands::Guarded::verbose(1);

my $getopt = GetOptions
  (
   debug       => \$opt_debug,
   'f=s'       => \my $opt_file, 
   'h|help|?'  => \my $opt_help,
   man         => \my $opt_man,
   undo        => \my $opt_undo,
   ignore_net  => \my $opt_ignore_net
  ) ;

pod2usage(-verbose => 2)
  if $opt_man ;

pod2usage(1)
  if ($opt_help or not $opt_file or not $getopt) ;

my $network_code = VCt::network->available_code  
  or die "no network codes available. See VCt::network for details.";

use Data::Dumper; 

load_config(file => $opt_file);
unless (validate_config()) {
  warn join "\n", @errors;
  die;
}


create_network();

exit;

sub mywarn {
  my ($message, $level) = @_;

  cluck sprintf "%s$message", ("\t" x $level) ;
}

sub create_network {



  my $operation_id;
  step "populate VC.operations" =>
    using {
      $dbh->do("INSERT INTO operations (operation_title) VALUES (?)", {}, $config{network_name});
    }
    ensure { 
      $operation_id = 
	$dbh->selectrow_array(qq{select operation_id from operations where operation_title=?}, {}, $config{network_name});
    }
      ;

  my $subdivision_id;
  step "populate 'VC.subdivisions'" =>
    using { 
      $dbh->do(qq{insert into subdivisions (type,title,default_lang,alias,operation_id,network_type)
              values ("internal",?,"en",?,?,?)},
	       {},
	       "$config{network_name} subdivision", "a$operation_id", $operation_id, $config{network_type})
     }
    ensure {
      $subdivision_id = 
	$dbh->selectrow_array(qq{select subdivision_id from subdivisions
                             where operation_id=?},
			      {},
			      $operation_id
			     )
    } ;


  step "populate VC.subdivision_currency_xref" =>
    using {
      $dbh->do(qq{insert into subdivision_currency_xref (subdivision_id,currency_code)
              values (?,?)},
	       {},
	       $subdivision_id,$config{currency} 
	      )
    }
    ensure {
      $dbh->selectrow_array('select * from subdivision_currency_xref where subdivision_id=? and currency_code=?', {}, $subdivision_id, $config{currency})
    } ;
      
  step "populate VC.networks" =>
    using {
      $dbh->do(qq{INSERT INTO networks
             (network_type, network_name, network_code) VALUES (?,?,?)},
	       {},
	       $config{network_type}, $config{network_name}, $network_code)
    }
    ensure {
      $dbh->selectrow_array('select * from networks where network_type=?', {}, $config{network_type})
    };


  for my $attr qw(banner_server redirect_server content_server domain 
		  ksa_domain default_redirect_url) 
    {
      step "\tpopulate VC.network_attribute $attr" =>
	using {
	  $dbh->do('INSERT INTO network_attribute 
               (network_type, attr_name, attr_value) VALUES (?,?,?)',
	       {},
	       $config{network_type}, $attr, $config{$attr})
	}
	ensure {
	  $dbh->selectrow_array('select * from network_attribute where network_type=? AND attr_name=? AND attr_value=?', {}, $config{network_type}, $attr, $config{$attr})
	};

    }


  my @rev_types =
    (
     [qw(campaign cpc)],
     [qw(campaign cpm)],
     ["hostsite", "cpc,cpm"],
     [qw(hostsite cpc)],
     [qw(hostsite cpm)],
    );

  my ($rev_type, $context);
  my $step = step "\tpopulate VC.rev_type_usage" =>
    using {
      ($context, $rev_type) = ($_[0]->[0], $_[0]->[1]) ;
      $dbh->do(qq{insert into rev_type_usage (context, rev_type, network_type)
                values (?,?,?)},
	       {},
            $context, $rev_type, $config{network_type})
    }
    ensure {
      ($context, $rev_type) = ($_[0]->[0], $_[0]->[1]) ;
      $dbh->selectrow_array('select * from rev_type_usage where context=? AND rev_type=? AND network_type=?', {}, $context, $rev_type, $config{network_type})
    };

  $step->do_foreach(@rev_types);

  my %default_sizes = qw(
	468x60	a
	small	i
	medium	j
	large	k
	full	l
  );

  $step =  step "\tpopulate VC.network_banner_sizes" =>
    using {
      $dbh->do(qq{insert into network_banner_sizes (network_type,size,code,host,campaign)
		values(?,?,?,'visable','visable')},
             {},
            $config{network_type}, $_[0], $default_sizes{$_[0]})
    }
    ensure {
      $dbh->selectrow_array('select * from network_banner_sizes where size=? AND code=? AND network_type=?', {}, $_[0], $default_sizes{$_[0]}, $config{network_type})
    };

  $step->do_foreach(keys %default_sizes);

}

sub validate_config {
  @errors = ();
  for my $key (@config_order) {
    push @errors, "$key not defined" unless $config{$key};
  }
  
  unless ($opt_ignore_net) {
    push @errors, "network_type already exists. either remove this network_type from networks for run with -ignore_net"
      if $dbh->selectrow_array(qq{select * from networks where network_type=?},{},$config{network_type});
  }
    
  push @errors, "unknown currency '$config{currency}'"
    unless $dbh->selectrow_array(qq{select * from currency where currency_code=?},{},$config{currency});


  return 0 if @errors;
  return 1;
}

sub save_config {
  my $file = shift;
  open F, ">$file" or die "Could not open $file for writing: $!";
  print F "quit: \n";
  for my $error (@errors) {
    print F "# ERROR: $error\n";
  }
  print F "\n" if @errors;
  for my $key (@config_order) {
    $config{$key} ||= "";
    print F "# $key: $config_examples{$key}\n"
      if $config_examples{$key};
    print F "$key:\t$config{$key}\n\n";
  }
  close F or die "closing file $file: $!";
  print "Saved config in $file\n";
}

sub load_config {
  my %p = validate(@_, { file => 1 }) ;

  my $config = LoadFile($p{file}) ;
  %config = %$config;
}

sub edit_config {

  my $dir = $locale->{home_dir} . "/workfiles/tmp";
  run "mkdir -p $dir" unless -e $dir;
  my $file = "$dir/new_network.$$" . rand;
  my $editor = $ENV{EDITOR} || $ENV{VISUAL} || 'vi';

  save_config($file);
  system("$editor $file");
  load_config($file);

}

sub dbh_do {
  my ($sql, @parms) = @_;
  $dbh = vcdb_open;

  $dbh->do()

}


=head1 NAME

new_network - tool to create a new network (and its required 1 subdivision)

=head1 SYNOPSIS

 new_network [options]

=head1 OPTIONS

=over 4

=item * -f $filename (required)

supply the name of the configuration file describing the new network

=item * -ignore_net

If you want to re-run this script and ignore the validation code which 
prevents running the script if the network exists, then set this flag.

=item * -help

print usage docs

=item * -h or -?

same as C<-help>

=item * -man

full docs

=back

=head1 USAGE

=over 4

=item * Create a configuration file as defined by L<YAML>

=item * run new_network specifying the file:

  new_network -f config.yml

=back

=head1 CONFIGURATION FILE

=head2 Required parameters

=over 4


=item * network_type

a 4-character field. Example

 network_type : tcdc

=item * network_name

a free-form English name for the field. Example

 network_name: Thomascook.com

=item * domain

 domain: Thomascook.com

=item * ksa_domain

 ksa_domain: cookie

=item * default_redirect_url

 default_redirect_url : http://www.valueclick.com/

=item * banner_server

 banner_server:    oz

=item * redirect_server

 redirect_server:  oz
 
=item * content_server

 content_server: st

=item * currency

 currency: GBP

=back


=head1 AUTOMATIC BEHAVIOR

=over 4

=item * VC.operations table

will have the network name added as C<operation_title>

=item * VC.subdivisions table

will have a subdivision with the same name as C<network_name> created

=item * VC.subdivision_currency_xref table

will have C<currency> added as a currency for this subdivision

=item * B<MAJOR SWITCH>: VC.networks

The earlier version of this program wrote a number of attributes to
VC.networks. However, the attributes for VC.networks are now stored in
VC.network_attribute, a table created by F<perl-hashify-table.pl>.

The only things to be inserted into VC.networks are C<network_name>, 
C<network_type>, and C<network_code>. Of these 3 attributes, the first 
2 must be supplied in the config file. The latter is generated by this
program. 

The other attributes in the config file go into VC.network_attribute.

=item * VC.rev_type_usage

A number of values as a function of C<network_type> are populated in 
this table.

=item * VC.network_banner_sizes

A number of default banner sizes are added to VC.network_banner_sizes

=back

