#!/usr/bin/env bash
set -e
solution_dir=$(cd "$(dirname "$0")"; pwd)
FOLDERS="src tests"
ALLOWED_EXTENSIONS_RE=".*\.\(cpp\|hpp\|c\|h\|inl\)"
EXCLUSION_RE="\(Bindings\|CLI\)/\|\(CppParser/\(Parse\)?\(Expr\|Stmt\)\)"
CLANG_FORMAT_OPTIONS="--fallback-style=none -i --verbose"

clang_format=""
VS_INSTALL_DIR=""


format()
{
    find_clang_format
    
    DRY_RUN=0
    # read -p "Dry run? (1/0): " DRY_RUN

    if [ "$DRY_RUN" -eq 1 ]; then
        CLANG_FORMAT_OPTIONS="$CLANG_FORMAT_OPTIONS --dry-run --Werror"
    fi

    # Format all files, in the specified list of directories
    # Loop through each file in the directory (and subdirectories)
    for p in $FOLDERS; do
        echo "Formatting files in folder \"$p\"..."
        cd "$solution_dir/$p"

        file_list=$(find . -type f -regex $ALLOWED_EXTENSIONS_RE -and -not -regex ".*/\($EXCLUSION_RE\).*" -print)
        
        "$clang_format" $CLANG_FORMAT_OPTIONS $file_list

        cd ..
    done
}

find_vswhere()
{
    echo "Looking for 'vswhere'..."
    VSWHERE_PATH="$(printenv 'ProgramFiles(x86)')/Microsoft Visual Studio/Installer/vswhere.exe"
    
    if [ -x "$(command -v vswhere.exe)" ]; then
        vswhere="vswhere.exe"
    elif [ -f "$VSWHERE_PATH" ]; then
        vswhere=$VSWHERE_PATH
    else
        echo -e "Could not locate vswhere.exe at:\n  " $VSWHERE_PATH
        read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key
        exit 1
    fi

    echo -e "Found 'vswhere.exe':\n  @" $vswhere "\n"
}

find_visual_studio()
{
    find_vswhere

    echo "Looking for visual studio..."

    # Find visual studio installation path
    VS_INSTALL_DIR=$("$vswhere" -latest -property installationPath)

    # Find visual studio installation path
    if [ ! -d "$VS_INSTALL_DIR" ]; then
        echo -e "Visual Studio Installation directory not found at vswhere specified path:\n  " $VS_INSTALL_DIR
        read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key
        exit 1
    fi

    echo -e "Found Visual Studio:\n  @" $VS_INSTALL_DIR "\n"
}

find_clang_format()
{
    echo "Looking for clang-format..."
    find_visual_studio

    CLANG_FORMAT_PATH="VC/Tools/Llvm/bin/clang-format.exe"
    clang_format="$VS_INSTALL_DIR/$CLANG_FORMAT_PATH"

    # Verify clang-format actually exists as well
    if ! [ -f "$clang_format" ]; then
        echo "clang-format.exe could not be located at: $clang_format"
        read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key
        exit 1
    fi
    
    echo -e "Found clang-format.exe:\n" "  Using" $("$clang_format" --version) "\n" "  @" $clang_format "\n"
}

format

echo "Done!"
read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key
exit 0