#!/usr/bin/env perl
use strict;
use lib $ENV{ 'MAIN_CONF'}  ;
use main_config ;
my %commands = () ;

if ( @ARGV <= 0 ) { push ( @{$commands{'help'}}, ' ' ) ; } 
else { (%commands) = main_config::get_commands ( @ARGV ) ; }

my $conf_file ;
if ( -f "main_config.data" )
{    $conf_file = "main_config.data" ; }
else
{    $conf_file = join ('', $ENV{"MAIN_CONF"}, "main_config.data" ) ; }

my %my_main = main_config::read_main_config_file ( $conf_file ) ; 

#print " map kicked random: {", @{$my_main{"MAP_KICK_SERIES"}}, "}\n" ;
#####################################################################

if ( !exists($my_main{"IO_TASK"}  ) )       { push ( @{$my_main{"IO_TASK"}}  , "READ" ) ; }
if ( !exists($my_main{"IO_OBJECT"}  ) )       { push ( @{$my_main{"IO_OBJECT"}}  , "ATOMS" ) ; }
if ( !exists($my_main{"IO_FILE"}  ) )    { push ( @{$my_main{"IO_FILE"}}  , "model.pdb" ) ; }
if ( !exists($my_main{"IO_SELECTION"}  ) )  { push ( @{$my_main{"IO_SELECTION"}}  , "active" ) ; }
if ( !exists($my_main{"IO_KEY"}  ) )  { push ( @{$my_main{"IO_KEY"}}  , "MY_PDB" ) ; }
if ( !exists($my_main{"IO_SEGMENT"}  ) )  { push ( @{$my_main{"IO_SEGMENT"}}  , "AS_IS" ) ; }
if ( !exists($my_main{"IO_SEGMENT_INSERT"}  ) )  { push ( @{$my_main{"IO_SEGMENT_INSERT"}}  , "" ) ; }
if ( !exists($my_main{"IO_SEGMENT_CENTER"}  ) )  { push ( @{$my_main{"IO_SEGMENT_CENTER"}}  , "ON" ) ; }


######################################################
#print " map after setup kicked random: {", @{$my_main{"MAP_KICK_SERIES"}}, "}\n" ;

my $MACRO_FILE = "io_data.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;
my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
#    print "option >$keyword<\n" ; 
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help" ) { goto HELP }
    if ( $keyword eq 'f' || substr($keyword, 0,4) eq "file" ) { goto FILE }
    if ( $keyword eq 't' || substr($keyword, 0,4) eq "task" ) { goto TASK }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "object" ) { goto OBJECT }
    if ( $keyword eq 's' || substr($keyword, 0,9) eq "selection" ) { goto SELECTION }
    if ( $keyword eq 'k' || substr($keyword, 0,3) eq "key" ) { goto KEY }
    if ( $keyword eq 'i' || substr($keyword, 0,7) eq "segment" ) { goto SEGMENT }
    if ( $keyword eq 'c' || substr($keyword, 0,6) eq "center" ) { goto CENTER }
    if ( $keyword eq 'n' || substr($keyword, 0,6) eq "insert" ) { goto INSERT }

    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

  HELP:
    print << "END_OF_TEXT" ;
-h|--help)        prints this message with available options and their current status 
-t|--task)        task to do  [READ/WRITE] [@{$my_main{"IO_TASK"}}]
-f|--file)        file name ( double click to search ) [@{$my_main{"IO_FILE"}}]
-o|--object)      object for io [ATOMS/CELL/MAP/REFLECTION/SURFACE/SYMMETRY] [@{$my_main{"IO_OBJECT"}}]
-s|--selection)   selection [@{$my_main{"IO_SELECTION"}}]
-i|--segment)     segment ID of atoms [@{$my_main{"IO_SEGMENT"}}]
-b|--insert)      insert char before segment ID of atoms [@{$my_main{"IO_SEGMENT_INSERT"}}]
-c|--center)      center molecule in the middle of screen [ON/OFF] [@{$my_main{"IO_SEGMENT_CENTER"}}]
-k|--key)         selection key to address the newly read atom [@{$my_main{"IO_KEY"}}]
   --doit)        create the macro
END_OF_TEXT
    exit ;

 TASK:       @{$my_main{"IO_TASK"}} = shift ( @{$commands{$keyword}} ) ; next ;
 FILE:       @{$my_main{"IO_FILE"}} = shift ( @{$commands{$keyword}} ) ; next ;
 OBJECT:     @{$my_main{"IO_OBJECT"}} = shift ( @{$commands{$keyword}} ) ; next ;
 SELECTION:  @{$my_main{"IO_SELECTION"}} = join (" ", @{$commands{$keyword}} ) ; next ;
 KEY:        @{$my_main{"IO_KEY"}} = join (" ", @{$commands{$keyword}} ) ; next ;
 SEGMENT:    @{$my_main{"IO_SEGMENT"}} = join (" ", @{$commands{$keyword}} ) ; next ;
 INSERT:     @{$my_main{"IO_SEGMENT_INSERT"}} = join (" ", @{$commands{$keyword}} ) ; next ;
 CENTER:     @{$my_main{"IO_SEGMENT_CENTER"}} = join (" ", @{$commands{$keyword}} ) ; next ;
}

