#!/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 Utils;
use Crystal::WIEN2k;

	my $DataDir = ($ARGV[0] eq '')? "." : $ARGV[0];
	$DataDir = Utils::ReduceDirectory($DataDir);
	my ($drive, $directory, $filename, $ext, $lastdir, $filebody) = Deps::SplitFilePath($DataDir);
	my $StructFile = Deps::MakePath($DataDir, "$filename.struct", 0);
	if(!-e $StructFile) {
		my $fmask = Deps::MakePath($DataDir, "*.struct");
		my @files = glob($fmask);
		if($files[0] =~ /setrmt\.struct$/) {
			$StructFile = $files[1];
		}
		else {
			$StructFile = $files[0];
		}
		unless($StructFile) {
			print("Struct file is not found.\n");
			return 0;
		}
	}
	($drive, $directory, $filename, $ext, $lastdir, $filebody) = Deps::SplitFilePath($StructFile);
	my $Title   = $filebody;
	$Title =~ s/\s/_/g;

	my $KListPath = "$filebody.klist";
	my $div = 60;
	my ($nx, $ny, $nz) = (10, 10, 10);
	my ($emin, $emax) = (-2.0, 1.5);
	my $K   = 1;

	print "Struct: $StructFile\n";
	print "KList : $KListPath\n";

	my $WIEN2k = new WIEN2k;
	my $Crystal = $WIEN2k->ReadStructFile($StructFile, 0);
	unless($Crystal) {
		print("Error: Can not read [$StructFile].\n");
		return 0;
	}

	print "Lattice System: ", $Crystal->LatticeSystem(), "\n";
	print "Space Group   : ", $Crystal->SPGName(), "\n";

	open(OUT, ">$KListPath") or die "$!: Can not write to [$KListPath]\n";

	my $ra = $Crystal->GetMatrixConventionalToPrimitiveReciprocalCell();
if(1) {
	print "Primitive RLattice vector: ($ra->[0][0], $ra->[0][1], $ra->[0][2])\n";
	print "                           ($ra->[1][0], $ra->[1][1], $ra->[1][2])\n";
	print "                           ($ra->[2][0], $ra->[2][1], $ra->[2][2])\n";
}
	my $count = 1;
	my $BandName = "";
	print "kx range: (0 - $nx)\n";
	for(my $ix = 0 ; $ix <= $nx ; $ix++) {
		my $iy0 = $Crystal->GetYRangeForIteration($ix);
		print "ky range: $ix-($iy0 - $ny)\n";
		for(my $iy = $iy0 ; $iy <= $ny ; $iy++) {
			my $iz0 = $Crystal->GetZRangeForIteration($ix, $iy);
			print "kz range: $ix-$iy-($iz0 - $nz)\n";
			for(my $iz = $iz0 ; $iz <= $nz ; $iz++) {
				if($iz == 0) {
					$BandName = sprintf("B%02d%02d", $ix+1, $iy+1);
				}
				else {
					$BandName = "";
				}

				my $px = $K * $div * $ix / $nx;
				my $py = $K * $div * $iy / $ny;
				my $pz = $K * $div * $iz / $nz;
				my $x = $ra->[0][0] * $px + $ra->[0][1] * $py + $ra->[0][2] * $pz;
				my $y = $ra->[1][0] * $px + $ra->[1][1] * $py + $ra->[1][2] * $pz;
				my $z = $ra->[2][0] * $px + $ra->[2][1] * $py + $ra->[2][2] * $pz;
				if($count == 1) {
					printf(OUT "%5s       %3d  %3d  %3d  %3d%4.1f %4.1f %4.1f\n",
						$BandName, $x, $y, $z, $div, 1.0, $emin, $emax);
				}
				else {
					printf(OUT "%5s       %3d  %3d  %3d  %3d%4.1f\n",
						$BandName, $x, $y, $z, $div, 1.0);
				}
				$count++;
			}
		}
	}
	printf(OUT "END\n\n");
	close(OUT);
	exit;
