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


   $from=[];
   $to=[];

   $flist="";
   foreach (glob("COM_.IOVBK/*.xml") as $filename) {
     if (!file_exists(str_replace(".xml", ".lmx", $filename))) {
      file_put_contents(str_replace(".xml", ".lmx", $filename), file_get_contents($filename));
     }
     $flist.=$filename."\n";
   }
   
   $lbits=explode("\n", $lookup);
   
   for ($i=0; $i<sizeof($lbits); $i++) {
    $recbits=explode(",", $lbits[$i]);
    $from[sizeof($from)]=$recbits[0];
    $to[sizeof($to)]=$recbits[1];
   }
   
   
   //echo $flist;
   foreach (glob("COM_.IOVBK/*.xml") as $filename) {
     $contents=file_get_contents(str_replace(".xml", ".lmx", $filename));
     $newc=$contents;
     for ($i=0; $i<sizeof($from); $i++) {
       $newc=str_replace("olink>" . $from[$i],"olink>" . str_replace(".","#@!@#.",$to[$i]),$newc);
       $newc=str_replace('olink targetptr="start">' . $from[$i],'olink targetptr="start">' . str_replace(".","#@!@#.",$to[$i]),$newc);
       $newc=str_replace('olink targetptr="end">' . $from[$i],'olink targetptr="end">' . str_replace(".","#@!@#.",$to[$i]),$newc);
       $newc=str_replace('label="[' . str_replace("[","",str_replace(".","",$from[$i])) . '.', 'label="[' . str_replace("[","",str_replace(".","",$to[$i])) . '#@!@#.', $newc);
       $newc=str_replace('label="' . str_replace("[","",str_replace(".","",$from[$i])) . '"', 'label="' . str_replace("[","",str_replace(".","",$to[$i])) . '#@!@#"', $newc);
       $newc=str_replace("COMM.IOVBK~CH." . str_replace("[","",str_replace(".","",$from[$i])) . '"', "COMM.IOVBK~CH." . str_replace("[","",str_replace(".","",$to[$i])) . '#@!@#"', $newc);
     }
     $newc=str_replace("#@!@#", "", $newc);
     file_put_contents($filename, $newc);
   }
   
   echo sizeof($from) . " " . sizeof($to);
?>