if ( $PROC_ERROR eq "TRUE" ) { exit ; }

#######################################

$conf_file = "main_config.data" ; 
main_config::write_main_config_file ( $conf_file, %my_main ) ; 

###  . ${MAIN_CONF}main_config.data.save.sh

if ( $doit eq "FALSE" )  { exit ; }

######################################

print "creating \"$MACRO_FILE\" \n" ;
if ( -f $MACRO_FILE ) 
{ 
    my $back = join ('', $MACRO_FILE, ".old" ) ; 
    system ( "mv  $MACRO_FILE $back" ) ; 
}


my @my_lines = () ;

my $IO_FILE ; 
my $MY_SELECTION ; 
my $MY_KEY ; 
my $MY_SEGMENT ; 
my $MY_INSERT ; 
my $MY_CENTER ; 


open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ;

$IO_FILE = ${$my_main{"IO_FILE"}}[0] ;
$MY_SELECTION = ${$my_main{"IO_SELECTION"}}[0] ; 
$MY_KEY = ${$my_main{"IO_KEY"}}[0] ; 
$MY_SEGMENT = ${$my_main{"IO_SEGMENT"}}[0] ; 
$MY_INSERT = ${$my_main{"IO_SEGMENT_INSERT"}}[0] ; 
$MY_CENTER = ${$my_main{"IO_SEGMENT_CENTER"}}[0] ; 

# tasks


if ( ${$my_main{"IO_OBJECT"}}[0] eq "SURFACE" )
{
    if ( uc(${$my_main{"IO_TASK"}}[0]) eq "WRITE" )
    {
print MACRO << "END_OF_TEXT" ;
write over file $IO_FILE \\
sele $MY_SELECTION end \\
point surface 3net main \\
first sele $MY_SELECTION end
END_OF_TEXT
    }
    else
    {
print MACRO << "END_OF_TEXT" ;
read file $IO_FILE \\
point main first sele $MY_SELECTION end

END_OF_TEXT
    }
}

if ( ${$my_main{"IO_OBJECT"}}[0] eq "ATOMS" )
{
    my $MY_FORM ;
    if ( lc($IO_FILE) =~ "cif" )
    { $MY_FORM = 'mmcif' ; }
    else
    { $MY_FORM = 'pdb' ; }

    if ( uc(${$my_main{"IO_TASK"}}[0]) eq "WRITE" )
    {
print MACRO << "END_OF_TEXT" ;
write over file $IO_FILE \\
sele $MY_SELECTION end \\
atom $MY_FORM
END_OF_TEXT
    }
    else
    {

if ( $MY_SEGMENT ne "AS_IS" )
{
    print MACRO << "END_OF_TEXT" ;
delete atom sele segm name $MY_SEGMENT end
END_OF_TEXT
}

print MACRO << "END_OF_TEXT" ;
key $MY_KEY sele all end
read file $IO_FILE atom $MY_FORM
key $MY_KEY sele .not $MY_KEY end
END_OF_TEXT

# print " my segment:?", $MY_SEGMENT, "?\n" ;
if ( $MY_SEGMENT ne "AS_IS" )
{
    print MACRO << "END_OF_TEXT" ;
rena segm $MY_SEGMENT sele $MY_KEY end
END_OF_TEXT
}
# print " my insert:?", $MY_INSERT, "?\n" ;
if ( $MY_INSERT ne "" && $MY_INSERT ne " "   )
{
    print MACRO << "END_OF_TEXT" ;
rena segm $MY_INSERT sele $MY_KEY end insert
END_OF_TEXT
}
    print MACRO << "END_OF_TEXT" ;
set var WORK_SEGM global char segm sele segm name WORK_SEGM .o $MY_KEY end
make bond from topo sele $MY_KEY end
dele bond sele bond range 4. 1000. .a $MY_KEY end

END_OF_TEXT

if ( $MY_CENTER eq "ON" )
{ print MACRO "translate atom sele $MY_KEY end center\n" ; }

    }
}

if ( ${$my_main{"IO_OBJECT"}}[0] eq "SYMMETRY" )
{
    if ( uc(${$my_main{"IO_TASK"}}[0]) eq "WRITE" )
    {
print MACRO << "END_OF_TEXT" ;
write over file $IO_FILE symm
END_OF_TEXT
    }
    else
    {
print MACRO << "END_OF_TEXT" ;
read file $IO_FILE symm
END_OF_TEXT
    }
}



close (MACRO ) ;
exit ;
