#!/usr/bin/perl

$EXPORT="/var/www/www.sexycostumes.org.uk/db";
open EXPORT or die "can't open $EXPORT";
@products=<EXPORT>;
close EXPORT;

$TEMPLATE="/var/www/www.sexycostumes.org.uk/product.html";
open TEMPLATE or die "can't open $TEMPLATE";
@pagetemplate=<TEMPLATE>;
close TEMPLATE;

foreach $product (@products){
    ($productid, $title, $description, $category, $price, $quantity, $url, $imagelarge, $imagemedium, $imagesmall, $longdescription, $department, $suppliercode) = split "\t", $product;
    unless ($price eq '0.00' or
            $category eq 'PC_BONUS_CATEGORY' or
            $category eq 'DISCONTINUED' or
            $category eq 'Gift Certificates') {

	$url="http://www.awin1.com/awclick.php?mid=854&id=66611&p=$url";

	$catfile=$category;

	$catfile =~ s/&trade;//g;
	$catfile =~ s/&amp;//g;
	$catfile =~ s/[^a-zA-Z_0-9 \t\n\r\f]//g;
	$catfile =~ s/\s+/ /g;
	$catfile =~ s/\s/_/g;
	$catfile =~ s/\W//g;
	$catfile =~ s/_/-/g;
	$catfile =~ tr/A-Z/a-z/;

	$titlefile=$title;
	$titlefile =~ s/&trade;//g;
	$titlefile =~ s/&amp;//g;
	$titlefile =~ s/[^a-zA-Z_0-9 \t\n\r\f]//g;
	$titlefile =~ s/\s+/ /g;
	$titlefile =~ s/\s/_/g;
	$titlefile =~ s/\W//g;
	$catfile =~ s/_/-/g;
	$titlefile =~ tr/A-Z/a-z/;
	$titlefile = "$titlefile.htm";

	$chosendescription=$description;

	$image = $imagelarge;

	$pick=int(rand(4));
	if ($pick == 0) {
	    $seealso="<a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Nun+Costumes\">Nun Costumes</a>, <a href=\"http://www.sexycostumes.orgwww..uk/search.cgi?q=Vinyl+Outfit\">Sexy+Vinyl+Outfit</a>, <a href=\"http://sexycostumes.org.uk/search.cgi?q=Sexy+Costumes\">Sexy Costumes</a>, <a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Sexy+Pirate+Costumes\">Sexy Pirate Costumes</a>";
	}
	elsif ($pick ==1) {
	    $seealso="<a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Muscle+Chest\"> Muscle Chest Costumes</a>, <a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Sexy+Vinyl\">Sexy Vinyl Costumes</a>, <a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Cheeky+Outfits\">Cheeky Outfits</a>, <a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Super+Sexy+Costume\">Super Sexy Costume</a>";
	}
	else {
	    $seealso="<a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Novelty+Costumes\">Joke Costumes</a>, <a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Pink+Lady+Costume\">Pink Lady Costume</a>, <a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Sexy+Outfits\">Sexy Outfits</a>, <a href=\"http://www.sexycostumes.org.uk/search.cgi?q=Sexy+Halloween+Costumes\">Sexy Halloween Costumes</a>";
	}

#	$seealso="<a href=\"http://www.sexycostumes.org.uk/fancy-dress\">Sitemap</a>, $seealso";

	my @template = @pagetemplate;
	foreach $templateline (@template) {
	    $templateline =~ s/XXTITLE/$title/g;
            $templateline =~ s/XXCHOSENDESCRIPTION/$chosendescription/g;
            $templateline =~ s/XXCATEGORY/$category/g;
            $templateline =~ s/XXPRICE/$price/g;
	    $templateline =~ s/XXTFILE/$titlefile/g;
	    $templateline =~ s/XXCATFILE/$catfile/g;
	    $templateline =~ s/XXIMAGE/$image/g;
	    $templateline =~ s/XXURL/$url/g;
	    $templateline =~ s/XXCATDROPDOWN/$varcatdropdown/g;
	    $templateline =~ s/XXSEEALSO/$seealso/g;
	}
	system ("mkdir /var/www/www.sexycostumes.org.uk/fancy-dress/$catfile");
	open FILE, ">/var/www/www.sexycostumes.org.uk/fancy-dress/$catfile/$titlefile";
	print FILE @template;
	close FILE;
    }
}

