#!/usr/bin/perl

# Script written to batch-place ads on www.barnstormers.com
# Commissioned by Paul Miller from Mathias Moe (mathias@jpg.no)

use utf8;
use DBI;
use DBD::Pg;
use URI::Encode qw/uri_encode/;

use lib "./";
# use Aquinas qw(indexfile books);
require "./barnstormers.pl";

# Settings
my $username = "pjdmiller";
my $password = "eym5shBgiKDN";
my $dbname   = "";
my $dbuser   = "";
my $dbpass   = "";


# Test data
my $headline = 'GTX-330(ES) ADSB XPNDR';
my $price    = '2195';
my $actionphrase = 'FREE DELIVERY';
my $adbody       = 'New in box from Garmin upgrade, 8130, ADSB out';
my $manufacturer = 'Garmin';
my $model        = 'GTX330ES';
my $partnumber   = '011-00455-60';
my $iam          = 'Owner';
my $category1    = 'Avionics';
my $subcategory1 = 'Garmin';
my $category2    = 'Avionics';
my $subcategory2 = 'Transponder';



# Step 1. Connect to database.
# my $dbh      = 

# Step 2 -- log in to barnstormers and aquire session cookie -- the cookies are stored in a separate file, cookie.txt. 
# Ignore output. Assume everything went OK

# First make a get-request to get the cookie

print qq!\n\n-------\n\n!;
print qq!curl -v -b cookie.txt -c cookie.txt https://www.barnstormers.com/login.php\n!;
#$output = `curl -v -c cookie.txt https://www.barnstormers.com/login.php`;
$output = `curl -s -c cookie.txt https://www.barnstormers.com/login.php`;
#print $output;

# Then try to log in

#print qq!\n\n-------\n\n!;
print qq!curl -v -b cookie.txt -c cookie.txt -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "username=$username&password=$password" https://www.barnstormers.com/login.php\n!;
$output = `curl -s -b cookie.txt -c cookie.txt -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "username=$username&password=$password" https://www.barnstormers.com/login.php`;
#print $output;

# Then comfirm adress -- this might not be nescessary
#print qq!\n\n-------\n\n!;
#print qq!curl -v -b cookie.txt -c cookie.txt https://www.barnstormers.com/acct.php\n!;
$output = `curl -s -b cookie.txt -c cookie.txt https://www.barnstormers.com/acct.php`;
#print $output;


# Step 3 -- Fetch new ads from database. Post them one by one.

my $moreads = 1;
while ($moreads) {
	
	# Prepare part 1
	my $actionnumber = $actionphrases{$actionphrase} || '';
	
	# TODO: MAKE SURE HEADING IS NO MORE THAT 30 CHARS LONG
	my $data = ("headline=" . uri_encode($headline));
	# price
	$data .= ("&price=" . uri_encode($price));
	# action phrase (number)
	$data .= "&action_phrase=$actionnumber";
	# ad-body
	$data .= ("&body=" . uri_encode($adbody));
	# lister is ...
	$data .= ("&lister_is=" . uri_encode($iam));
	#manufacturer
	$data .= ("&mfg=" . uri_encode($manufacturer));
	#model
	$data .= ("&model=" . uri_encode($model));
	# part/serial-number
	$data .= ("&part_num=" . uri_encode($partnumber));
	# advance to next page
	$data .= "&action=activate";
	$data .= "&next_page=next_step";
	
	# Note the "-L" option, the server will redirect to a new page that contains the listing-id
	$output = `curl -s -b cookie.txt -c cookie.txt -L -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "$data" https://www.barnstormers.com/edit_basic_listing.php`;
	
	print qq!\n\n-------\n\n!;
	print $output;
	
	print qq!\n\n-------\n\n!;
	print qq!curl -b cookie.txt -c cookie.txt -L -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "$data" https://www.barnstormers.com/edit_basic_listing.php\n!;
	print $output;
	
	# Capture the listing-id
	$output =~ m/<input.*?name=\'listing_id\'.*?value='(\d+)'>/;
	#$output =~ m/edit_categories.php\?id=(\d+)&action=activate/;
	$listingid = $1;
	
	# See if we have a listing-id. That means the previous step was successful
	if ($listingid) {
		# required fields
		
		print qq!Listing-id: $listingid\n!;
		
		$data = 'continue=1';
		$data .= "&listing_id=$listingid";
		$data .= '&action=activate';
		$data .= '&submitForm=Continue';

		# Map categories and sub-categories to barnstormers numeric representation	
		my $c1 = $maincategories{$category1}    || 0;
		my $s1 = $sec_categories{$subcategory1} || 0;

		my $c2 = $maincategories{$category2}    || 0;
		my $s2 = $sec_categories{$subcategory2} || 0;

		my $c3 = $maincategories{$category3}    || 0;
		my $s3 = $sec_categories{$subcategory3} || 0;
		
		# The parameters names categories[1] .. 3 and sub_categories[1] .. 3 are uri-encoded below. The parameter values are numeric.
		$data .= "&categories[1]=$c1&sub_categories[1]=$s1&categories[2]=$c2&sub_categories[2]=$s2&categories[3]=$c3&sub_categories[3]=$s3";
		
		# Prepare a new post
#		$output = `curl -v -b cookie.txt -c cookie.txt -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "$data" https://www.barnstormers.com/manager/edit_categories.php?id=$listingid&action=activate`;
		$output = `curl -s -b cookie.txt -c cookie.txt -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "$data" https://www.barnstormers.com/manager/useraddcategories.php`;
		print qq!\n\n-------\n\n!;
		print qq!curl -v -b cookie.txt -c cookie.txt -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "$data" https://www.barnstormers.com/manager/useraddcategories.php\n!;
		print $output;
		
		# Activate the AD
		$data = "ad=$listingid";
		$data .= "&willreplaceid=";
		$data .= "&update=";
		$data .= "&process=1";
		$data .= "&action=activate";
		
		$output = `curl -s -b cookie.txt -c cookie.txt -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "$data" https://www.barnstormers.com/activate-ad.php`;
		print qq!\n\n-------\n\n!;
		print qq!curl -v -b cookie.txt -c cookie.txt -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "$data" https://www.barnstormers.com/activate-ad.php\n!;
		
		}	
	
	else {
		print qq!\n\nNO LISTING ID -- SOMETHING WENT WRONG\n\n!;
		}
	
	
	$moreads = 0;
	}




