 $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,avtz, h=vtz';
$basis2 = 'basis,avqz, h=vqz';

$CVTZ3d = 'Basis={
Default=cc-pCVTZ;
spd,H,cc-pVTZ;c;
spd,He,cc-pVTZ;c;
spf,Li,cc-pCVTZ;c;
spf,Be,cc-pCVTZ;c;
spf,B,cc-pCVTZ;c;
spf,C,cc-pCVTZ;c;
spf,N,cc-pCVTZ;c;
spf,O,cc-pCVTZ;c;
spf,F,cc-pCVTZ;c;
spf,Ne,cc-pCVTZ;c;
spf,Na,cc-pCVTZ;c;
spf,Mg,cc-pCVTZ;c;
spf,Al,cc-pCVTZ;c;
spf,Si,cc-pCVTZ;c;
spf,P,cc-pCVTZ;c;
spf,S,cc-pCVTZ;c;
spf,Cl,cc-pCVTZ;c;
spf,Ar,cc-pCVTZ;c;
d,Li,cc-pCVQZ;c;
d,Be,cc-pCVQZ;c;
d,B,cc-pCVQZ;c;
d,C,cc-pCVQZ;c;
d,N,cc-pCVQZ;c;
d,O,cc-pCVQZ;c;
d,F,cc-pCVQZ;c;
d,Ne,cc-pCVQZ;c;
d,Na,cc-pCVQZ;c;
d,Mg,cc-pCVQZ;c;
d,Al,cc-pCVQZ;c;
d,Si,c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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;
d,Li,VDZ;c;
d,Be,VDZ;c;
d,B,VDZ;c;
d,C,VDZ;c;
d,N,VDZ;c;
d,O,VDZ;c;
d,F,VDZ;c;
d,Ne,VDZ;c;
d,Na,VDZ;c;
d,Mg,VDZ;c;
d,Al,VDZ;c;
d,Si,VDZ;c;
d,P,VDZ;c;
d,S,VDZ;c;
d,Cl,VDZ;c;
d,Ar,VDZ;c;
}';

$VDZd = 'Basis={
Default=VDZ;
s,H,VDZ;c;
s,He,VDZ;c;
}';

