#!/usr/bin/env bash
set -euo pipefail


# For xnvme support, meson build system is required
# https://github.com/xnvme/xnvme

# Rebuilds a static Linux AMD64 fio binary with io_uring support.
# Output binary path: ./fio

# Directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

TARGET_DIR="$SCRIPT_DIR/../../"

lastWORK_DIR=''
WORK_DIR=''
cd "$TARGET_DIR"
source "${TARGET_DIR}/lib/trim.sh"


createOrSetWorkDir()
{
  # Check if previous WORK_DIR already set
  if [ -f ".WORK_DIR" ]; then
    lastWORK_DIR=$(cat .WORK_DIR)
  fi
    
  if [ ! -d "$lastWORK_DIR" ]; then
      lastWORK_DIR=""
  fi
  

  # Either we don't have a previous WORK_DIR or it doesn't exist
  if [ "$lastWORK_DIR" == "" ]; then

    # printf "%-8s [CREATE]"
    # Create one
    declare -g WORK_DIR="$(mktemp -d)"
    #printf "$WORK_DIR" > .WORK_DIR
  else
    # printf "%-8s [OK]"
    declare -g WORK_DIR="$lastWORK_DIR"
  fi

  cd "$WORK_DIR"
}

ispinner "Setting up build environment..." &
createOrSetWorkDir

sleep 1


SPIN_PID=$!
kill $SPIN_PID

# echo "[OK]"
printf "\b\b\b\b[%s]\n" "" "OK"

printf "%-4s WORKDR_DIR: %s\n" "" "$WORK_DIR"


printf "%-4s We will now build fio in $WORK_DIR\n"  ""
printf "%-4s Intermediate files will be deleted on completion\n" ""
printf "%-4s and binaries will be deposited in ./%s" " " $(basename "$SCRIPT_DIR")


# Delete the WORK_DIR on exit, so we don't leave a mess
trap 'rm -rf "$WORK_DIR"' EXIT

need_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    printf 'error: required command not found: %s\n' "$1" >&2
    exit 1
  fi
}

need_cmd git
need_cmd make
need_cmd gcc
need_cmd pkg-config
need_cmd file
need_cmd ldd

liburing_prefix="$WORK_DIR/liburing-static"

git clone --depth=1 https://github.com/axboe/liburing.git "$WORK_DIR/liburing"


# If we don't change into the directory, the stuff ends up directly in $WORK_DIR
cd "$WORK_DIR/liburing"
"$WORK_DIR/liburing/configure" --prefix="$liburing_prefix"
make -C "$WORK_DIR/liburing" -j"$(nproc)"

# There is no actual need to install it
# make -C "$WORK_DIR/liburing" install

printf "Status: %s\n"  "$?"
echo "Finished building liburing?"
# exit


cd "$WORK_DIR"
git clone --depth=1 https://github.com/axboe/fio.git "$WORK_DIR/fio-src"


cd "$WORK_DIR/fio-src"
export PKG_CONFIG_PATH="${liburing_prefix}/lib/pkgconfig"
export PKG_CONFIG="pkg-config --static"

EXTFLAGS="-static" "$WORK_DIR/fio-src/configure" --disable-native --build-static
make -C "$WORK_DIR/fio-src" -j"$(nproc)"

printf "Status: %s\n" "$?"

cp "$WORK_DIR/fio-src/fio" "${TARGET_DIR}/fio-io_uring"

cd "$TARGET_DIR"

printf 'Verifying static linkage...\n'
file ./fio-io_uring
ldd ./fio-io_uring || true

printf 'Verifying io_uring engine...\n'
./fio-io_uring --enghelp | grep -E '(^|[[:space:]])io_uring([[:space:]]|$)'

printf 'Done. Rebuilt binary at %s/fio-io_uring\n' "${TARGET_DIR}"



# # Fetching libaio
# git clone https://github.com/anlongfei/libaio
# cd libaio
# make prefix=../build ENABLE_SHARED=false
# make prefix=../build ENABLE_SHARED=false install

# cd ../fio-src/
# export PKG_CONFIG_PATH="../build/lib/pkgconfig"
# export PKG_CONFIG="pkg-config --static"
# EXTFLAGS="-static" ./configure" --disable-native --build-static