wfn=*.fchk

[grid]
%process
    electron
    alie
%command
#!/bin/bash
    mkdir -p ${input}_ALIE
    mv density.cub ${input}_ALIE/${input}_den.cub
    mv avglocion.cub ${input}_ALIE/${input}_alie.cub
    mv ${output} ${input}_ALIE
end

[surface]
%process
    alieext
%command
#!/bin/bash

    unit=$(grep "REMARK.*Unit of B-factor field" surfanalysis.pdb | awk '{print $NF}')
    if [ -z "$unit" ]; then unit="unknown"; fi

    printf "%5s %6s %4s %15s\n" "index" "serial" "type" "Unit($unit)" > index.txt
    index=0
    while IFS= read -r line; do
        if [[ $line =~ ^HETATM ]]; then
            serial=$(echo "$line" | awk '{print $2}')
            type=$(echo "$line" | awk '{print $3}')
            value=$(echo "$line" | cut -c61-66 | awk '{print $1}')
            printf "%5d %6d %4s %8s\n" "$index" "$serial" "$type" "$value" >> index.txt
            ((index++))
        fi
    done < surfanalysis.pdb

    # 从 surface 输出中提取极值（单位 eV），并换算为 Hartree
    extrema_line=$(grep -m1 "Minimal value:.*Maximal value:" "${output}" | tr -d '\r')
    scale_cmd=""

    if [ -n "$extrema_line" ]; then
        min_ev=$(printf '%s\n' "$extrema_line" | sed -E 's/.*Minimal value:[[:space:]]*([-+0-9.]+)[[:space:]]*eV.*/\1/')
        max_ev=$(printf '%s\n' "$extrema_line" | sed -E 's/.*Maximal value:[[:space:]]*([-+0-9.]+)[[:space:]]*eV.*/\1/')

        if [ -n "$min_ev" ] && [ -n "$max_ev" ]; then
            read min_au max_au < <(
                awk -v min="$min_ev" -v max="$max_ev" 'BEGIN{printf "%.6f %.6f\n", min/27.211, max/27.211}'
            )
            scale_cmd="mol scaleminmax 0 1 $min_au $max_au"
        fi
    fi

    # 如果没解析到，就留一行注释，避免脚本报错
    if [ -z "$scale_cmd" ]; then
        scale_cmd="# mol scaleminmax 0 1 <min> <max>   ;# failed to parse from ${output}"
    fi

    cat << EOF > ${input}_ALIE/alie.bat
vmd -e alie.vmd
EOF

    cat << EOF2 > ${input}_ALIE/alie.vmd
vcube *_den.cub map *_alie.cub
$scale_cmd
mol new surfanalysis.pdb
mol modstyle 0 top VDW 0.07 20
mol modselect 0 top name C
mol modcolor 0 top ColorID 32
mol addrep top
mol modstyle 1 top VDW 0.07 20
mol modselect 1 top name O
mol modcolor 1 top ColorID 21
EOF2

    mv index.txt ${output} surfanalysis.pdb ${input}_ALIE
end

%command
cd ${input}_ALIE
vmd -e alie.vmd
end

# Bundled by bane dysta
# ConfDir: D:\MyProgram\banewfn\conf

#>>> BANEWFN_INLINE_CONF_BEGIN grid
## bundled module: grid
# # 主逻辑
# [main]
# 5
# 
# # define
# [wfn2]
# 0
# 1
# ${operator}
# 
# # density.cub
# [electron]
# 1
# ${grid:-3}
# 2
# 0
# 5
# 
# # spindensity.cub
# [spin]
# 5
# ${grid:-2}
# 2
# 0
# 5
# 
# # ELF.cub
# [elf]
# 9
# ${grid:-3}
# 2
# 0
# 5
# 
# # LOL.cub
# [lol]
# 10
# ${grid:-3}
# 2
# 0
# 5
# 
# # totesp.cub 
# # 注意：由于需要切换cub文件，esp必须作为最后一个处理步骤使用
# [esp]
# 12
# ${grid:-1}
# 2
# 0
# r
# totesp.cub
# 13
# 11
# 5
# 627.51
# 0
# totesp.cub
# -1
# 5
# 
# # avglocion
# [alie]
# 18
# ${grid:-3}
# 2
# 0
# 5
# 
# # 退出
# [quit]
# -10
# q
#<<< BANEWFN_INLINE_CONF_END grid

#>>> BANEWFN_INLINE_CONF_BEGIN surface
## bundled module: surface
# [main]
# 12
# 
# [espext]
# 3
# 0.15
# 0
# 5
# mol.pdb
# 6
# 2
# -1
# -1
# 12
# 
# # surfanalysis.pdb
# [alieext]
# 1               # Select the way to define surface
# 1               #  Isosurface of electron density
# 0.0005          
# 2               # Select mapped function
# 2               # Average local ionization energy (${input}_ALIE)
# 0
# 2
# -1
# -1
# 12
# 
# [quit]
# -1
# q
#<<< BANEWFN_INLINE_CONF_END surface