foreach $name (@ARGV){
  @out=split(/\./, $name);
  open(FILE2,">$out[0]_W3XL.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(t)-f12;\n method(1)='HF(CABS)     7-TZ';\n method(2)='CCSD-F12b    7-TZ';\n method(3)='CCSD(T)-F12b 7-TZ';\n e(1)=energr;\n e(2)=energc(2);\n e(3)=energy(2);\n\n";
  } else {
    print FILE2 "hf; \nuccsd(t)-f12;\n method(1)='HF(CABS)     7-TZ';\n method(2)='CCSD-F12b    7-TZ';\n method(3)='CCSD(T)-F12b 7-TZ';\n e(1)=energr;\n e(2)=energc(2);\n e(3)=energy(2);\n\n";
  }
  print FILE2 "$basis2 \n";
  if ($spin == 0) {
    print FILE2 "hf; \nccsd-f12;\n method(4)='HF(CABS)     7-QZ';\n method(5)='CCSD-F12b    7-QZ';\n e(4)=energr;\n e(5)=energy(2);\n\n";
  } else {
    print FILE2 "hf; \nuccsd-f12;\n method(4)='HF(CABS)     7-QZ';\n method(5)='CCSD-F12b    7-QZ';\n e(4)=energr;\n e(5)=energy(2);\n\n"; 
  }
  print FILE2 "$CVTZ3d \n";
  print FILE2 "RHF;\n RMP2;\n method(6)='MP2          CVTZ(3d)';\n e(6)=energy; \n DKroll=1; \n RHF; \n{RMP2; Core;} \n method(7)='MP2(CR)      CVTZ(3d)'; \n e(7)=energy;\n\n"; 
  print FILE2 "$CVDZ2d \n DKroll=0;\n";
  if ($spin == 0) {
    print FILE2 "RHF;\n CCSD(T);\n";
  }  else {
    print FILE2 "RHF;\n UCCSD(T);\n";
  }
  print FILE2 "method(8)='MP2          CVDZ(2d)';\n method(9)='CCSD(T)      CVDZ(2d)';\n e(8)=emp2;\n e(9)=energy;\n DKroll=1;\n";
  if ($spin == 0) {
    print FILE2 "RHF;\n {CCSD(T); Core;}\n";
  }  else {
    print FILE2 "RHF;\n {UCCSD(T); Core;}\n";
  }
  print FILE2 "method(10)='MP2(CR)      CVDZ(2d)';\n method(11)='CCSD(T)(CR)  CVDZ(2d)';\n e(10)=emp2;\n e(11)=energy;\n ehf=e(4);\n ecc=(e(5)-e(4))-((e(5)-e(4))-(e(2)-e(1)))*4^-5.8769/(4^-5.8769-3^-5.8769);\n e_t=(e(3)-e(2))*1.0607;\n ecr=(e(7)-e(6))*1.1383+((e(11)-e(9))-(e(10)-e(8)))*1.5577;\n method(12)='W2X              ';\n e(12)=ehf+ecc+e_t+ecr;\n\n";
  print FILE2 "$VDZNP \n";
  if ($spin == 0) {
    print FILE2 "RHF;\n CCSD(T);\n";
  }  else {
    print FILE2 "RHF;\n UCCSD(T);\n";
  }
  print FILE2 "method(13)='CCSD(T)      VDZ(NP)';\n e(13)=energy;\n MRCC,Method=CCSDT;\n method(14)='CCSDT        VDZ(NP)';\n e(14)=energy;\n\n";
    
  print FILE2 "$VTZd \n";
  if ($spin == 0) {
    print FILE2 "RHF;\n CCSD(T);\n";
  }  else {
    print FILE2 "RHF;\n UCCSD(T);\n";
  }
  print FILE2 "method(15)='CCSD(T)      VTZ(d)';\n e(15)=energy;\n MRCC,Method=CCSDT;\n method(16)='CCSDT        VTZ(d)';\n e(16)=energy;\n\n";  
  
  print FILE2 "$VDZd \n";
  if ($spin == 0) {
    print FILE2 "RHF;\n ";
  }  else {
    print FILE2 "UHF;\n ";
  } 
  
  print FILE2 "MRCC,Method=CCSDT,Dir=MRCCBUFFER;\n method(17)='CCSDT        VDZ(d)';\n e(17)=energy;\n MRCC,Method=CCSDT(Q),Dir=MRCCBUFFER,Restart=1;\n method(18)='CCSDT(Q)     VDZ(d)';\n";
  print FILE2 "e(18)=energy;\n eft=(e(16)-e(15))-((e(16)-e(15))-(e(14)-e(13)))*3^-2.6069/(3^-2.6069-2^-2.6069);\n e_q=(e(18)-e(17))*0.7837;\n method(19)='W3X-L               ';\n e(19)=e(12)+eft+e_q;\n table,method,e;\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]; 
#       print "# of atoms  $natom;"
     }
     if (m/Input orientation/) { 
       $start=$k+5;
       $end = $start + $natom-1;
       last;
     }
#     if (m/Distance matrix/) { 
#      $end = $k-2;
#      last;
#     }
   }

    
  for $i ($start .. $end) {
    $temp=$lines[$i];
#    print "$temp \n";
    @temp2=($temp=~ /\S+/g);
    $j = $temp2[0]-1;
#    print "$temp2[0] $temp2[1]  $temp2[3]  $temp2[4]  $temp2[5] \n";
    $geometry[$j] = "$element[$temp2[1]-1]  $temp2[3]  $temp2[4]  $temp2[5]";
#    print "$geometry[$j] \n"
  }
 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];
#      print "charge =  $chargemultip[0] multiplicity = $chargemultip[1] \n";
      last;
     }
   }

 return @chargemultip;
}
   

