#!/bin/bash

file_list=$(find 22-05-13-LPS-no-POTCAR -name OUTCAR -exec echo {} \;)

substr="SCF"
for outcar in $file_list; do
    parentdir="$(dirname "$outcar")"

    # If the directory contains "SCF", skip it
    if [[ $parentdir == *"$substr"* ]]; then
        continue
    fi

    # For every calculation of a spectrum, grep the spectral function and
    # put it into `mu.txt`
    target="$parentdir"/mu.txt
    grep "IMAGINARY DIELECTRIC FUNCTION" "$outcar" -A 40002 > "$target"

    # Similarly, extract the Fermi energy from the outcar file and put that
    # in `efermi.txt`
    target="$parentdir"/efermi.txt
    grep "Fermi energy" "$outcar" | tail -n 1 | awk '{print $3}' > "$target"

    echo "done:" "$parentdir"

done
