#!/usr/bin/perl

BEGIN {
#use lib 'd:/Programs/Perl/lib';
#use lib '/home/tkamiya/bin/lib';
my $BaseDir = $ENV{'TkPerlDir'};
#print "\nBaseDir: $BaseDir\n";
@INC = ("$BaseDir/lib", "$BaseDir/VNL", "d:/Programs/Perl/lib", @INC);
}

use strict;
#use warnings;
use File::Path;
use File::Basename;
use File::Find;

use Math::Matrix;
use Math::MatrixReal;

use Deps;
use Utils;
use JFile;

use MyApplication;
use Sci::Algorism;
use Sci::GeneralFileFormat;

use Crystal::CIF;
use Crystal::Crystal;
use Crystal::SpaceGroup;
use Crystal::AtomType;
use Crystal::VASP;
use Crystal::VESTA;
use Crystal::XCrySDen;

#===============================================
# デバッグ関係変数
#===============================================
#$PrintLevelが大きいほど、情報が詳しくなる
my $PrintLevel = 0;

#===============================================
# 文字コード関係変数
#===============================================
# sjis, euc, jis, noconv
my $PrintCharCode      = Deps::PrintCharCode();
my $OSCharCode         = Deps::OSCharCode();
my $FileSystemCharCode = Deps::FileSystemCharCode();

my $LF        = Deps::LF();
my $DirSep    = Deps::DirSep();
my $RegDirSep = Deps::RegDirSep();

#===============================================
# Applicationオブジェクト作成
#===============================================
my $App = new MyApplication;
exit if($App->Initialize() < 0);

#$App->SetLF("<br>\n");
#$App->SetPrintCharCode("sjis");
#$App->SetDebug($Debug);
$App->SetDeleteHTMLFlag(1);

#===============================================
# スクリプト大域変数
#===============================================
my $InitialDirectory = Deps::GetWorkingDirectory();
my %ParamHash;

#==========================================
# コマンドラインオプション読み込み
#==========================================
#$App->AddArgument("--Action", "--Action=[ModifyINCARforPBE0]", '');
exit 1 if($App->ReadArgs(1, "sjis", 0) != 1);
my $Args = $App->Args();
#my $form = new CGI;
#$Args->SetCGIForm($form);
#$Args->parseInput($WebCharCode);

my %ArgHash = $Args->GetArgHash();
foreach my $key (keys %ArgHash) {
##print "key: [$key]: $ArgHash{$key}\n";
#	if($key =~ /^Param:(.*?)$/i) {
#		$ParamHash{$1} = $ArgHash{$key};
##print "$1:  $ArgHash{$key}\n";
#	}
}
#$App->{pParamHash} = \%ParamHash;

#==========================================
# メイン関数スタート
#==========================================

#Utils::InitHTML("Research", $WebCharSet, "_self");

my $ret = &ModifyINCARforPBE0();

#Utils::EndHTML();

exit $ret;

#===============================================
# スクリプト終了
#===============================================

#==========================================
# &Subroutines
#==========================================
sub ModifyINCARforPBE0
{
	$App->print("\n\n<b>Modify INCAR file for PBE0:</b>\n");

	my $VASP = new VASP;

	my $INCARPath = $Args->GetGetArg(0);

	if(!$INCARPath) {
		$App->print("INCAR Path should be specified.\n");
		$App->print("   Usage: ModifyINCARforPBE0.pl INCAR_Path\n");
		return 0;
	}

$App->print("Input INCAR path: [$INCARPath]\n");

	$App->print("  Read from [$INCARPath].\n");
	my $in    = new JFile();
	my @lines = $in->ReadFileToLines($INCARPath);
	$in->Close();
	
	for(my $i = 0 ; $i < @lines ; $i++) {
		if($lines[$i] =~ /^(\s*)(#?)(\s*)(ALGO\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}53${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(LHFCALC\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}.TRUE.${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(AEXX\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}0.25${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(AGGAC\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}1.0${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(ALDAC\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}1.0${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(HFSCREEN\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}#${3}${4}0.2${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(ENCUTFOCK\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}#${3}${4}0${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(PRECFOCK\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}N${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(NKRED\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}#${3}${4}${5}${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(NKREDX\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}1${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(NKREDY\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}1${6}\n";
		}
		elsif($lines[$i] =~ /^(\s*)(#?)(\s*)(NKREDZ\s*=\s*)(\S+)(.*)$/i) {
			$lines[$i] = "${1}${3}${4}1${6}\n";
		}
	}

	$App->print("  Write to [$INCARPath].\n");
	my $out = new JFile($INCARPath, "w");
	if(!$out) {
		print "Error: Can not write to [$INCARPath].\n";
		return undef;
	}

	for(my $i = 0 ; $i < @lines ; $i++) {
		$out->printf($lines[$i]);
	}
	
	$out->Close();

	$App->print("\n<b>Finished: Modify INCAR file for PBE0</b>\n");

	return 1;
}

1;
