<?php
$from = "";
$to = "";
$outf = "";
$argv1 = "";
$argv2 = "";
$argv3 = "";
$argv4 = "";
$argv5 = "";

if (!isset($argv[1])) {
secondchoice();
} else {
$argv1 = $argv[1];
if (isset($argv[2])) $argv2 = $argv[2];
if (isset($argv[3])) $argv3 = $argv[3];
if (isset($argv[4])) $argv4 = $argv[4];
if (isset($argv[5])) $argv5 = $argv[5];
}
if (strlen($argv1)) {
 if (file_exists($argv1)) {
  $ourfp = fopen($argv1, "r");
  while (($buffer = fgets($ourfp, 4096)) !== false) {
   if (strlen($argv2) && strlen($argv3)) {
    if (strpos($argv2, "-n") !== false && strlen($argv2) == 2) {
      $from = str_replace("@!@'", "", str_replace('@!@"', "", str_replace("'@!@", "", str_replace('"@!@', "", str_replace("@!@", "", ("@!@".$argv3."@!@"))))));
      $to = str_replace("@!@'", "", str_replace('@!@"', "", str_replace("'@!@", "", str_replace('"@!@', "", str_replace("@!@", "", ("@!@".$argv4."@!@"))))));
      //echo "from=".$from." and to=".$to;
    } else if (strpos($argv2, "-w") !== false && strlen($argv2) == 2) {
      $outf = "y";
      $outfp = fopen($argv1, "w");
      $from = str_replace("@!@'", "", str_replace('@!@"', "", str_replace("'@!@", "", str_replace('"@!@', "", str_replace("@!@", "", ("@!@".$argv4."@!@"))))));
      $to = str_replace("@!@'", "", str_replace('@!@"', "", str_replace("'@!@", "", str_replace('"@!@', "", str_replace("@!@", "", ("@!@".$argv5."@!@"))))));
      //echo "from=".$from." and to=".$to;
    } else if (strpos($argv2, "-a") !== false && strlen($argv2) == 2) {
      $outf = "y";
      $outfp = fopen($argv1, "a");
      $from = str_replace("@!@'", "", str_replace('@!@"', "", str_replace("'@!@", "", str_replace('"@!@', "", str_replace("@!@", "", ("@!@".$argv4."@!@"))))));
      $to = str_replace("@!@'", "", str_replace('@!@"', "", str_replace("'@!@", "", str_replace('"@!@', "", str_replace("@!@", "", ("@!@".$argv5."@!@"))))));
      //echo "from=".$from." and to=".$to;
    } else if (strlen($from) == 0) {
      $from = str_replace("@!@'", "", str_replace('@!@"', "", str_replace("'@!@", "", str_replace('"@!@', "", str_replace("@!@", "", ("@!@".$argv2."@!@"))))));
      $to = str_replace("@!@'", "", str_replace('@!@"', "", str_replace("'@!@", "", str_replace('"@!@', "", str_replace("@!@", "", ("@!@".$argv3."@!@"))))));
      //echo "from=".$from." and to=".$to;
    }
    if (strlen($outf) > 0) {
      fprintf($outfp, "%s", str_replace($from, $to, $buffer));
    } else {
      echo str_replace($from, $to, $buffer);
    }
   } else {
    echo $buffer;
   }
  }
  if (strlen($outf) > 0) fclose($outfp);
  fclose($ourfp);
 } else {
   $arg = str_replace("*", "", str_replace("?", "", $argv1));
   if (strlen($arg) == strlen($argv1)) echo $argv1 . " does not exist.";
 }
}

function secondchoice() {
 global $argv1, $argv2, $argv3, $argv4, $argv5;
 $here1 = "";
 $rec = "";
 if (file_exists("phpcmdparams-are.here1")) {
  $vourfp = fopen("phpcmdparams-are.here1", "r");
  if (strlen($argv1) == 0) {
  $argv1 = fgets($vourfp, 4096);
  while (($rec = fgets($vourfp, 4096)) !== false) {
  $here1 .= $rec;
  }
  }
  fclose($vourfp);
  $argv1x = str_replace("\n", "", str_replace("\r", "", str_replace("\r\n", "", $argv1)));
  $argv1 = $argv1x;
  if (strlen($here1) == 0) {
  unlink("phpcmdparams-are.here1");
  } else {
  $wvourfp = fopen("phpcmdparams-are.here1", "w");
  fprintf($wvourfp, "%s", $here1);
  fclose($wvourfp);
  }
 }
 if (file_exists("phpcmdparams-are.here2")) {
  $vourfp = fopen("phpcmdparams-are.here2", "r");
  $argv2 = fgets($vourfp, 4096);
  fclose($vourfp);
  $argv1x = str_replace("\n", "", str_replace("\r", "", str_replace("\r\n", "", $argv2)));
  $argv2 = $argv1x;
 }
 if (file_exists("phpcmdparams-are.here3")) {
  $vourfp = fopen("phpcmdparams-are.here3", "r");
  $argv3 = fgets($vourfp, 4096);
  fclose($vourfp);
  $argv1x = str_replace("\n", "", str_replace("\r", "", str_replace("\r\n", "", $argv3)));
  $argv3 = $argv1x;
 }
 if (file_exists("phpcmdparams-are.here4")) {
  $vourfp = fopen("phpcmdparams-are.here4", "r");
  $argv4 = fgets($vourfp, 4096);
  fclose($vourfp);
  $argv1x = str_replace("\n", "", str_replace("\r", "", str_replace("\r\n", "", $argv4)));
  $argv4 = $argv1x;
 }
 if (file_exists("phpcmdparams-are.here5")) {
  $vourfp = fopen("phpcmdparams-are.here5", "r");
  $argv5 = fgets($vourfp, 4096);
  fclose($vourfp);
  $argv1x = str_replace("\n", "", str_replace("\r", "", str_replace("\r\n", "", $argv5)));
  $argv5 = $argv1x;
 }
}
?>