#!/usr/bin/env bash

# vim: ft=sh

function show_help() {
    less <<END
$(basename "$0") [-?|-h|--help] [-f|--force] [-u|--update]

    -u --update   Update OpenAI OpenAPI spec from github
    -? -h --help  Show this help and exit
    -f --force    Force the rebuild, even if there are uncommitted changes
    -n --notest   Skip the tests

Attempts to rebuild this project, including running tests and rebuilding the
documentation. If you have uncommitted changes, this script will refuse to
run. If you want to force the rebuild, use the -f option.
END
}

force=false
notest=false
update=false
openai_url="https://raw.githubusercontent.com/openai/openai-openapi/master/openapi.yaml"
target_dir="share/openapi.yaml"

POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
    -f|--force)
    force=true
    shift
    ;;
    -n|--notest)
    notest=true
    shift
    ;;
    -u|--update)
    update=true
    shift
    ;;
    -?|-h|--help)
    shift # past argument
    show_help;
    exit;
    ;;
    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

set -Eeuo pipefail

# Check for uncommitted changes in the git repository
if git diff-index --quiet HEAD --; then
    # No uncommitted changes. Do nothing.
    echo Rebuilding project
else
    # Uncommitted changes found
    if [ "$force" = true ]; then
      echo "Uncommitted changes found, but proceeding due to -f flag."
    else
      echo "Uncommitted changes found. Use -f to force."
      exit 1
    fi
fi

if [ "$update" = true ]; then
    echo "Updating OpenAI OpenAPI spec from github"
    if command -v curl &> /dev/null; then
        curl -o $target_dir $openai_url
    elif command -v wget &> /dev/null; then
        wget -O $target_dir $openai_url
    else
        echo "Error: Neither curl nor wget is available."
        exit 1
    fi
fi

# Check if Makefile exists in the current directory
if [ -f Makefile ]; then
    # Run make realclean
    make realclean
    # Check if make realclean was successful
    if [ $? -eq 0 ]; then
      echo "make realclean executed successfully."
    else
      echo "make realclean failed."
    fi
fi

perl Makefile.PL

make

# Obviously, this builds the documentation
perl devel/build_docs.pl

# And we rebuild our README.md from the POD
if command -v pod2markdown &> /dev/null; then
    pod2markdown < lib/OpenAPI/Client/OpenAI.pm > README.md
else
    echo "Error: pod2markdown is not available."
    exit 1
fi

if [ "$notest" = false ]; then
    RELEASE_TESTING=1 make test
else
    echo Skipping tests due to -n|--notest flag
fi