all: pull-all cache-update data builders_test builders_run (update "True" "True")

pull-all:
    # git pull
    git submodule update --recursive --remote

cache-update: pull-all flatcache runelite

flatcache: pull-all
    #!/usr/bin/env bash
    root=$(git rev-parse --show-toplevel)
    echo 'flatcache'
    echo 'building-flatcache'
    cd data/cache/osrs-flatcache
    mvn clean && mvn install -Dcheckstyle.skip=false
    cd packer/target
    jar_file=$(ls | grep .shaded.jar)
    echo 'extracting cache'
    rm -r "${root}/data/cache/cache-data"
    mkdir -p "${root}/data/cache/cache-data"
    java -jar "${jar_file}" unpack "${root}/data/cache/osrs-cache" "${root}/data/cache/cache-data"

runelite: flatcache
    #!/usr/bin/env bash
    root=$(git rev-parse --show-toplevel)
    rl=$(echo "${root}/runelite")
    echo "runelite"
    cd "${rl}"
    git pull

    echo "Installing custom plugins"
    cp -R "${root}/rl-plugins/*" "${rl}/runelite-client/src/main/java/net/runelite/client/plugins/"

    echo "Building RuneLite..."
    mvn clean
    mvn install -DskipTests

    # Find the cache.jar file with current version and bundled with dependencies
    # For example: cache-1.5.27-SNAPSHOT-jar-with-dependencies.jar
    cd "${rl}/cache/target"
    jar_file=$(ls | grep .jar-with-dependencies.)

    # Remove old cache dumps
    echo -e "  > Removing the old cache dump in osrsbox-db..."
    rm -r "${root}/data/cache/items/"
    rm -r "${root}/data/cache/npcs/"
    rm -r "${root}/data/cache/objects/"

    # Dump the cache
    echo -e "  > Dumping cache using RuneLite cache tool..."
    java -classpath "${jar_file}" net.runelite.cache.Cache \
    -cache "${root}/data/cache/cache-data" \
    -items "${root}/data/cache/items"

    java -classpath "${jar_file}" net.runelite.cache.Cache \
    -cache "${root}/data/cache/cache-data" \
    -npcs "${root}/data/cache/npcs"

    java -classpath "${jar_file}" net.runelite.cache.Cache \
    -cache "${root}/data/cache/cache-data" \
    -objects "${root}/data/cache/objects"

    echo "Run runelite, log in, and configure the metadatadumper. run ::item. There are about 27k items now"
    echo "TODO: MAKE THIS AUTOMATED. \$rl/cache/src/main/java/net/runelite/cache/Cache.java has a main function"

data: (poetry "install")
    #!/usr/bin/env bash
    root=$(git rev-parse --show-toplevel)
    poetry install

    poetry run python -m scripts.cache.update

    poetry run python -m scripts.icons.convert_item_icons

    poetry run python -m scripts.items.update

    poetry run python -m scripts.monsters.update

builders_test: (poetry "install")
    #!/usr/bin/env bash
    set -x
    poetry run python -m builders.items.builder --test=True
    poetry run python -m builders.monsters.builder --test=True

builders_run: (poetry "install") builders_test
    #!/usr/bin/env bash
    set -x
    poetry run python -m builders.items.builder --export=True
    poetry run python -m builders.monsters.builder --export=True

update monsters items: (pipx "poetry") data builders_run
    #!/usr/bin/env bash
    poetry run python -m scripts.update.update_json_files --monsters={{monsters}} --items={{items}}

poetry action: (pipx "poetry")
    poetry {{action}}

pipx package_to_install:
    pipx install {{ package_to_install }}

monsters:
    PYTHONPATH="." python scripts/monsters/update.py
    PYTHONPATH="." python builders/monsters/builder.py --test=True
    PYTHONPATH="." python builders/monsters/builder.py --export=True
    PYTHONPATH="." python scripts/update/update_json_files.py --monster=True
    cp docs/monsters-complete.json monsters-complete.json
