 $pt = 'H                                                                                                He
        Li Be                                                                           B  C   N  O   F  Ne
        Na Mg                                                                          Al Si   P  S  Cl  Ar
        K  Ca                                           Sc Ti  V Cr Mn Fe Co Ni Cu Zn  Ga Ge  As Se  Br  Kr
        Rb Sr                                            Y Zr Nb Mo Tc Ru Rh Pd Ag Cd  In Sn  Sb Te   I  Xe
        Cs Ba La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu Hf Ta W  Re Os Ir Pt Au Hg  Tl Pb  Bi Po  At  Rn
        Fr Ra Ac Th Pa U  Np Pu Am Cm Bk Cf Es Fm Md No Lr Rf Ha Sg Bh Hs Mt Ds Rg Cn Uut Fl Uup Lv Uus Uuo';
@element = split(/\s+/, $pt);
$mem='memory,800,m';

$workdir=`pwd`;
chomp $workdir;

$basis1 = 'basis, VDZ-F12';
$basis2 = 'basis, VTZ-F12';

$VTZNP ='Basis={
Default=VTZ;
s,H,VTZ;c;
s,He,VTZ;c;
sp,Li,VTZ;c;
sp,Be,VTZ;c;
sp,B,VTZ;c;
sp,C,VTZ;c;
sp,N,VTZ;c;
sp,O,VTZ;c;
sp,F,VTZ;c;
sp,Ne,VTZ;c;
sp,Na,VTZ;c;
sp,Mg,VTZ;c;
sp,Al,VTZ;c;
sp,Si,VTZ;c;
sp,P,VTZ;c;
sp,S,VTZ;c;
sp,Cl,VTZ;c;
sp,Ar,VTZ;c;
}';

$VDZNP = 'Basis={
Default=VDZ;
s,H,VDZ;c;
s,He,VDZ;c;
sp,Li,VDZ;c;
sp,Be,VDZ;c;
sp,B,VDZ;c;
sp,C,VDZ;c;
sp,N,VDZ;c;
sp,O,VDZ;c;
sp,F,VDZ;c;
sp,Ne,VDZ;c;
sp,Na,VDZ;c;
sp,Mg,VDZ;c;
sp,Al,VDZ;c;
sp,Si,VDZ;c;
sp,P,VDZ;c;
sp,S,VDZ;c;
sp,Cl,VDZ;c;
sp,Ar,VDZ;c;
}';


