#!/usr/bin/perl

# cpanel - cPanel/Ecommerce/OSCommerce/mysql_normalized_dump.pl Copyright(c) 2013 cPanel, Inc.
#                                                            All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use warnings;

use Getopt::Long;

my $input  = 'src/catalog/install/oscommerce.sql';
my $output = 'dump.mysql';
GetOptions(
    'help' => \&printHelp,

    'input=s'  => \$input,
    'output=s' => \$output,
);

# Open output file and print header line.
open OUT, '>', $output or die "Cannot open file '$output': $!";
print OUT "USE [% osc %];\n\n";

open IN, '<', $input or die "Cannot open file '$input': $!";
while (<IN>) {

    # Normalize the values and then print them to the file

    # Normalize the install path.
    s!(?<=')/tmp/?(?=',)![% installdir %]/includes/work/!g;

    # Normalize the email address.
    s/\broot\@localhost\b/[% email %]/g;
    s/(?<='MODULE_PAYMENT_PAYPAL_EXPRESS_SELLER_ACCOUNT', ')(?=', 'The email address)/[% email %]/;

    # Normalize the username.
    s/(?<='STORE_OWNER', ')Harald Ponce de Leon(?=', 'The name of)/[% username %]/;
    s/(?<='EMAIL_FROM', ')[^<]*(?= <\[% email %\]>)/[% username %]/g;

    print OUT $_;
}
close IN or warn "Cannot close file '$input': $!";

# Add the cPanel user as an administrator.
print OUT "\n# cPanel User\n";
print OUT "INSERT INTO `administrators` (`id`, `user_name`, `user_password`) VALUES (1, '[% username %]', '[% password_md5_salt %]');\n";

close OUT or warn "Cannot close file '$output': $!";

exit 0;

sub printHelp {
    print <<EOH;
Usage: $0 [OPTIONS]

Options:
   -i <file>      Input file (defaults to '$input').
   -o <file>      Output file (defaults to '$output').
EOH
}
