<?php
// once_a_day_report.php
// RJM Programming
// September, 2015
// Report on snapshot of Yahoo YQL asx_international_look.php and asx_look.php data
//    eg. http://www.rjmprogramming.com.au/ITblog/phpjavascripthtml-public-json-asx-international-tutorial/
// Good with crontab like ...
// 58 23 * * *  curl http://www.rjmprogramming.com.au/PHP/JSON/once_a_day_report.php

   ini_set('max_execution_time', 600);  
   $filter = " | sed '/^[^\"]*\"industry-group\"/s//~\"industry-group\"/g' | tr '\\n' '\\f' | tr '~' '\\n' | tr '\\f' ' ' | tr '\\t' ' ' | sed '/  /s// /g' | sed '/ :/s//:/g' | cut -d':' -f'2,4,6' | cut -d',' -f'2,3' | cut -d':' -f'2,3' | cut -d',' -f'2,1' | awk -F':' '{ print $2 \" \" $1}' | sort -r | sed '/^ /s///g' | awk -F',' '{ print $2 \" \" $1}' | sed '/GICS Sector Code Not Applicable_/s//GICS_Sector_Code_Not_Applicable_/g' | sed '/Classification Pending_/s//Classification_Pending_/g' | uniq -f 2 | sort "; 
   $startdate = new DateTime();
   $rnow = $startdate->format("Y-m-d_H_i_s");
   $rhour = $startdate->format("Y-m-d_H");
   $rday = $startdate->format("Y-m-d");
   $rmonth = $startdate->format("Y-m");
   $ryear = $startdate->format("Y");
   $prevyear = $ryear--;
   if (!isset($_GET['nosleep']) && !isset($_POST['nosleep'])) sleep(120);
   if (file_exists("dreports/aonceaday.rpt" . $rmonth)) {
    exec("fgrep '" . '" : "' . "' *_" . $rday . "*.dat " . $filter . " | tee -a  dreports/aonceaday.rpt" . $ryear . ".html  dreports/aonceaday.rpt" . $rmonth . ".html  dreports/aonceaday.rpt" . $rday . ".html; cat dreports/aonceaday.rpt" . $rmonth . ".html | sort | uniq -f 2 >  dreports/aonceaday.rpt" . $ryear . ".html; cp -f dreports/aonceaday.rpt" . $ryear . ".html dreports/aonceaday.rpt" . $rmonth . ".html");
   } else if (file_exists("dreports/aonceaday.rpt" . $ryear)) {
    exec("fgrep '" . '" : "' . "' *_" . $rday . "*.dat " . $filter . " | tee -a  dreports/aonceaday.rpt" . $ryear . ".html  dreports/aonceaday.rpt" . $rmonth . ".html  dreports/aonceaday.rpt" . $rday . ".html; cat dreports/aonceaday.rpt" . $rmonth . ".html | sort | uniq -f 2 >  dreports/aonceaday.rpt" . $ryear . ".html; cp -f dreports/aonceaday.rpt" . $ryear . ".html dreports/aonceaday.rpt" . $rmonth . ".html");
   } else {
    exec("fgrep '" . '" : "' . "' *_" . $ryear . "-*.dat " . $filter . " > dreports/aonceaday.rpt" . $ryear . ".html");
    exec("fgrep '" . '" : "' . "' *_" . $rmonth . "-*.dat " . $filter . " > dreports/aonceaday.rpt" . $rmonth . ".html");
    exec("fgrep '" . '" : "' . "' *_" . $rday . "*.dat " . $filter . " > dreports/aonceaday.rpt" . $rday . ".html");
   }
   if (!file_exists("dreports/aonceaday.rpt" . $prevyear))  exec("fgrep '" . '" : "' . "' *_" . $prevyear . "-*.dat " . $filter . " > dreports/aonceaday.rpt" . $prevyear . ".html");
   exit
      
?>
