#!/bin/perl


use 5.004;


$version_number = '1.000';


$n_vn = length($version_number);
for ($k_vn = 1; $k_vn < $n_vn; $k_vn++) {
        $line .= '-';
        $space .= ' ';
}
print <<"END";


+${line}---------------------------------+
|${space}                                 |
|${space}                                 |
|        log2mkl version ${version_number}        |
|${space}                                 |
|${space}                                 |
+${line}---------------------------------+


END
do {
        $log_file_cannot_be_opened = 0;
        print <<'END';
     Please input the name(path) of GAMESS-DRC's log file below.

END
        print('> ');
        $log_file_name = <STDIN>;
        print <<'END';

END
        chomp($log_file_name);
        if (!(-e $log_file_name)) {
                print <<"END";
     The file "${log_file_name}" does not exist (or it is hidden by the permissons of the directories).


END
                $log_file_cannot_be_opened = 1;
        } elsif (-d $log_file_name) {
                print <<"END";
     "${log_file_name}" is a directory, not GAMESS-DRC's log file.


END
                $log_file_cannot_be_opened = 1;
        } elsif (-B $log_file_name) {
                print <<"END";
     The file "${log_file_name}" is binary file, not Gamess-DRC's log file. Gamess-DRC's log files must be text file.


END
                $log_file_cannot_be_opened = 1;
        } elsif (!((-r $log_file_name) || (-R $log_file_name))) {
                print <<"END";
     I can not read the file "${log_file_name}", because we are not permitted to read it by the operating system.


END
                $log_file_cannot_be_opened = 1;
        } else {
                $ret = open(LOGFILE , "<${log_file_name}");
                flock(LOGFILE , 2);
                if ($ret eq undef) {
                        print <<"END";
     I have encountered an unexpected error. I have been unable to open the file "${log_file_name}". I don't know the reason.


END
                        $log_file_cannot_be_opened = 1;
                }
        }
} while ($log_file_cannot_be_opened);
print <<"END";

 ${line}---------------------------------


