<?php
// renumber_more.php
   
   $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;


   if (isset($argv[0])) {  // PHP command line specific interactive input code below ...
     $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;
 }
 }  // ... end of PHP command line specific interactive input code
 
   //echo $ourlookup;

   $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) . "\n";
?>
