#!/usr/bin/perl
# cfb 1.0.4 03jul2006
# executed automatically by ./bloombergBBMX, in turn exec by 
# /etc/daily.local
# massage Bloomberg MassIndex quotes
# 1.0.1: correct month indexing
# 1.0.2: field N.A. price quotes
# 1.0.3: correct quote date format
# 1.0.4: modified for new Bloomberg page format a/o 27jun2006
print "\nBloombergMAIndex v1.0.4\n\n";
print "C F Baum  Jul 2006\n\n";
use LWP::Simple;
use Text::Wrap;
$dir = "MassInd";
$tick = "BBMX";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$yy = $year+1900;
$mon++;
$quote = $yy.$mon.$mday;
#$ouf=">/volumes/loki1/fmwww/repec/RAIM/".$dir."/bloomberg_".$tick."_".$quote.".raw";
#$ou2=">/volumes/loki1/fmwww/repec/RAIM/".$dir."/bloomberg_".$tick.".do";
$ouf=">/fmwww0/repec/RAIM/".$dir."/bloomberg_".$tick."_".$quote.".raw";
$ou2=">/fmwww0/repec/RAIM/".$dir."/bloomberg_".$tick.".do";
#$ouf=">bloomberg_".$tick."_".$quote.".raw";
#$ou2=">bloomberg_".$tick.".do";
open(OUT,$ouf) || die "Cannot open $ouf for output";
open(OU2,$ou2) || die "Cannot open $ou2 for output";
$ymd = $yy."-".$mon."-".$mday;
print "$ymd\n";
#$str = get("http://quote.bloomberg.com/apps/data?pid=ri_members&Ticker=$tick");
# corr 7C15
$str = get("http://www.bloomberg.com/apps/data?pid=ri_members&Ticker=$tick");

#print "$str\n";

$str =~ s/&amp;/\&/igm;
$str =~ s/,//igm;
$str =~ s!ticker=.+?>!!igm;
#(@chunk) = ($str =~ /<span class="style5block">(.+?)</ig);
(@chunk) = ($str =~ /<td><a href="\/apps\/quote\?(.+?)\n/igm);

$chunk = @chunk;
#print "chunk: $chunk\n";
$i = 0;
$ll = 0;
$ff = 0;
foreach(@chunk) {

	$temp = $_;
#	print "$temp\n\n";
	$temp =~ s/<td align="right">/\t/igm;
	$temp =~ s!<\/td>!!igm;
	$temp =~ s!<\/a>!!igm;
	print OUT "$ymd\t$temp\n";
	$ll++;
}
	
#		if ($ff) {
#			$i++;
#			if($i < 6) {
#			print OUT "$_ \t";
#			}
#		else {
#			print OUT "$_ \n";
#			print OUT "$ymd\t";
#			$i = 0;
#			$ll++;
#			}
#		}
#		if ($ff==0) {print OUT "$ymd\t";}
#		$ff++;	
#}

print "# $quote  $chunk  $ll\n";
#print OU2 "cd \"/volumes/loki1/fmwww/repec/RAIM/".$dir."/\"\n";
print OU2 "cd \"/fmwww0/repec/RAIM/".$dir."/\"\n";
print OU2 "!date\n clear\n";
print OU2 "insheet qdate firm price change pctchg vol tstamp using bloomberg_".$tick."_$quote\n";
print OU2 "foreach v of varlist price change pctchg vol {\n";
print OU2 "	destring `v', gen(s_`v') force\n";
print OU2 "	capture confirm numeric variable s_`v'\n";
print OU2 "	if _rc == 0 {\n";
print OU2 "		drop `v'\n";
print OU2 "		rename s_`v' `v'\n";
print OU2 "		}\n";
print OU2 "	}\n";
print OU2 "g qd = date(qdate,\"ymd\") \n";
print OU2 "format qd \%td \n";
print OU2 "g qy=year(qd) \n g qm = month(qd)\n g qdy=day(qd)\n";
print OU2 "g long sqdate=qy*10000+qm*100+qdy \n local sqdate=sqdate[1] \n";
print OU2 "drop if price ==. \n";
#print OU2 "save bloomberg_".$tick."_$quote, replace \n";
print OU2 "save bloomberg_".$tick."_`sqdate', replace \n";
__END__