END
do {
        $mkl_file_cannot_be_opened = 0;
        print <<'END';
     Please input the name(path) of the new mkl file below.

END
        print('> ');
        $mkl_file_name = <STDIN>;
        print <<'END';

END
        chomp($mkl_file_name);
        @mkl_file_name_array = split(/\// , $mkl_file_name);
        $n_mkl_file_name_array = $#mkl_file_name_array;
        $mkl_file_eigen_name = $mkl_file_name_array[$n_mkl_file_name_array];
        pop(@mkl_file_name_array);
        if ($n_mkl_file_name_array >= 1) {
                $mkl_file_directory_name = join('/' , @mkl_file_name_array);
                $mkl_file_directory_name .= '/';
        } else {
                $mkl_file_directory_name = './';
        }
        if (!(-e $mkl_file_directory_name)) {
                print <<"END";
     I can not write the new mkl file "${mkl_file_name}", because the directory "${mkl_file_directory_name}" does not exist (or it is hidden by the permissons of the directories).


END
                $mkl_file_cannot_be_opened = 1;
        } elsif (!((-w $mkl_file_directory_name) || (-W $mkl_file_directory_name))) {
                print <<"END";
     I can not write the new mkl file "${mkl_file_name}", because we are not permitted to write in the directory "${mkl_file_directory_name}" by the operating system.


END
                $mkl_file_cannot_be_opened = 1;
        } elsif (-e ${mkl_file_name}) {
                print <<"END";
     I don't write the new mkl file "${mkl_file_name}", because the file "${mkl_file_name}" has proved to exists already.


END
                $mkl_file_cannot_be_opened = 1;
        } elsif (!(${mkl_file_eigen_name} =~ m/\w+\.?\w*/)) {
                print <<"END";
     I don't write the new mkl file "${mkl_file_name}", because the file name "${mkl_file_eigen_name}" is dangerous for the UNIX system.


END
                $mkl_file_cannot_be_opened = 1;
        } else {
                $ret = open(MKLFILE , ">${mkl_file_name}");
                flock(MKLFILE , 2);
                if ($ret eq undef) {
                        print <<"END";
     I have encountered an unexpected error. I have been unable to open the file "${mkl_file_name}". I don't know the reason.


END
                        $mkl_file_cannot_be_opened = 1;
                }
        }
} while ($mkl_file_cannot_be_opened);
print <<"END";

 ${line}---------------------------------


END
do {
        $sampling_rate_is_not_available = 0;
        print <<'END';
     Please input the sampling rate[step(s)] from the GAMESS-DRC's log file to the new mkl file below.

END
        print('> ');
        $sampling_rate = <STDIN>;
        print <<'END';

END
        if (!($sampling_rate =~ m/^\s*\+?[\s0]*[\s1-9]+[\s0]*\.?[\s0]*$/)) {
                print <<"END";

     I can not adopt the string "${sampling_rate}", which you have input above. The sampling rate must be positive integer, but the string "${sampling_rate}" is not positive integer.


END
                $sampling_rate_is_not_available = 1;
        }
} while($sampling_rate_is_not_available);
$sampling_rate =~ s/\s//g;
$sampling_rate =~ s/\+//;
$sampling_rate =~ s/\..*//;
$sampling_rate =~ s/^0*//;
if ($sampling_rate == 1) {
        $s_or_none = '';
} else {
        $s_or_none = 's';
}
print <<"END";

 ${line}---------------------------------


END
print(MKLFILE qq/        This mkl file is written by log2mkl version ${version_number}.\r\n/);
print(MKLFILE "--------------------\r\n");
print(MKLFILE '$MKL');
print(MKLFILE "\r\n");
print(MKLFILE '$COORD');
print(MKLFILE "\r\n");
$state = 1;
$is_first = 1;
$counter = 0;
while (!eof(LOGFILE)) {
        $line_of_log_file = <LOGFILE>;
        if ($state == 1) {
                if ($line_of_log_file =~ m/cartesian\s*coordinate/i) {
                        $state = 2;
                }
        } elsif ($state == 2) {
                if ($line_of_log_file =~ m/\d|\./) {
                        $state = 3;
                        if ($is_first) {
                                $is_first = 0;
                        } elsif ($counter == 0) {
                                print(MKLFILE '$$');
                                print(MKLFILE "\r\n");
                        }
                }
        }
        if ($state == 3) {
                if (!($line_of_log_file =~ m/\d|\./)) {
                        $counter++;
                        if ($counter == $sampling_rate) {
                                $counter = 0;
                        }
                        if ($line_of_log_file =~ m/cartesian\s*coordinate/i) {
                                $state = 2;
                        } else {
                                $state = 1;
                        }
                } elsif ($counter == 0) {
                        @coordinates_table = split(/ +/ , $line_of_log_file);
                        if ($coordinates_table[0] == '') {
                                shift(@coordinates_table);
                        }
                        $coordinates_table[0] =~ s/\..*//;
                        $n_rows = $#coordinates_table;
                        for ($k_rows = 0; ($k_rows <= 3) && ($k_rows <= $n_rows); $k_rows++) {
                                $temp = $coordinates_table[$k_rows];
                                if ($k_rows != 0) {
                                        $temp *= 0.5;
                                }
                                print(MKLFILE "        ${temp}")
                        }
                        print(MKLFILE "\r\n");
                }
        }
}
print(MKLFILE '$END');
print(MKLFILE "\r\n\r\n\r\n");
chmod(0600 , "${mkl_file_name}");
print <<"END";
     The new mkl file "${mkl_file_eigen_name}" have been made from the GAMESS-DRC's log file "${log_file_name}" at the sampling rate of ${sampling_rate}[step${s_or_none}], put in the directory "${mkl_file_directory_name}", and the permission of the file have been set as "rwx------".


END
flock(LOGFILE , 8);
close(LOGFILE);
flock(MKLFILE , 8);
close(MKLFILE);
