#!/usr/bin/perl -w
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 ) ; 

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

if ( !exists($my_main{"SYM_IMAGE"}  ) )  { push ( @{$my_main{"SYM_IMAGE"}} , "SPHERE" ) ; }
if ( !exists($my_main{"SYM_IMAGE_RAD"}  ) )  { push ( @{$my_main{"SYM_IMAGE_RAD"}} , 30.0 ) ; }

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

my $ICOLOR = 105 ;
my $ICOLOR_STEP = 5 ;
if ( $my_main{"SG_NMOL"}[0] > 4 ) { $ICOLOR = 90 } ;
if ( $my_main{"SG_NMOL"}[0] > 8 ) { $ICOLOR_STEP = 2 } ;
#print " sym image rad", ${$my_main{"SYM_IMAGE_RAD"}}[0], "\n" ;
my $ATOM_SPHERE = ${$my_main{"SYM_IMAGE_RAD"}}[0] ;

my $MACRO_FILE = "symmetry.cmds" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;

my $keyword ; 
foreach $keyword ( sort keys (%commands) ) 
{
#    print " $keyword \n" ;
    if ( $keyword eq 'h' || substr($keyword, 0,4) eq "help"   ) { goto HELP }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'c' || substr($keyword, 0,5) eq "color"  ) { goto COLOR }
    if ( $keyword eq 's' || substr($keyword, 0,4) eq "step"   ) { goto STEP }
    if ( $keyword eq 'r' || substr($keyword, 0,6) eq "radius" ) { goto RADIUS }
    if ( $keyword eq 'g' || substr($keyword, 0,5) eq "gener"  ) { goto GENER }
    if ( substr($keyword, 0,4) eq "doit" ) { $doit = "TRUE" ; next ; }
    print "unknown option >$keyword<\n" ; $PROC_ERROR = "TRUE" ;

  HELP: 
    print 
	"  -h|--help)    prints this message with available options and current status \n",
	"  -o|--output)  set file name of the created macro [$MACRO_FILE]\n", 
	"  -c|--color)   starting color index for molecules [$ICOLOR]\n",
	"  -s|--step)    increment of next molecule color [$ICOLOR_STEP]\n",
	"  -r|--radius)  symmetry generation in specified sphere radius\n",
        "                around the last picked atom [$ATOM_SPHERE]\n",
	"  -g|--gener)   symmetry generation of symmetry mates\n",
	"                by [SPHERE/WHOLE/CLOSEST/EXACT]  current [@{$my_main{\"SYM_IMAGE\"}}]\n",
        "                SPHERE: within a sphere of the above specified radius\n",
        "                CLOSEST: whole segments positioned closes to the last clicked atom\n",
        "                WHOLE: whole segments positioned into the unit cell  \n",
        "                EXACT: segments are cut and shiftd to fit exactly the unit cell edges \n",
	"     --doit)    create the macro\n" ;
    exit ;
  OUTPUT:  $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
  COLOR:   $ICOLOR = shift ( @{$commands{$keyword}} ) ; next ;
  STEP:    $ICOLOR_STEP = shift ( @{$commands{$keyword}} ) ; next ;
  RADIUS:  $ATOM_SPHERE = shift ( @{$commands{$keyword}} ) ; next ;
  GENER:   @{$my_main{"SYM_IMAGE"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;

}
# print " proc error $PROC_ERROR \n" ;
# print " doit  $doit\n" ;
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" ) ; 
}
    open ( MACRO,  "> $MACRO_FILE" ) || die "FILE OPEN ERROR: >$MACRO_FILE< \n" ; 
print MACRO 
    "subroutine char SEGMENTS \n",
    " delete atom select segment name #* end \n" ;
#    print $my_main{"SYM_IMAGE"}[0], " sys_image< \n" ;
if ( $my_main{"SYM_IMAGE"}[0] eq "SPHERE" )
{ print MACRO " set vari ATOM_SPHERE real = $ATOM_SPHERE \n\n" ; }

if ( $my_main{"SG_NMOL"} == 1 )
{
print MACRO " key old select all end\n" ;

if ( $my_main{"SYM_IMAGE"}[0] eq "SPHERE" )
{ print MACRO
    " symm select segment name SEGMENTS end around center atom \$1  \\\n",
      "   dist ATOM_SPHERE neigh 2 \n" ;
} 
elsif ( $my_main{"SYM_IMAGE"}[0] eq "WHOLE" )
{ print MACRO
      "symm select segment name SEGMENTS end cell ( whole ) \n";
} 
elsif ( $my_main{"SYM_IMAGE"}[0] eq "CLOSEST" )
{ print MACRO
      "symm sele segm name SEGMENTS end cell ( close sele atom numb $1 end ) \n";
}
elsif ( $my_main{"SYM_IMAGE"}[0] eq "EXACT" )
{ print MACRO
      "symm sele segm name SEGMENTS end cell ( cut ) \n";
}
 print MACRO
    " key SYMM_M select .not old end \n",
    " image select SYMM_M end col $ICOLOR bond \n",
    " image select SYMM_M .a .not by bond all end col $ICOLOR atom cros\n\n";
}
else
{
    my $segm ; my $swat ;
    foreach $segm ( @{$my_main{"SG_LIST"}} )
    {
	if ( $segm eq "WAT" ) { $swat = "WAT*" ; } else { $swat = $segm ; }
	print MACRO
	    " key old select all end \n" ;
if ( $my_main{"SYM_IMAGE"}[0] eq "SPHERE" )
{ print MACRO
     " symm select segment name $swat .a segm name SEGMENTS end around \\\n", 
      "  center atom \$1 dist ATOM_SPHERE neigh 2 \n";
} 
elsif ( $my_main{"SYM_IMAGE"}[0] eq "WHOLE" )
{ print MACRO
      "symm select segment name $swat .a segm name SEGMENTS end cell whole\n";
} 
elsif ( $my_main{"SYM_IMAGE"}[0] eq "CLOSEST" )
{ print MACRO
      "symm sele segm name $swat .a segm name SEGMENTS end \\\n", 
      "      cell ( close sele atom numb \$1 end ) \n";
}
elsif ( $my_main{"SYM_IMAGE"}[0] eq "EXACT" )
{ print MACRO
      "symm sele segm name $swat .a segm name SEGMENTS end \\\n", 
      "      cell ( cut ) \n";
}

	print MACRO
	    " key SYMM_M sele .not old end \n",
	    " image select SYMM_M end col $ICOLOR bond \n",
	    " image select SYMM_M .a .not by bond all end col $ICOLOR atom cros \n\n" ;
	$ICOLOR = $ICOLOR + $ICOLOR_STEP ;
    }
}
print MACRO " key old select all end \n" ;
if ( $my_main{"SYM_IMAGE"}[0] eq "SPHERE" )
{ print MACRO 
      " symm select segm name WAT end around center atom \$1 dist ATOM_SPHERE neigh 2 \n";
}
print MACRO 
" key SYMM_WAT select .not old end \n",
" image select SYMM_WAT .and. atom name O* end col 125 atom cros \n\n",
"return \n";

close(MACRO) ;

exit ;

