<?php
// renumber_more.php
// RJM Programming
// August, 2015
// Chapter changer for xml

  function cget($inv) {
    global $curlget;
    $retval="";
    if (isset($_GET[$inv])) {
     $retval=urldecode($_GET[$inv]);
    } else {
    $things=explode($inv . "=", $curlget);
    if (sizeof($things) >= 2) {
     if ($inv == "lookup") {
      $subthings=explode("you_will_neverfind-this", $things[1]);
      $retval=str_replace("|", "\n", $subthings[0]);
     } else {
      $subthings=explode("|", $things[1]);
      $retval=$subthings[0];
     }
    }
    }
    return $retval;
  }

   $cidea="curl " . "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] . "?curlget=";
   $curlget="";
   if (isset($_GET['curlget'])) $curlget=urldecode($_GET['curlget']);
   $letthrough=" ";   
   $filespec="COM_.IOVBK/*.xml";   
   $thisext="xml";
   $bckext="lmx";

   $ynftt="#@!@#";
   $prefix1="olink>";
   $prefix2='olink targetptr="start">';
   $prefix3='olink targetptr="end">';
   $prefix4='label="[';
   $prefix5='label="';
   $prefix6='COMM.IOVBK~CH.';

   $lookup="[30.,[29.
[32.,[30.
[33.,[31.
[34.,[32.
[35.,[33.
[36.,[34.
[37.,[35.
[38.,[36.
[39.,[37.
[40.,[38.
[41.,[39.
[42.,[40.
[43.,[41.
[46.,[42.
[47.,[43.
[48.,[44.
[49.,[45.
[50.,[46.
[50A.,[47.
[52.,[48.
[53.,[49.
[54.,[50.
[55.,[51.
[56.,[52.
[59.,[53.
[60.,[54.
[61.,[55.";

   $ourlookup=$lookup;
   
   $argv0="";
   if (isset($argv[0])) $argv0=$argv[0];
   
   
   if (isset($argv[0])) {  // PHP command line specific interactive input code below ...
     $letthrough= "*" . $_SERVER['SCRIPT_FILENAME'] . "!";
     $ourlookup="";

 print "
        Chapter Renumbering
        -------------------
        ";

 $filespec = "COM_.IOVBK/*.xml";
 print "Type file specification [COM_.IOVBK/*.xml].\n   Type nothing on a line by itself when you're done.\n";
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first) {
    if ($things[0] == "" || strlen($line) == 0) {
      $filespec="COM_.IOVBK/*.xml";
    } else {
      $filespec=$things[0];
    }
  }
  $first=false;
 }
 $bits=explode(".",$filespec);
 $thisext=$bits[sizeof($bits) - 1];
 $bckext=strrev($thisext);
 if ($bckext == $thisext) $bckext .= "_bck";
 $things=explode("\n", $line);
 print "Type comma separated chapter mapping data [[30.,[29.
[32.,[30.
[33.,[31.
[34.,[32.
[35.,[33.
[36.,[34.
[37.,[35.
[38.,[36.
[39.,[37.
[40.,[38.
[41.,[39.
[42.,[40.
[43.,[41.
[46.,[42.
[47.,[43.
[48.,[44.
[49.,[45.
[50.,[46.
[50A.,[47.
[52.,[48.
[53.,[49.
[54.,[50.
[55.,[51.
[56.,[52.
[59.,[53.
[60.,[54.
[61.,[55.]\n   Type nothing on a line by itself when you're done.\n";
 
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first || 1 == 1) {
    if ($first && ($things[0] == "" || strlen($line) == 0)) {
      $ourlookup=$lookup;
    } else {
      $ourlookup.=$things[0] . "\n";
    }
  }
  $first=false;
 }

 print "Type the string that causes no internal clashes [#@!@#] by being temporarily placed before '.'.\n   Type nothing on a line by itself when you're done.\n";
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first) {
    if ($things[0] == "" || strlen($line) == 0) {
      $ynftt="#@!@#";
    } else {
      $ynftt=$things[0];
    }
  }
  $first=false;
 }


 print "Type the prefix identifier 1 of 6 [olink>] (before what is in your lookup).\n  Type dot to not do this substitution idea.\n  Type nothing on a line by itself when you're done.\n";
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first) {
    if ($things[0] == "" || strlen($line) == 0) {
      $prefix1="olink>";
    } else if ($things[0] == '.') {
      $prefix1="";
    } else {
      $prefix1=trim($things[0]);
    }
  }
  $first=false;
 }

 print "Type the prefix identifier 2 of 6 [olink targetptr=" . '"' . "start" . '"' . ">] (before what is in your lookup).\n  Type dot to not do this substitution idea.\n  Type nothing on a line by itself when you're done.\n";
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first) {
    if ($things[0] == "" || strlen($line) == 0) {
      $prefix2='olink targetptr="start">';
    } else if ($things[0] == '.') {
      $prefix2="";
    } else {
      $prefix2=trim($things[0]);
    }
  }
  $first=false;
 }


 print "Type the prefix identifier 3 of 6 [olink targetptr=" . '"' . "end" . '"' . ">] (before what is in your lookup).\n  Type dot to not do this substitution idea.\n  Type nothing on a line by itself when you're done.\n";
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first) {
    if ($things[0] == "" || strlen($line) == 0) {
      $prefix3='olink targetptr="end">';
    } else if ($things[0] == '.') {
      $prefix3="";
    } else {
      $prefix3=trim($things[0]);
    }
  }
  $first=false;
 }


 print "Type the prefix identifier 4 of 6 [label=" . '"' . "[] (before just the chapter number and " . '.' . ").\n  Type dot to not do this substitution idea.\n  Type nothing on a line by itself when you're done.\n";
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first) {
    if ($things[0] == "" || strlen($line) == 0) {
      $prefix4='label="[';
    } else if ($things[0] == '.') {
      $prefix4="";
    } else {
      $prefix4=trim($things[0]);
    }
  }
  $first=false;
 }


 print "Type the prefix identifier 5 of 6 [label=" . '"' . "] (before just the chapter number and " . '"' . ").\n  Type dot to not do this substitution idea.\n  Type nothing on a line by itself when you're done.\n";
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first) {
    if ($things[0] == "" || strlen($line) == 0) {
      $prefix5='label="';
    } else if ($things[0] == '.') {
      $prefix5="";
    } else {
      $prefix5=trim($things[0]);
    }
  }
  $first=false;
 }


 print "Type the prefix identifier 6 of 6 [COMM.IOVBK~CH.] (before just the chapter number and " . '"' . ").\n  Type dot to not do this substitution idea.\n  Type nothing on a line by itself when you're done.\n";
 $line = " ";
 $first=true;
 while (strlen($line) != 0) {
  $line = trim(fgets(STDIN)); // reads one line from STDIN
  //fscanf(STDIN, "%s\n%s", $filespec, $other); // reads number from STDIN
  $things=explode("\n", $line);
  if ($first) {
    if ($things[0] == "" || strlen($line) == 0) {
      $prefix6="COMM.IOVBK~CH.";
    } else if ($things[0] == '.') {
      $prefix6="";
    } else {
      $prefix6=trim($things[0]);
    }
  }
  $first=false;
 }
 } else if (!isset($_GET['thisext']) && !isset($_GET['curlget'])) {  // ... end of PHP command line specific interactive input code ...
  $letthrough="";
  $cidea .= urlencode("|filespec=" . $filespec);
  $cidea .= urlencode("|ynftt=" . $ynftt);
  $cidea .= urlencode("|prefix1=" . $prefix1);
  $cidea .= urlencode("|prefix2=" . $prefix2);
  $cidea .= urlencode("|prefix3=" . $prefix3);
  $cidea .= urlencode("|prefix4=" . $prefix4);
  $cidea .= urlencode("|prefix5=" . $prefix5);
  $cidea .= urlencode("|prefix6=" . $prefix6);
  $cidea .= urlencode("|lookup=" . str_replace("\n","|",$lookup));
  echo "<html><head><title>" . $_SERVER['DOCUMENT_ROOT'] . "</title></head><body style='background-color:yellow;'><h1 align='center'>Chapter Renumbering</h1><br><div align='center'><form method='GET' action='./renumber_more.php'>
  Filespec (of data input): <input type='text' value='COM_.IOVBK/*.xml' name='filespec' id='filespec'></input><input type='hidden' name='thisext' value='xml'></input><input type='hidden' name='bckext' value='lmx'></input><br>
  Lookup to map chapters: <textarea rows=27 name='lookup' id='lookup'>[30.,[29.
[32.,[30.
[33.,[31.
[34.,[32.
[35.,[33.
[36.,[34.
[37.,[35.
[38.,[36.
[39.,[37.
[40.,[38.
[41.,[39.
[42.,[40.
[43.,[41.
[46.,[42.
[47.,[43.
[48.,[44.
[49.,[45.
[50.,[46.
[50A.,[47.
[52.,[48.
[53.,[49.
[54.,[50.
[55.,[51.
[56.,[52.
[59.,[53.
[60.,[54.
[61.,[55.</textarea><br>
  Padding text (make it something not in data input): <input type='text' value='#@!@#' name='ynftt' id='ynftt'></input><br>
  Prefix 1 of 6 (before what is in your lookup ... enter . for none): <input type='text' value='olink&gt;' name='prefix1' id='prefix1'></input><br>
  Prefix 2 of 6 (before what is in your lookup ... enter . for none): <input type='text' value='olink targetptr=\"start\"&gt;' name='prefix2' id='prefix2'></input><br>
  Prefix 3 of 6 (before what is in your lookup ... enter . for none): <input type='text' value='olink targetptr=\"end\"&gt;' name='prefix2' id='prefix2'></input><br>
  Prefix 4 of 6 (before just the chapter number and . ... enter . for none): <input type='text' value='label=\"[' name='prefix4' id='prefix4'></input><br>
  Prefix 5 of 6 (before just the chapter number and " . '"' . " ... enter . for none): <input type='text' value='label=\"' name='prefix5' id='prefix5'></input><br>
  Prefix 6 of 6 (before just the chapter number and " . '"' . " ... enter . for none): <input type='text' value='COMM.IOVBK~CH.' name='prefix6' id='prefix6'></input><br><br></input><input type='submit' value='Renumber Chapters'></input></form><p style='display:none;'>\n\nYou might be here using curl and we want you to copy/adjust/paste the example usage string below to help ...\n\n" . $cidea . "\n\n</p></body></html>";   
 } else if (isset($_GET['filespec']) || isset($_GET['curlget'])) {    // phase two run via form results
  $filespec=cget('filespec'); //urldecode($_GET['filespec']);
  $bits=explode(".",$filespec);
  $thisext=$bits[sizeof($bits) - 1];
  $bckext=strrev($thisext);
  if ($bckext == $thisext) $bckext .= "_bck";

   $ynftt=cget('ynftt'); //urldecode($_GET['ynftt']);
   $prefix1=cget('prefix1'); //urldecode($_GET['prefix1']);
   $prefix2=cget('prefix2'); //urldecode($_GET['prefix2']);
   $prefix3=cget('prefix3'); //urldecode($_GET['prefix3']);
   $prefix4=cget('prefix4'); //urldecode($_GET['prefix4']);
   $prefix5=cget('prefix5'); //urldecode($_GET['prefix5']);
   $prefix6=cget('prefix6'); //urldecode($_GET['prefix6']);
   if (prefix1 == '.') $prefix1='';
   if (prefix2 == '.') $prefix2='';
   if (prefix3 == '.') $prefix3='';
   if (prefix4 == '.') $prefix4='';
   if (prefix5 == '.') $prefix5='';
   if (prefix6 == '.') $prefix6='';

   $lookup=cget('lookup'); //urldecode($_GET['lookup']);
   $ourlookup=$lookup;
   $letthrough .= " <a href='./renumber_more.php' title='Another go'>Another go?</a>";
 }
 
 if (strlen($letthrough) > 0) {
   $from=[];
   $to=[];

   $flist="";
   $num = 0;
   foreach (glob($filespec) as $filename) {
     if (!file_exists(str_replace("." . $thisext, "." . $bckext, $filename))) {
      file_put_contents(str_replace("." . $thisext, "." . $bckext, $filename), file_get_contents($filename));
     }
     $flist.=$filename."\n";
     $num++;
   }
   
   $lbits=explode("\n", $ourlookup); //$lookup);
   
   for ($i=0; $i<sizeof($lbits); $i++) {
    if (strpos($lbits[$i], ",") !== false) {
     $recbits=explode(",", $lbits[$i]);
     $from[sizeof($from)]=$recbits[0];
     $to[sizeof($to)]=$recbits[1];
    }
   }
   
   
   //echo $flist;
   foreach (glob($filespec) as $filename) {
     $contents=file_get_contents(str_replace("." . $thisext, "." . $bckext, $filename));
     $newc=$contents;
     for ($i=0; $i<sizeof($from); $i++) {
       if ($prefix1 != "") $newc=str_replace($prefix1 . $from[$i],$prefix1 . str_replace(".",$ynftt . ".",$to[$i]),$newc);
       if ($prefix2 != "") $newc=str_replace($prefix2 . $from[$i],$prefix2 . str_replace(".",$ynftt . ".",$to[$i]),$newc);
       if ($prefix3 != "") $newc=str_replace($prefix3 . $from[$i],$prefix3 . str_replace(".",$ynftt . ".",$to[$i]),$newc);
       if ($prefix4 != "") $newc=str_replace($prefix4 . str_replace("[","",str_replace(".","",$from[$i])) . '.', $prefix4 . str_replace("[","",str_replace(".","",$to[$i])) . $ynftt . ".", $newc);
       if ($prefix5 != "") $newc=str_replace($prefix5 . str_replace("[","",str_replace(".","",$from[$i])) . '"', $prefix5 . str_replace("[","",str_replace(".","",$to[$i])) . $ynftt . ".", $newc);
       if ($prefix6 != "") $newc=str_replace($prefix6 . str_replace("[","",str_replace(".","",$from[$i])) . '"', $prefix6 . str_replace("[","",str_replace(".","",$to[$i])) . $ynftt . ".", $newc);
     }
     $newc=str_replace($ynftt, "", $newc);
     file_put_contents($filename, $newc);
   }
   
   echo "\n" . $num . " files amended.  Original files stored in " . str_replace("." . $thisext, "." . $bckext, $filespec) . $letthrough . "\n";
 }
?>
