#!/usr/bin/env bash

# Check for rustc
printf "Checking for rustc..."
if [ -n "$RC" ] && command -v $RC &> /dev/null
then
    echo "$RC"
elif [ -n "$RUSTC" ] && command -v $RUSTC &> /dev/null
then
    echo "$RC"
    RC=$RUSTC
elif ! command -v rustc &> /dev/null
then
    echo "not found"
    echo "rustc not found. Please install it to continue."
    exit 1
else
    RC=rustc
    echo "rustc"
fi

# Check for cargo
printf "Checking for cargo..."
if [ -n "$CARGO" ] && command -v $CARGO &> /dev/null
then
    echo "$CARGO"
elif ! command -v cargo &> /dev/null
then
    echo "not found"
    echo "cargo not found. Please install it to continue."
    exit 1
else
    CARGO=cargo
    echo "cargo"
fi

# Check for make
printf "Checking for make..."
if ! command -v make &> /dev/null
then
    echo "not found"
    echo "make not found. Please install it to continue."
    exit 1
else
    echo "found"
fi

# Check for a C compiler
printf "Checking for C compiler..."
if [ -n "$CC" ] && command -v $CC &> /dev/null
then
    echo "$CC"
elif command -v gcc &> /dev/null
then
    CC=gcc
    echo "gcc"
elif command -v clang &> /dev/null
then
    CC=clang
    echo "clang"
elif command -v cc &> /dev/null
then
    CC=cc
    echo "cc"
else
    echo "not found"
    echo "No C compiler found. Please install gcc, clang, or some C compiler to continue."
    exit 1
fi

mkdir -p /tmp/e10b5eccac1ecb4e8706e3898719b197a1e9d10d65e860192d38155cb31233ea-configure
cd /tmp/e10b5eccac1ecb4e8706e3898719b197a1e9d10d65e860192d38155cb31233ea-configure
for header in stdio.h stdlib.h math.h; do
    printf "Checking for $header..."
    echo "#include <$header>" > test.c
    echo "int main() { return 0; }" >> test.c
    if $CC -c test.c -o test.o 2>/dev/null; then
        echo "found"
    else
        echo "not found"
        echo "Header file $header not found. Please ensure it is installed and in your include path."
        exit 1
    fi
    rm -f test.c test.o
done
cd - > /dev/null 2>&1
rm -rf /tmp/e10b5eccac1ecb4e8706e3898719b197a1e9d10d65e860192d38155cb31233ea-configure

# Check for ar
printf "Checking for ar..."
if command -v ar &> /dev/null
then
    echo "found"
else
    echo "not found"
    echo "ar not found. Please install it to continue. It usually comes with the coreutils package."
    exit 1
fi

printf "Checking if libmysolvers should be included..."
if [ "$INCLUDE_LIBMYSOLVERS" = "FALSE" ]; then
    echo "no"
elif [ "$INCLUDE_LIBMYSOLVERS" = "TRUE" ]; then
    echo "yes"
else
    echo "no"
    INCLUDE_LIBMYSOLVERS="FALSE"
fi

# Check for prefix
printf "Checking for prefix..."
if printf '%s\n' "$@" | grep -q -- "^--prefix="; then
    prefix_arg=$(printf '%s\n' "$@" | grep -m1 '^--prefix=')
    prefix_path="${prefix_arg#--prefix=}"
    PREFIX=$prefix_path
    echo "$PREFIX"
else
    PREFIX="/usr/local"
    echo "$PREFIX"
fi

# Check whether to build GUI
# printf "Checking if GUI should be built..."
# if printf '%s\n' "$@" | grep -q -- "^--enable-gui$"; then
#     echo "yes"
#     BUILD_GUI="TRUE"
# else
#     echo "no"
#     BUILD_GUI="FALSE"
# fi

# Check for python
# if [ "$BUILD_GUI" = "TRUE" ]; then
#     printf "Checking for python..."
#     if command -v python &> /dev/null || command -v python3 &> /dev/null
#     then
#         echo "found"
#     else
#         echo "not found"
#         echo "python not found. Please install it to continue."
#         exit 1
#     fi

#     printf "Checking for tkinter..."
#     if python -c "import tkinter" &> /dev/null || python3 -c "import tkinter" &> /dev/null
#     then
#         echo "found"
#     else
#         echo "not found"
#         echo "tkinter not found. Please install it to continue."
#         exit 1
#     fi

#     printf "Checking for python-is-python3 or similar..."
#     if command -v python &> /dev/null
#     then
#         echo "found"
#     else
#         echo "not found"
#         echo "The 'python' command is not found. Please install a package like 'python-is-python3' that links the 'python' command to 'python3', or create a similar alias."
#         exit 1
#     fi
# else
#     echo "Skipping python and tkinter checks since GUI is not being built."
# fi

# Checking for realpath
# printf "Checking for realpath..."
# if command -v realpath &> /dev/null
# then
#     echo "found"
# else
#     echo "not found"
#     echo "realpath not found. Please install it to continue."
#     exit 1
# fi

# Check OS type in order to determine proper sed
# Detect OS and set sed -i syntax
if [[ "$OSTYPE" == "darwin"* ]]; then
    SED_INPLACE=("sed" "-i" "")
else
    SED_INPLACE=("sed" "-i")
fi

# Patching Makefile
echo "Executing depfiles commands..."
cp Makefile.in Makefile
"${SED_INPLACE[@]}" "s#@CC@#$CC#g" Makefile
"${SED_INPLACE[@]}" "s#@PREFIX@#$PREFIX#g" Makefile
"${SED_INPLACE[@]}" "s#@CARGO@#$CARGO#g" Makefile
"${SED_INPLACE[@]}" "s#@RC@#$RC#g" Makefile
"${SED_INPLACE[@]}" "s#@INCLUDE_LIBMYSOLVERS@#$INCLUDE_LIBMYSOLVERS#g" Makefile
"${SED_INPLACE[@]}" "s#@BUILD_GUI@#$BUILD_GUI#g" Makefile
rm -rf /tmp/e10b5eccac1ecb4e8706e3898719b197a1e9d10d65e860192d38155cb31233ea

# Setting up
export CC="$CC"
# if [ "$BUILD_GUI" = "TRUE" ]; then
#     echo "Setting up aliases..."
#     rm -rf build
#     mkdir -p build/bin
#     if command -v python3 &> /dev/null; then
#         echo '#!/usr/bin/env bash
#     python3 "$@"' > build/bin/python
#         chmod +x build/bin/python
#         BUILD_BIN=$(cd build/bin && pwd)
#         if ! command -v python &> /dev/null; then
#             printf "Adding $BUILD_BIN to PATH..."
#             export PATH=$BUILD_BIN:$PATH
#             echo "done"
#         fi
#     fi
# fi

touch .configured
echo "Configuration complete. Run 'make' to build."
