#! /usr/bin/env perl
 
#---------------------------------------------------------------------------
#@COPYRIGHT :
#             Copyright 1998, Alex P. Zijdenbos
#             McConnell Brain Imaging Centre,
#             Montreal Neurological Institute, McGill University.
#             Permission to use, copy, modify, and distribute this
#             software and its documentation for any purpose and without
#             fee is hereby granted, provided that the above copyright
#             notice appear in all copies.  The author and McGill University
#             make no representations about the suitability of this
#             software for any purpose.  It is provided "as is" without
#             express or implied warranty.
#---------------------------------------------------------------------------- 
#$RCSfile: insect.in,v $
#$Revision: 1.1 $
#$Author: claude $
#$Date: 2006-09-14 19:37:53 $
#$State: Exp $
#---------------------------------------------------------------------------

# modified by Jason Lerch (Mar 2002) to be included in autoconfiscation

use MNI::Startup;
use MNI::Spawn;
#use MNI::DataDir;
use MNI::FileUtilities qw(test_file);
use MNI::PathUtilities qw(split_path);
use Getopt::Tabular;
use IO::File;

use strict;
use warnings "all";
use FindBin;

# User-modifiable globals
my $Inputfile;

# Other globals
my($Usage, $Help);

&Initialize;


my $Template = "$FindBin::Bin/../share/ICBM/" . 'icbm_template_1.00mm.mnc';
#my $Template = MNI::DataDir::dir('ICBM') . 'icbm_template_1.00mm.mnc';

my($Dir, $Base, $Ext) = split_path($Inputfile, 'last', [qw(gz z Z)]);

my $NUC = OutputFile($Base, '_nuc.mnc');
my $XFM = OutputFile($Base, '_nuc_total.xfm');
my $TAL = OutputFile($Base, '_nuc_tal.mnc');
my $CLA = OutputFile($Base, '_nuc_tal_cla.mnc');

Spawn(['nu_correct_norm', $Inputfile, $NUC]);

Spawn(['mritotal', $NUC, $XFM]);

Spawn(['mincresample', '-transformation', $XFM, '-like',  $Template, $NUC, $TAL]);

Spawn(['classify_clean', $TAL, $CLA]);

# ------------------------------ MNI Header ----------------------------------
#@NAME       : Initialize
#@INPUT      : 
#@OUTPUT     : 
#@RETURNS    : 
#@DESCRIPTION: 
#@METHOD     : 
#@GLOBALS    : 
#@CALLS      : 
#@CREATED    : 98/11/29, Alex Zijdenbos
#@MODIFIED   : 
#-----------------------------------------------------------------------------
sub Initialize
{
   $, = ' ';     # set output field separator

   # First, announce ourselves to stdout (for ease in later dissection
   # of log files) -- unless STDOUT is a tty.
   self_announce if $Verbose;

   # Set defaults for the global variables.
   $Verbose      = 1;
   $Execute      = 1;
   $Clobber      = 0;
   $KeepTmp      = 0;

   &CreateInfoText;

   my(@argTbl) = (@DefaultArgs);
   
   my(@leftOverArgs);

   GetOptions (\@argTbl, \@ARGV, \@leftOverArgs) || die "\n";
   if (@leftOverArgs != 1) {
       warn $Usage;
       die "Incorrect number of arguments\n";
   }

   my $tempfile = shift @leftOverArgs;
   ($Inputfile = test_file('-e', $tempfile)) || die "Couldn't find input file $tempfile\n";

   RegisterPrograms([qw(nu_correct_norm mritotal mincresample classify_clean)]) || die;

   AddDefaultArgs('nu_correct_norm', ($Verbose) ? ['-verbose'] : ['-quiet']);
   AddDefaultArgs('mritotal',        ($Verbose) ? ['-verbose'] : ['-quiet']);
   AddDefaultArgs('mincresample',    ($Verbose) ? ['-verbose'] : ['-quiet']);
   AddDefaultArgs('classify_clean',  ($Verbose) ? ['-verbose'] : ['-quiet']);

   AddDefaultArgs('nu_correct_norm', ['-clobber']);
   AddDefaultArgs('mritotal',        ['-clobber']);
   AddDefaultArgs('mincresample',    ['-clobber']);
   AddDefaultArgs('classify_clean',  ['-clobber']);

   # Be strict about having programs registered
   MNI::Spawn::SetOptions (strict => 2);
}

# ------------------------------ MNI Header ----------------------------------
#@NAME       : CreateInfoText
#@INPUT      : 
#@OUTPUT     : 
#@RETURNS    : 
#@DESCRIPTION: 
#@METHOD     : 
#@GLOBALS    : 
#@CALLS      : 
#@CREATED    : 98/11/29, Alex Zijdenbos
#@MODIFIED   : 
#-----------------------------------------------------------------------------
sub CreateInfoText
{
    $Usage = <<USAGE;
Usage: $ProgramName [options] <input.mnc>
       $ProgramName -help for details
USAGE

    $Help = <<HELP;

$ProgramName runs the INSECT classification, consisting of:

    1. nu_correct + global intensity normalization
    2. mritotal
    3. mincresample
    4. classify_clean

$ProgramName creates four output files in the current directory, 
corresponding to these four steps.
 
HELP

   Getopt::Tabular::SetHelp ($Help, $Usage);
}

sub OutputFile {
    my ($base, $ext) = @_;

    my $file = "$base$ext";

    die "Output file $file exists; use -clobber to overwrite\n"
	if (-e $file && ! $Clobber);

    $file;
}
