<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <title>Generator</title>
  </head>
  <body>
  <?php

  $outFile 
$_POST['outFile'] or die('No output prefix provided.');
  
$title $_POST['title'] or die('No title provided.');
  
$splitOn strval(intval($_POST['son']));
  
$author $_POST['author'];
  
$date $_POST['date'];

  
// Get a unique directory
  
while( file_exists("$outFile/") )
  {
    
$outFile .= "b";
  }
  
mkdir("$outFile");
  
  
// Get the cover if it exists
  // TODO: Type checking and conversion
  
if( !empty($_FILES['cover']['name']) )
  {
    
$target_path "$outFile/cover.jpg";
    if(
move_uploaded_file($_FILES['cover']['tmp_name'], $target_path))
    {
      echo 
"The cover has been uploaded.<br/>";
    }
    else
    {
      echo 
"There was an error uploading the cover.<br/>";
    }
  }  
  
  
// Book Text
  
if ( !empty($_FILES['uploadedfile']['name']) )
  {
    
$inFile "$outFile/fulltext.txt";
    if(
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $inFile))
    {
      echo 
"The book has been uploaded.<br/>";
    }
    else
    {
      die(
"There was an error uploading the book, please try again.");
    }
  }
  else
  {
    
$inFile $_POST['inFile'] or die("No Input Provided.");
  }
  
  
$outTag 1;
  
  
$inHandle fopen($inFile"r");
  
$contents fread($inHandlefilesize($inFile));
  
fclose($inHandle);
  echo 
"Running splitter on book.<br/>";
  
strip_tags($contents);
  
$len strlen($contents);
  echo 
"Length: $len<br>";
  
$splits = ($len/$splitOn);
  echo 
"Split on $splitOn yields $splits Splits<br/>";
  echo 
"<br/>Starting Build<br/>";
  print 
"- Making INF File..";
  
$outHandle fopen("$outFile/$outFile-inf.txt""w+");
  
$infString $title."\n".($splits+1)."\n".$author."\n".$date;
  
fwrite($outHandle,$infString);
  
fclose($outHandle);
  print 
".Done<br/>";
  
  while(
$outTag $splits+1)
  {
    echo 
" -Making $outFile$outTag: ";
    
$outHandle fopen("$outFile/$outFile-$outTag.txt""w+");
    
fwrite($outHandle,(preg_replace("/\n+/","<br>",(substr($contents,(($outTag-1)*$splitOn),$splitOn)))));
    echo 
"Done<br>";
    
$outTag++;
  }
  echo 
"<br><br><b>Run Finished<br><a href=\"viewer.php?book=$outFile&page=0\">Go To Book</a>";
  
?>
  </body>
</html>