#!/usr/bin/perl

use strict;

use lib '/app/tools/common/lib';

use Getopt::Std;
use Common::Consts;

use lib '/app/tools/common/lib';
use Common::RSDB;

use lib '/app/tools/data_classes/lib';
use File::File;
use Client::Service;

use lib '/app/tools/sale_import/lib';
use Sale::File;

use lib '/app/tools/common/lib';
use Common::Util;

my @clientIDs;
push @clientIDs, _getLocalClientIDs();

foreach my $clientID (@clientIDs) {
    next unless ($clientID);
    print $clientID. "\n";
    my $dbo = new Common::RSDB( client_id => $clientID );
    my $dbh = $dbo->DBH;

    my $sql = "TRUNCATE product_digital";
    $dbo->DoCmd($sql);

    $sql = "SELECT product_id FROM product WHERE product_type_id = 3";
    my $sth = $dbo->DoCmd($sql);
    while ( my ($product_id) = $sth->fetchrow_array() ) {
        $sql = "INSERT INTO product_digital SET product_id = $product_id, date_created = NOW()";
        my $sth = $dbo->DoCmd($sql);
    }
}

sub _getLocalClientIDs {

    # We have a nifty little script that will give us a list of client databases.
    #
    my $clientListString = `/app/tools/rps/bin/db/list_client_dbs.pl`;
    chomp $clientListString;

    # Find the client id that matches each database.
    #
    my @ids;
    my @dbNames = split( / /, $clientListString );
    foreach my $dbName (@dbNames) {
        my $id = Common::RSDB::DBNameToClientID($dbName);
        push @ids, $id;

    }

    return @ids;
}