foreach $name (@ARGV){
  @out=split(/\./, $name);
  open(FILE2,">$out[0]_W3X.com") || die "can't open $!"; 
  print FILE2 "***,$out[0]\n";
  print FILE2 "$mem\n";
  
  open(FILE4, "<$name") || die "can't open $!";
  @lines = (<FILE4>);
  close FILE4;
  @geom = grabgeom(@lines);
  @cm = grabCM(@lines);
  $noa = @geom ;
  $spin = $cm[1]-1;
  print FILE2 "Set,Charge=$cm[0],Spin=$spin;\n";
  print FILE2 "geomtyp=xyz\n";  
  print FILE2 'geometry={';
  print FILE2 "\n$noa \n";
  print FILE2 "$out[0]\n";
  for $i (0 .. @geom-1){
     print FILE2 "$geom[$i] \n";
   }
  print FILE2 "}\n";

  print FILE2 "$basis1 \n";

  if ($spin == 0) {
    print FILE2 "hf; \nccsd-f12;\n method(1)='HF(CABS)     VDZ-F12';\n method(2)='CCSD-F12b    VDZ-F12';\n  e(1)=energr;\n e(2)=energy(2);\n \n";
  } else {
    print FILE2 "hf; \nuccsd-f12;\n method(1)='HF(CABS)     VDZ-F12';\n method(2)='CCSD-F12b    VDZ-F12';\n e(1)=energr;\n e(2)=energy(2);\n \n";
  }
  print FILE2 "$basis2 \n";
  if ($spin == 0) {
    print FILE2 "hf; \nccsd-f12;\n method(3)='HF(CABS)     VTZ-F12';\n method(4)='CCSD-F12b    VTZ-F12';\n e(3)=energr;\n e(4)=energy(2);\n\n";
  } else {
    print FILE2 "hf; \nuccsd-f12;\n method(3)='HF(CABS)     VTZ-F12';\n method(4)='CCSD-F12b    VTZ-F12';\n e(3)=energr;\n e(4)=energy(2);\n\n"; 
  }
  print FILE2 "basis, aug-cc-pV(D+d)Z,H=VDZ \n";
    if ($spin == 0) {
    print FILE2 "hf; \nccsd(t);\n method(5)='CCSD         7-DZ';\n method(6)='CCSD(T)      7-DZ';\n e(5)=energc;\n e(6)=energy;\n\n";
  } else {
    print FILE2 "hf; \nuccsd(t);\n method(5)='CCSD         7-DZ';\n method(6)='CCSD(T)      7-DZ';\n e(5)=energc;\n e(6)=energy;\n\n";
  }
  print FILE2 "basis, aug-cc-pV(T+d)Z,H=VTZ \n";
    if ($spin == 0) {
    print FILE2 "hf; \nccsd(t);\n method(7)='CCSD         7-TZ';\n method(8)='CCSD(T)      7-TZ';\n e(7)=energc;\n e(8)=energy;\n\n";
  } else {
    print FILE2 "hf; \nuccsd(t);\n method(7)='CCSD         7-TZ';\n method(8)='CCSD(T)      7-TZ';\n e(7)=energc;\n e(8)=energy;\n\n"; 
  }

  print FILE2 "Basis=cc-pCVTZ; \n RHF;\n RMP2;\n method(9)='MP2          CVTZ';\n e(9)=energy;\n\n";
  print FILE2 "DKroll=1;\n RHF;\n {RMP2; Core;}\n method(10)='MP2(FU,DKH)  CVTZ';\n e(10)=energy;\n";
  print FILE2 "ehf=e(3)-(e(3)-e(1))*3^-5/(3^-5-2^-5);\n";
  print FILE2 "ecc=(e(4)-e(3))-((e(4)-e(3))-(e(2)-e(1)))*3^-3.6725/(3^-3.6725-2^-3.6725);\n";
  print FILE2 "e_t=(e(8)-e(7))-((e(8)-e(7))-(e(6)-e(5)))*3^-2.0436/(3^-2.0436-2^-2.0436);\n";
  print FILE2 "efu=e(10)-e(9);\n method(11)='W1X-1              ';\n e(11)=ehf+ecc+e_t+efu;\n\n";
  print FILE2 "$VTZNP \n";
  
  if ($spin == 0) {
    print FILE2 "RHF;\n CCSD(T);\n";
  }  else {
    print FILE2 "RHF;\n UCCSD(T);\n";
  }
  print FILE2 "method(12)='CCSD(T)      VTZ-NP'; \n e(12)=energy;\n";
  print FILE2 "MRCC,Method=CCSDT;\n method(13)='CCSDT        VTZ-NP';\n e(13)=energy;";
    
  print FILE2 "$VDZNP \n";
  if ($spin == 0) {
    print FILE2 "RHF;\n ";
  }  else {
    print FILE2 "UHF;\n ";
  } 
   print FILE2 "MRCC,Method=CCSDT,Dir=MRCCBUFFER;\n method(14)='CCSDT        VDZ-NP';\n e(14)=energy; \n\n" ;
   print FILE2 "MRCC,Method=CCSDT(Q),Dir=MRCCBUFFER,Restart=1;\n method(15)='CCSDT(Q)     VDZ-NP';\n e(15)=energy; \n\n";
   print FILE2 "et3=1.1373*(e(13)-e(12)); \n e_q=0.6852*(e(15)-e(14));\n method(16)='W3X                ';\n e(16)=e(11)+et3+e_q;\n table,method,e;\n\n";
   print FILE2 "---";
   close FILE2;

 }

sub grabgeom{
  my ($i, $j, $k, $start, $end, $temp,@temp3,@temp2, @geometry);
  @lines = @_ ;
  for $k (0 .. @lines) {
     $_=$lines[$k];
     if (m/NAtoms=/) {
       @temp3 = ($_ =~ /\S+/g);
       $natom = $temp3[1];
     }
     if (m/Input orientation/) {
       $start=$k+5;
       $end = $start + $natom-1;
       last;
     }
   }


  for $i ($start .. $end) {
    $temp=$lines[$i];
    @temp2=($temp=~ /\S+/g);
    $j = $temp2[0]-1;
    $geometry[$j] = "$element[$temp2[1]-1]  $temp2[3]  $temp2[4]  $temp2[5]";
  }
 return @geometry;
}

sub grabCM{
 my ( $k,  @temp, @chargemultip);
  @lines = @_ ;
  for $k (0 .. @lines) {
     $_=$lines[$k];
     if (m/Charge =  /) {
      @temp=($_ =~ /\S+/g);
      $chargemultip[0] = $temp[2];
      $chargemultip[1] = $temp[5];
      last;
     }
   }

 return @chargemultip;
}

