echo "------------------------------------------------------------------------"
echo "------------------------NMR-DATA-GENERATOR------------------------------"
echo "------------------------------------------------------------------------"
echo ""
echo "This program reads the nmr data for the specific atom numbers to compare"
echo "Ensure the following files exist in current working directory "
echo "a) combine.f90.back "
echo "A fortran program to combine the output files data "
echo ""
echo "b) nmr-shift.f90.back" 
echo "A fortran program used to shift the shielding values"
echo ""
echo "b) Gaussian NMR output files "
echo "The output file names are in following format: prefix-number-suffix.log "
echo "Enter the prefix of file name "
echo "Ex. nm-180.log => prefix=nm, text-1.log => prefix=text-;" 
read -p " press Enter if none " pre
echo ""
echo "Enter suffix of the file name "
echo "Ex. nm-180-test.log => suffix=-test"
read  -p " press Enter if none "  post
echo ""
while true
do
    read -p "Enter which nmr you are looking for H/C/Cl: " atom

    # --- define atom-specific settings ---
    case $atom in
        H)
            label="H"
            shift_val=31.88
            name="hydrogen"
            ;;
        C)
            label="C"
            shift_val=182.46
            name="carbon"
            ;;
        Cl)
            label="Cl"
            shift_val=0.0
            name="chlorine"
            ;;
        *)
            echo "Invalid atom type. Use H, C, or Cl"
            continue
            ;;
    esac

    # --- input ---
    read -p "Enter starting number of $name: " start
    read -p "Enter last number of $name: " end
    read -p "Enter total number of points in each file: " n

    total=$((end - start + 1))

    # --- loop over atoms ---
    for h in $(seq $start $end)
    do
        grep " $h  $label.*Isotropic =" *.log > nmr-$h.dat

        sed -i "s/.log//g" nmr-$h.dat
        sed -i "s/://g" nmr-$h.dat
        sed -i "s/ $h  $label.*Isotropic =//g" nmr-$h.dat
        sed -i "s/Anisotropy =//g" nmr-$h.dat
        sed -i "s/$pre//g" nmr-$h.dat
        sed -i "s/$post//g" nmr-$h.dat

        cp nmr-shift.f90.back nmr-shift.f90
        sed -i "s/shift_value/$shift_val/g" nmr-shift.f90
        sed -i "s/input_file/nmr-$h.dat/g" nmr-shift.f90
        sed -i "s/output_file/oshift-$h.dat/g" nmr-shift.f90
        sed -i "s/no_of_points/$n/g" nmr-shift.f90

        gfortran nmr-shift.f90 -o nmr.exe
        ./nmr.exe
        rm -f nmr-shift.f90

        sort -k1,1n oshift-$h.dat > shift-$h.dat
    done

    # --- combine ---
if [ "$start" -eq "$end" ]; then
    echo "Only one $atom atom, skipping combine step"
    echo "Final file: shift-$start.dat"
else
    cp copy.f90.back copy.f90
    sed -i "s/no_of_h/$total/g" copy.f90
    sed -i "s/start_file/$start/g" copy.f90
    sed -i "s/combined.dat/combined-$atom-$start-$end.dat/g" copy.f90

    gfortran copy.f90 -o copy.exe
    ./copy.exe

    echo "Combined file: combined-$atom-$start-$end.dat"
fi

    echo "Done with the required $atom atoms"
    echo "-------------------------"
    echo " "

    read -p "Enter any key to continue or n to exit: " choice
    if [[ "$choice" == "n" || "$choice" == "N" ]]; then
        echo "Exiting..."
        break
    fi

done

rm -f *.exe *.f90 oshift-*
