$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,1900,m';

$workdir=`pwd`;
chomp $workdir;
$basis1 = 'basis, aug-cc-pV(D+d)Z, H=VDZ';
$basis2 = 'basis, aug-cc-pV(T+d)Z, H=VTZ';
$basis3 = 'basis, cc-pwCVDZ';
$basis4 = 'basis, cc-pwCVTZ';
$WM = 'WMS';

for $i (0 .. @ARGV-1){ 
  $name = $ARGV[$i];
  @out=split(/\./, $name);
  
  open(FILE2,">$out[0]_$WM.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 \n{ccsd(t)-f12,gem_beta=0.9 ,df_basis=vdz/mp2fit,df_basis_exch=vdz/jkfit,ri_basis=vdz/jkfit}\n";
  } else {
    print FILE2 "HF \n{uccsd(t)-f12,gem_beta=0.9,df_basis=vdz/mp2fit,df_basis_exch=vdz/jkfit,ri_basis=vdz/jkfit}\n";
  }

  print FILE2 "SHOW,ENERG*,EMP2*,EF12* \n\n";
  print FILE2 "$basis2 \n";
  if ($spin == 0) {
    print FILE2 "HF \n{ccsd(t)-f12,gem_beta=1.0,df_basis=vtz/mp2fit,df_basis_exch=vtz/jkfit,ri_basis=vtz/jkfit}\n";
  } else {
    print FILE2 "HF \n{uccsd(t)-f12,gem_beta=1.0,df_basis=vtz/mp2fit,df_basis_exch=vtz/jkfit,ri_basis=vtz/jkfit}\n";
  }

  print FILE2 "SHOW,ENERG*,EMP2*,EF12* \n\n";

  print FILE2 "$basis3 \n";

  if ($spin == 0) {
    print FILE2 "HF; \nccsd(t)\nSHOW,ENERG*,EMP2* \nHF;\n{ccsd(t); Core,c1,c2,c3,c4,c5,c6,c7,c8;}\nSHOW,ENERG*,EMP2*\n\n";
  } else {
    print FILE2 "HF; \nuccsd(t)\nSHOW,ENERG*,EMP2* \nHF;\n{uccsd(t); Core,c1,c2,c3,c4,c5,c6,c7,c8;}\nSHOW,ENERG*,EMP2*\n\n";
  }

  print FILE2 "$basis4 \n";
  print FILE2 "HF; \nRMP2;\nSHOW,ENERG*,EMP2* \nHF;\n{RMP2; Core,c1,c2,c3,c4,c5,c6,c7,c8;}\nSHOW,ENERG*,EMP2* \n\n";

  print FILE2 "basis, AVDZ-DK, h=VDZ-DK \ndkroll=1 \nHF \nRMP2; \nSHOW,ENERG*,EMP2*  \n\n";
  print FILE2 "basis, AVDZ, h=VDZ \ndkroll=0 \nHF \nRMP2;\nSHOW,ENERG*,EMP2*   \n\n";

  print FILE2 "basis, AVTZ-DK, h=VTZ-DK \ndkroll=1 \nHF \nRMP2; \nSHOW,ENERG*,EMP2*  \n\n";
  print FILE2 "basis, AVTZ, h=VTZ \ndkroll=0 \nHF \nRMP2;\nSHOW,ENERG*,EMP2*   \n\n";

  print FILE2 "---";
  close FILE2;

 }


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

     if (($opt==1) && (m/Standard 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;
}


