{{=<% %>=}}

GROUP_ID := env_var('GROUP_ID')
ARTIFACT_ID := env_var('ARTIFACT_ID')
VERSION_ID := `cat version_id`

TARGET_FOLDER := 'target'

YELLOW_COLOR := '\033[0;33m'
GREEN_COLOR := '\033[0;92m'
NORMAL_TEXT := '\033[0m'
CURRENT_TIME := `date`
CURRENT_OS := os()

##################################################################################################################

# Help
default:
    @echo "List of available recipes"
    @just --list

# Install project requirements (OS will be detected automatically)
requirements:
    @{{ if CURRENT_OS == 'macos' {"just _macos-requirements" } else {""} }}
    @{{ if CURRENT_OS == 'linux' {"just _linux-requirements" } else {""} }}

# Install project requirements for MacOS
_macos-requirements:
    @just _cmdprint "Installing MacOS requirements...\n"
    -brew install git
    -brew install coreutils
    -brew install direnv
    -brew install cljstyle && xattr -r -d com.apple.quarantine /usr/local/bin/cljstyle
    -brew install borkdude/brew/clj-kondo

    @just _cprint '{{YELLOW_COLOR}}' "\nDon't forget to install 'direnv' hook for your shell.\n"
    @echo 'zsh hook example:'
    @echo '\teval "$(direnv hook zsh)"\n'


# Install project requirements for Linux
_linux-requirements:
    @echo "Installing Linux requirements...is not implemented."
    @echo "Please, ensure that the following tools are installed: git, direnv, cljstyle, clj-kondo."
    @exit 1

# Print command name
_cmdprint text:
    #!/usr/bin/env bb
    (println "-----------------------------------------------")
    (import 'java.time.format.DateTimeFormatter 'java.time.LocalDateTime)
    (def date (LocalDateTime/now))
    (def formatter (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss"))
    (printf "{{GREEN_COLOR}}%s\n"(.format date formatter))
    (println "{{YELLOW_COLOR}}{{text}}{{NORMAL_TEXT}}")

_cprint color text:
    #!/usr/bin/env bb
    (println "{{color}}{{text}}{{NORMAL_TEXT}}")

##################################################################################################################

# Clean target folder
clean:
    @just _cmdprint "Clean target folder...\n"
    @rm -rf ./{{TARGET_FOLDER}}/*


# Build deployable jar file of this project
build:
    @just _cmdprint "Building project...\n"
    @mkdir -p target
    clojure -X:jar :jar {{TARGET_FOLDER}}/{{ARTIFACT_ID}}-{{VERSION_ID}}.jar :group-id {{GROUP_ID}} :artifact-id {{ARTIFACT_ID}} :version '"{{VERSION_ID}}"'


# Install deployable jar locally (requires the pom.xml file)
install:
    @just _cmdprint "Installing jar file to local .m2 repository...\n"
    clojure -X:install :installer :local :artifact '"{{TARGET_FOLDER}}/{{ARTIFACT_ID}}-{{VERSION_ID}}.jar"'


# Deploy this library to Clojars
deploy:
    @just _cmdprint "Deploying jar file to Clojars...\n"
    clojure -X:install :installer :remote :artifact '"{{TARGET_FOLDER}}/{{ARTIFACT_ID}}-{{VERSION_ID}}.jar"'


# Run Clojure repl
repl:
    @just _cmdprint "Running Clojure repl...\n"
    @clojure -M:repl


# Check for outdated dependencies
outdated:
    @just _cmdprint "Checking for outdated dependencies...\n"
    @clojure -M:outdated


# Run tests
test:
    @just _cmdprint "Running tests...\n"
    @clojure -M:test


# Format source code
format:
    @just _cmdprint "Formatting source code...\n"
    @cljstyle fix


# Lint source code
lint:
	@just _cmdprint "Linting source code...\n"
	clj-kondo --parallel --lint src:test/src:dev/src
	cljstyle check


#  Bump version artifact in `version_id` file, level may be one of: major, minor, patch, alpha, beta, rc, release.
bump level='patch' value='':
    @just _cmdprint "Bumping version artifact: {{VERSION_ID}} at level {{level}} in file 'version_id'...\n"
    @bb -f scripts/bump-semver.clj {{VERSION_ID}} {{level}} {{value}} > version_id
    @just _cprint '{{YELLOW_COLOR}}' "New version artifact: `cat version_id`\n"

<%={{ }}=%>
