#!/usr/bin/env php
<?php # (jEdit options) :folding=explicit:collapseFolds=1:
/*****************************************************************************
INPUTS / SWITCHES (via $_SERVER['argv']):
    inDir           a directory of files ending in ".pdb"
    outDir          a place to put the output files
    -q              quiet mode (no progress messages)

OUTPUTS / RESULTS:
    Creates multikins for all PDBs in inDir, which should already have H added.

*****************************************************************************/
// EVERY *top-level* page must start this way:
// 1. Define it's relationship to the root of the MolProbity installation.
// Pages in subdirectories of lib/ or public_html/ will need more "/.." 's.
    if(!defined('MP_BASE_DIR')) define('MP_BASE_DIR', realpath(dirname(__FILE__).'/..'));
// 2. Include core functionality - defines constants, etc.
    require_once(MP_BASE_DIR.'/lib/core.php');
    require_once(MP_BASE_DIR.'/lib/visualize.php');
// 3. Restore session data. If you don't want to access the session
// data for some reason, you must call mpInitEnvirons() instead.
    mpInitEnvirons();       // use std PATH, etc.
    //mpStartSession(true);   // create session dir
// 5. Set up reasonable values to emulate CLI behavior if we're CGI
    set_time_limit(0); // don't want to bail after 30 sec!
// 6. Unlimited memory for processing large files
    ini_set('memory_limit', -1);

#{{{ a_function_definition - sumary_statement_goes_here
############################################################################
/**
* Documentation for this function.
*/
//function someFunctionName() {}
#}}}########################################################################

# MAIN - the beginning of execution for this page
############################################################################
// Default options
$optVerbose = true;
$optGzipKin = true;
$optMultikin = array(
    'ribbons'   => false,
    'Bscale'    => true,
    'Qscale'    => true,
    'altconf'   => true,
    'rama'      => true,
    'geom'      => true,
    'rota'      => true,
    'geom'      => true,
    'cbdev'     => true,
    'pperp'     => true,
    'clashdots' => true,
    'hbdots'    => true,
    'vdwdots'   => true,
);

// First argument is the name of this script...
if(is_array($_SERVER['argv'])) foreach(array_slice($_SERVER['argv'], 1) as $arg)
{
    if($arg == '-q')            $optVerbose = false;
    elseif(!isset($inDir))      $inDir = $arg;
    elseif(!isset($outDir))     $outDir = $arg;
    else                        die("Too many or unrecognized arguments: '$arg'\n");
}

if(!isset($inDir))          die("No input directory specified.\n");
elseif(!is_dir($inDir))     die("Input directory '$inDir' does not exist.\n");
elseif(!isset($outDir))     die("No output directory specified.\n");
elseif(!is_dir($outDir))    die("Output directory '$outDir' does not exist.\n");

if($optVerbose)
{
    echo "INPUT   : ".realpath($inDir)."\n";
    echo "OUTPUT  : ".realpath($outDir)."\n";
}

foreach(listDir($inDir) as $inFile) if(endsWith($inFile, ".pdb"))
{
    #echo $inFile."\n";
    $outFile = basename($inFile, ".pdb") . ".kin";

    mpStartSession(true); // create a new session
    $inpath = $inDir.'/'.$inFile;
    $modelID = addModelOrEnsemble(
                 $inpath,
                 basename($inpath),
                 false,
                 true,
                 true,
                 false);

    #echo "modID: ".$modelID."\n";
    $model =& $_SESSION['models'][$modelID];
    $ensemble =& $_SESSION['ensembles'][$modelID];
    $reduce_blength = $_SESSION['reduce_blength'];
    #echo "model: ".isset($model)."\n";
    #echo "ensemble: \n";
    #print_r($ensemble);
    if (isset($model)) {
      $pdbfile = array($_SESSION['dataDir'].'/'.MP_DIR_MODELS."/$model[pdb]");
    }
    if (isset($ensemble)) {
      $pdbfile = array();
      foreach($ensemble['models'] as $splitmodel) {
        // adds each model pdb to the end of the $pdbfile array
        $pdbfile[] = $_SESSION['dataDir'].'/'.MP_DIR_MODELS."/$splitmodel.pdb";
      }
    }

    if($optVerbose)
    {
      echo "Making multi-criterion kinemage for $inFile using $reduce_blength x-H vdW radii ...\n";
    }
    
    makeMulticritKin2(
      $pdbfile,
      $outDir.'/'.$outFile,
      $optMultikin);
    if($optGzipKin) destructiveGZipFile($outDir.'/'.$outFile);

    mpDestroySession();
}

############################################################################
// Clean up and go home
//mpDestroySession(); // only call this if we created one
?>
