#!/usr/bin/perl

#<input type="hidden" name="ropen">
#<INPUT NAME="__chx" VALUE="REPORT  " TYPE="HIDDEN">


$pager = $ENV{PAGER} || 'less';

$auth = shift;

print "I need a pass eh: ";
system("stty -echo");
$pass = <STDIN>;
chop $pass;
system("stty echo");

#print "$auth $symbols\n";

$symbols =~ s/,/%2c/g;

$query = 'curl -u ' . $auth . ':' . $pass . ' -d "rdhis&__chx=REPORT%20%20" https://orders5.datek.com';

$page = qx|$query|;

open (OHOH, "|$pager");
print OHOH $page;
close OHOH;

# I want to match whatever is inside a <TD></TD> set provided it is
# not a HTML tag. Starting with the first TR Found.


$page =~ s|
		<TR BGCOLOR="\#cccccc">
		.		# matches the newline
		(<TD.+?</TD>)	# Enter Date
		.		# matches the newline
		(<TD.+?</TD>)	# Enter Time
		.		# matches the newline
		(<TD.+?</TD>)	# Exec Time
		.		# matches the newline
		(<TD.+?</TD>)	# Buy/Sell
		.		# matches the newline
		(<TD.+?</TD>)	# Qty
		.		# matches the newline
		(<TD.+?</TD>)	# Stock
		.		# matches the newline
		(<TD.+?</TD>)	# Price
		.		# matches the newline
		(<TD.+?</TD>)	# Order Type
		.		# matches the newline
		(<TD.+?</TD>)	# Expire
		.		# matches the newline
		(<TD.+?</TD>)	# Status
		.		# matches the newline
		(<TD.+?</TD>)	# Cxl (cancel ?)
		.		# matches the newline
		(<TD.+?</TD>)	# Net Change
		.		# matches the newline
		(<TD.+?</TD>)	# Balance
		.		# matches the newline
		(<TD.+?</TD>)	# Tracking
		||smx;

$headers = [$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14]; 

$x = 0;
while($page =~ s|
		<tr\sbgcolor="\#......">
		.
		(<td.+?</td>)	# Enter Date
		.
		(<td.+?</td>)	# Enter Time
		.
		(<td.+?</td>)	# Exec Time
		.
		(<td.+?</td>)	# Buy/Sell
		.
		(<td.+?</td>)	# Qty
		.
		(<td.+?</td>)	# Stock
		.
		(<td.+?</td>)	# Price
		.
		(<td.+?</td>)	# Order Type
		.
		(<td.+?</td>)	# Expire Type
		.
		(<td.+?</td>)	# Status
		.
                (<td.+?</td>) # cancel name
		.
#                <TD.+?NAME="([^"]+)".+?</TD> # cancel name
#                <TD.+?NAME="([^"]+?)" # cancel name
		(<td.+?</td>)	# Net Change
		.
		(<td.+?</td>)	# Balance
		.
		(<td.+?</td>)	# Tracking
		.
		</tr>
		||smx)
	 {
	   $openord->[$x] = [$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14]; 
	   $x++;
#	   print "$1\n$2\n$3\n$4\n$5\n$6\n$7\n$8\n$9\n$10\n$11\n\n"
	 }

print  $page if $DEBUG;

sub detag { 
  $ohoh = shift;
  while ($ohoh =~ s/<.+?>/ /) {}
  $ohoh =~ s/<.+?>/ /g;
  $ohoh =~ s/\&nbsp\;//g;
  $ohoh =~ s/\s\s+/ /g;
  return $ohoh;
}

#	print $blat->[0][0];

open(PAGE,"|$pager") || die"Can't open $pager :$!";

#print PAGE $openord->[0][9] . "\n";
#print PAGE $openord->[1][9] . "\n";

for ($listem = 0; $listem < $x; $listem++) {
  for ($line = 0; $line < 14; $line++) {
    print PAGE detag($headers->[$line]) . "\t";
    print PAGE detag($openord->[$listem][$line]) . "\n";
  }
  print PAGE "============\n\n";
}
close PAGE;


print "\n\nEnter record number of order to cancel (starts at 0):\t";
$anum = <STDIN>;
chop $anum;

$cancelit=$openord->[$anum][9];

$query = 'curl -u ' . $auth . ':' . $pass . ' -d "' . $cancelit . '=checked&__chx=ORDER%20%20%20&try=Cancel%20checked%20orders" https://orders5.datek.com';

print "\nConfirm ,  ctrl-c  otherwise...\t";

$page = qx|$query|;

$page =~ s|
	NAME="([^"]+)"\sVALUE="checked"
	||smx;

$checked=$1;

$query = 'curl -o gone -u ' . $auth . ':' . $pass  . ' -d "userpasswd=' . $pass . '&' . $checked . '=checked&cancel=Cancel%20these%20orders&__chx=ORDER%20%20%20" https://orders9.datek.com/trading/Orderpg.htm';

print $checked . "\n";

print "\nConfirm or ctrl-c to exit NOW :\t";
$confirm = <STDIN>;

$page = qx|$query|;
