<?php
 // tr_mapping.php
 // RJM Programming
 // March, 2017
 // Global substitution in two parts:
 //    1) From filespec create CSV mapping (tr_mapping.csv) for second part via section tags containing role="paragraph" and old_label="[OldLabel]" and label="[NewLabel]" all before section tag end
 //    2) From input index file and CSV above create Korn Shell (ksh) file that runs ... ksh -x tr_mapping.ksh ... to update for new index file (but backup yourself first)

 $results="";
 $csvout="";
 $kshout="#!/bin/ksh\n";
 $delim="";
 
 $filespec="COMM.PINSW*.xml";
 if (isset($_GET['filespec'])) $filespec=str_replace("+"," ",urldecode($_GET['filespec']));
 $indexfilecalled="COMM.PIN~INDEX.xml";
 if (isset($_GET['index'])) $indexfilecalled=str_replace("+"," ",urldecode($_GET['index']));
 $startfind="<section ";
 if (isset($_GET['startfind'])) $startfind=str_replace("+"," ",urldecode($_GET['startfind']));
 $endfind=">";
 if (isset($_GET['endfind'])) $endfind=str_replace("+"," ",urldecode($_GET['endfind']));
 $newlabelfindstart=' label="';
 if (isset($_GET['newlabelfindstart'])) $newlabelfindstart=str_replace("+"," ",urldecode($_GET['newlabelfindstart']));
 $newlabelfindend='"';
 if (isset($_GET['newlabelfindend'])) $newlabelfindend=str_replace("+"," ",urldecode($_GET['newlabelfindend']));
 $oldlabelfindstart=' old_label="';
 if (isset($_GET['oldlabelfindstart'])) $oldlabelfindstart=str_replace("+"," ",urldecode($_GET['oldlabelfindstart']));
 $oldlabelfindend='"';
 if (isset($_GET['oldlabelfindend'])) $oldlabelfindend=str_replace("+"," ",urldecode($_GET['oldlabelfindend']));
 $findthis=' role="paragraph"';
 if (isset($_GET['findthis'])) $findthis=str_replace("+"," ",urldecode($_GET['findthis']));
 $findthistwo='';
 if (isset($_GET['findthistwo'])) $findthistwo=str_replace("+"," ",urldecode($_GET['findthistwo']));
 $findthisthree='';
 if (isset($_GET['findthisthree'])) $findthisthree=str_replace("+"," ",urldecode($_GET['findthisthree']));
 $csvname="tr_mapping.csv"; 
 if (isset($_GET['csvname'])) $csvname=str_replace("+"," ",urldecode($_GET['csvname']));
 $kshname="tr_mapping.ksh";
 if (isset($_GET['kshname'])) $kshname=str_replace("+"," ",urldecode($_GET['kshname']));
 $doksh=""; // "ksh -x tr_mapping.ksh";
 if (isset($_GET['doksh'])) $doksh=str_replace("+"," ",urldecode($_GET['doksh']));
 
 $uext=".XML";
 $lext=".xml";
 $ualt=".XXX";
 $lalt=".xxx";
 $extis=explode(".", $indexfilecalled);
 if (sizeof($extis) > 1) {
   $uext=strtoupper("." . $extis[-1 + sizeof($extis)]);
   $lext=strtolower("." . $extis[-1 + sizeof($extis)]);
   if ($lext == ".xxx") {
     $lalt=".xzz";
     $ualt=".XZZ";
   } else {
     $lalt=substr($lext,0,2) . "xx";
     $ualt=substr($uext,0,2) . "XX";
   }
 }

 
 foreach (glob($filespec) as $filename) {
   $cont=@file_get_contents($filename);
   $sections=explode($startfind, $cont);
   for ($i=1; $i<sizeof($sections); $i++) {
     $thisbits=explode($endfind, $sections[$i]);
     $thebit="  " . $thisbits[0];
     if (strpos($thebit, $oldlabelfindstart) !== false && strpos($thebit, $newlabelfindstart) !== false && ($findthis == '' || strpos($thebit, $findthis) !== false) && ($findthistwo == '' || strpos($thebit, $findthistwo) !== false) && ($findthisthree == '' || strpos($thebit, $findthisthree) === false)) {
      if (str_replace("]","\\]",explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0]) != "") {
        $csvout.=$delim . "," . explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0] . ",," . explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0];
        $kshout.=$delim . "cat " . $indexfilecalled . " | sed '/\\" . str_replace("]","\\]",explode($oldlabelfindend,explode($oldlabelfindstart, $thebit)[1])[0]) . "/s//\\" . str_replace("]","\\]",explode($newlabelfindend,explode($newlabelfindstart, $thebit)[1])[0]) . "/g' > " . str_replace($uext,$ualt,str_replace($lext,$lalt,$indexfilecalled)) . " ; rm -f " . $indexfilecalled . " ; cat " . str_replace($uext,$ualt,str_replace($lext,$lalt,$indexfilecalled)) . " > " . $indexfilecalled . " ; ";
		$delim="\n";
	  }
     }
   }
 }
 
 if ($csvout != "") {
  file_put_contents($csvname, $csvout); 
  file_put_contents($kshname, $kshout);
 
  if ($doksh != "") {
    if (!file_exists($indexfilecalled . "_original_backup")) copy($indexfilecalled, $indexfilecalled . "_original_backup");
    exec($doksh);
    $results="<table><tbody><tr><td>" . basename($indexfilecalled) . " below ...<br><textarea rows=50 cols=60 id='new'>" . file_get_contents($indexfilecalled) . "</textarea></td><td> ... New Versus Old ...</td><td>" . basename($indexfilecalled . "_original_backup") . " below ...<br><textarea id='old' rows=50 cols=60 >" . file_get_contents($indexfilecalled . "_original_backup") . "</textarea></td></tr></tbody></table>";
  }
 }
 ?>
 <!doctype html>
 <html>
 <body onload=" document.getElementById('myform').action=document.URL.split('#')[0].split('?')[0]; " style="background-color:lightgreen;">
 <h1>Global Substitution Helper</h1>
 <h3>RJM Programming</h3>
 <h3>March, 2017</h3>
 <br>
 <div id='results'><?php echo $results; ?></div>
 <form id="myform" action="#" method="GET">
 File name for input (and output) index file (to change): <input type="text" name="index" value="COMM.PIN~INDEX.xml"></input><br>
 File specification for input data: <input type="text" name="filespec" value="COMM.PINSW*.xml"></input><br>
 Start delimitation of tags of interest: <input type="text" name="startfind" value="<section "></input><br>
 End delimitation of tags of interest: <input type="text" name="endfind" value=">"></input><br>
 New label regarding global substitutions start delimitation: <input type="text" name="newlabelfindstart" value=' label="'></input><br>
 New label regarding global substitutions end delimitation: <input type="text" name="newlabelfindend" value='"'></input><br>
 Old label regarding global substitutions start delimitation: <input type="text" name="oldlabelfindstart" value=' old_label="'></input><br>
 Old label regarding global substitutions end delimitation: <input type="text" name="oldlabelfindend" value='"'></input><br>
 Find this within tag 1/3: <input type="text" name="findthis" value=' role="paragraph"'></input><br>
 Find this within tag 2/3: <input type="text" name="findthistwo" value=''></input><br>
 Do not find this within tag: <input type="text" name="findthisthree" value=''></input><br>
 Output (and intermediate) CSV file name: <input type="text" name="csvname" value="tr_mapping.csv"></input><br>
 Output (and intermediate) Korn Shell script file name: <input type="text" name="kshname" value="tr_mapping.ksh"></input><br>
 Optional command line to finalize job (eg. ksh -x tr_mapping.ksh): <input type="text" name="doksh" value=''></input><br>
 <input type="submit" value="Run this Job"></input>
 </form>
 </body>
 </html>
 
