#!/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 ) ; 
my %new_main = () ;

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

#print ${$my_main{"MY_COMMANDS"}}[0], "\n" ;
if ( !exists($my_main{"MY_COMMANDS"} ) ) { push ( @{$my_main{"MY_COMMANDS"}}, "" ) };
if ( !exists($my_main{"DEPP_ENV"} ) ) 
     { push ( @{$my_main{"DEPP_ENV"}}, "NEW" ) };

my $MACRO_FILE = ".main" ;
my $PDB_FILE = "" ;
my $INPUT_FILE = "read.com" ;
my $PROC_ERROR = "FALSE" ;
my $doit = "FALSE" ;
my $DEPP_PAGES = $my_main{"DEPP_ENV"}[0] ;
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 'c' || substr($keyword, 0,7) eq "command" ) { goto MY_COMMANDS }
    if ( $keyword eq 'i' || substr($keyword, 0,5) eq "input" ) { goto INPUT }
    if ( $keyword eq 'o' || substr($keyword, 0,6) eq "output" ) { goto OUTPUT }
    if ( $keyword eq 'p' || substr($keyword, 0,3) eq "pdb" ) { goto PDB }
    if ( $keyword eq 'd' || substr($keyword, 0,4) eq "depp" ) { goto DEPP }
    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 current status
  -c|--command) define macro with MY COMMANDS [@{$my_main{"MY_COMMANDS"}}]
  -i|--input)   set file name of the data input macro [$INPUT_FILE]
  -o|--output)  set file name of the created macro [$MACRO_FILE] 
  -p|--pdb)     extract information from a PDB file [$PDB_FILE]
  -d|--depp)    depp pages [NEW/OLD] menu [$DEPP_PAGES]
     --doit)    create the macro
END_OF_TEXT
    exit ;
 OUTPUT:  $MACRO_FILE = shift ( @{$commands{$keyword}} ) ; next ;
 INPUT:  $INPUT_FILE = shift ( @{$commands{$keyword}} ) ; next ;
 MY_COMMANDS: @{$my_main{"MY_COMMANDS"}} = shift ( @{$commands{$keyword}} ) ; next ;
 PDB:  $PDB_FILE = shift ( @{$commands{$keyword}} ) ; next ;
 DEPP:  @{$my_main{"DEPP_ENV"}} = uc(shift ( @{$commands{$keyword}} )) ; next ;

}
# print " proc error $PROC_ERROR \n" ;
#print @{$commands{"MY_COMMANDS"}}, " 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 ; }

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

$DEPP_PAGES = $my_main{"DEPP_ENV"}[0] ;


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" ; 

if ( $DEPP_PAGES eq "NEW" ) 
{    print MACRO "<>cmds_new/load_depp_page\n" ;}
else
{    print MACRO "<>cmds/load_depp_page\n" ;}

print MACRO "image init \n" ;
print MACRO "< $INPUT_FILE\n" ;

 if (length( $my_main{"MY_COMMANDS"}[0]) != 0 ) 
{
    my $x_cmds = $my_main{"MY_COMMANDS"}[0] ;
print MACRO << "END_OF_TEXT" ;
< $x_cmds 
END_OF_TEXT
}

print MACRO << "END_OF_TEXT" ;
ima  forward
menu page 9 current
return
END_OF_TEXT

close(MACRO) ;

if ( $PDB_FILE eq "" ) { exit ; }
#######################################

my @pdb_records = () ;

print "reading PDB file \"$PDB_FILE\" \n" ;
open ( PDB,  "< $PDB_FILE" ) || die "FILE OPEN ERROR: >$PDB_FILE< \n" ;

while (<PDB> ) { push(@pdb_records, $_) ; } close ( PDB ) ;

my $line;
my $sub_line ;
my $group ;
my @words;
foreach $line ( @pdb_records )
{
    if ( substr($line, 0,5) eq "CRYST" ) 
    {
	chomp ($line) ;
#	print "[$line]\n" ;
	$sub_line = substr( $line, 6,49 ) ;
#	print " SUBLINE: {$sub_line} \n" ;
	@words = split (" ", $sub_line ) ;

	@{$my_main{"CELL_DATA"}} = @words ; 
	print "UNIT CELL: @words \n" ;
	$sub_line = substr( $line, 55,12 ) ;
#	print " SUBLINE: {$sub_line} \n" ;
	@words = split (" ", $sub_line ) ; 
# while ( $words[0] eq " " ) { print "x",  shift (@words) , "\n" ; };
	$group = join( '', shift(@words), shift(@words) ) ; 
	$group = join( '_', $group, @words ) ; $group = lc ($group) ;
	@{$my_main{"FILE_SYMM"}} = ( $group ) ;
	print "SPACE GROUP: $group \n" ;
	last ;
    }
}

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

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

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

if ( exists($my_main{"CELL_DATA"} ) ) 
{ 
my $FILE = $my_main{"FILE_CELL"}[0] ;
print "creating \"$FILE\"\n" ;
open ( TXT,  "> $FILE" ) || print "FILE OPEN ERROR: >$FILE< \n" ;
printf TXT "%9.3f %9.3f %9.3f %8.2f %8.2f %8.2f\n", 
    @{$my_main{"CELL_DATA"}} ;
close ( TXT ) ;
}
my $macro_file ;
$macro_file  = join ('', $ENV{"MAIN_CONF"}, "create_read.pl" ) ; 
              system ( "$macro_file --doit" ) ; 
$macro_file = join ('', $ENV{"MAIN_CONF"}, "create_re_image.pl" ) ; 
              system ( "$macro_file --style CHAINS --doit" ) ; 
              system ( "cp $PDB_FILE SAVE_FILE.PDB" ) ; 
exit ;
