<?php
// Author: Ken True - webmaster-weather.org
// gen-CUtags.php 
// Purpose: read the tags.txt file from Cumulus and generate the CUtags.htx file for use with
//          Cumulus to substitute weather values for Cumulus tags in CUtags.php
//
// Version 1.00 - 06-Jan-2011 - Initial release
// --------------------------------------------------------------------------
// allow viewing of generated source
$Version = 'gen-CUtags.php - V1.07 - 02-Jan-2013';
$WXsoftware = 'CU'; // do NOT change this
$defsFile = $WXsoftware . '-defs.php'; // do NOT change this .. name of definitions file

if (isset($_REQUEST["sce"]) and strtolower($_REQUEST["sce"]) == "view" ) {
//--self downloader --
   $filenameReal = __FILE__;
   $download_size = filesize($filenameReal);
   header("Pragma: public");
   header("Cache-Control: private");
   header("Cache-Control: no-cache, must-revalidate");
   header("Content-type: text/plain");
   header("Accept-Ranges: bytes");
   header("Content-Length: $download_size");
   header("Connection: close");
   
   readfile($filenameReal);
   exit;
}

$inFile = 'tags.txt';

if(!file_exists($inFile)) {
	print "<h3>Program: $Version</h3>\n";
	print "<h1>Error: file '$inFile' not found.</h1>\n";
	print "<p>Make sure '$inFile' is in this same directory.</p>";
	return;
}

$showComments = false;
if(isset($_REQUEST['comments'])) {
	$showComments = (strtolower($_REQUEST['comments']) == 'yes')?true:false;
}
$useBeta = true;
if(isset($_REQUEST['beta'])) {
	$useBeta = (strtolower($_REQUEST['beta']) == 'no')?false:true;
}

$rawrecs = file($inFile);
header("Content-type: text/plain");
print_start();
foreach ($rawrecs as $rec) {
/*
input:
Parameters (Description)
Cumulus Tag	Variable
<#date>	The current date
<#time>	The current time and date. Example result: 18:30 on 30 December 2009.  If you simply wish the time, use a combination of the day, month, year tags (below)
<#timehhmmss>	The current time. Example format: 18:30:27

output:
temp|<#temp>|// The outside temperature:|:
intemp|<#intemp>|// The inside temperature:|:
temptrend|<#temptrend>|// The change in temperature over the last hour:|:
temptrendtext|<#temptrendtext>|// Temperature change over the last hour - Rising/Falling/Steady:|:

*/
  preg_match('|^(\S+)\s+<#([^>]+)>\s+(.*)$|i',$rec,$matches);
  if(!isset($matches[2])) {continue;}
  if(!$useBeta and $matches[1] == 'beta') {continue; }
  if($matches[1] == 'omit') {continue; }
  
  $ourname = $matches[2];
  $descr = $matches[3];
  $varname = "<#$ourname>";
  if($useBeta and $matches[1] == 'beta') {
	  $ourname = '!'.$ourname;  // use ! as beta marker
  }
  $comment = '';
  if($showComments) {
	  $comment = '|// '.$descr;
  }
  print "$ourname|$varname$comment:|:\n";
	
	
}
print_end();

// end of mainline
function print_start() {
	global $Version,$WXsoftware,$defsFile,$inFile;
print '<?php
/*
 File: CUtags.txt

 Purpose: load Cumulus variables into a $WX[] array for use with the Canada/World/USA template sets

 Instructions:  
 Save this page as CUtags.txt and place in your cumulus\webfiles directory
 
 Use the Cumulus, Configuration, Internet, Files panel select an unused entry.

 In the Local Filenames, press Browse and select the CUtags.txt file as the local filename.
 In the Remote Filenames box, enter the directory and CUtags.php as the Remote Filename to use. 
 Leave the Binary? box unchecked,
 Check the Process? box.
 Leave the Realtime? box unchecked.
 Check the FTP? box.
 
 Press OK on the Internet Settings dialog to close it and save the settings.


 Author: Ken True - webmaster@saratoga-weather.org

';
print " (created by $Version)\n";

print "\n These tags generated on ".gmdate('Y-m-d H:m:s T',time())."\n";
print "   From $inFile updated ".gmdate('Y-m-d H:m:s T',filemtime($inFile))."\n\n";

print '*/
// --------------------------------------------------------------------------

// allow viewing of generated source

if (isset($_REQUEST["sce"]) and strtolower($_REQUEST["sce"]) == "view" ) {
//--self downloader --
   $filenameReal = __FILE__;
   $download_size = filesize($filenameReal);
   header("Pragma: public");
   header("Cache-Control: private");
   header("Cache-Control: no-cache, must-revalidate");
   header("Content-type: text/plain");
   header("Accept-Ranges: bytes");
   header("Content-Length: $download_size");
   header("Connection: close");
   
   readfile($filenameReal);
   exit;
}
' . 
"\$WXsoftware = '$WXsoftware';  
\$defsFile = '$defsFile';  // filename with \$varnames = \$WX['$WXsoftware-varnames']; equivalents\n " .
'
$rawdatalines = <<<END_OF_RAW_DATA_LINES
';
}

function print_end() {
global $WXsoftware, $defsFile;	
print 'END_OF_RAW_DATA_LINES;

// end of generation script

// put data in  array
//
$WX = array();
global $WX;
$WXComment = array();
$data = explode(":|:",$rawdatalines);
$nscanned = 0;
foreach ($data as $v => $line) {
  list($vname,$vval,$vcomment) = explode("|",trim($line).\'|||\');
  if(substr($vname,0,1) == "#") { //
    continue; // ignore comment records
  }
  if(substr($vname,0,1) == "!") { // handle beta tags conditionally
    if($vval == "") {
	  continue; // skip this one
	} else {
	  $vname = substr($vname,1);
	  if($vcomment <> "") {$vcomment .= " (Beta)"; }
	}
  }
  if ($vname <> "") {
    $WX[$vname] = trim($vval);
    if($vcomment <> "") { $WXComment[$vname] = trim($vcomment); }
  }
  $nscanned++;
}
if(isset($_REQUEST[\'debug\'])) {
  print "<!-- loaded $nscanned $WXsoftware \$WX[] entries -->\n";
}

if (isset($_REQUEST["sce"]) and strtolower($_REQUEST["sce"]) == "dump" ) {

  print "<pre>\n";
  print "// \$WX[] array size = $nscanned entries.\n";
  foreach ($WX as $key => $val) {
	  $t =  "\$WX[\'$key\'] = \'$val\';";
	  if(isset($WXComment[$key])) {$t .=  " $WXComment[$key]"; }
	  print "$t\n";
  }
  print "</pre>\n";
  exit;
}
if(file_exists("'.$defsFile.'")) { include_once("'.$defsFile.'"); }
?>';
}