#!/usr/bin/env bash

set -euo pipefail

CLOJURE_TOOLS_VERSION="1.11.1.1200"

install_dir="${1:-/usr/local}"
mkdir -p "$install_dir"
cd /tmp
curl -O -sL "https://download.clojure.org/install/clojure-tools-$CLOJURE_TOOLS_VERSION.tar.gz"
tar xzf "clojure-tools-$CLOJURE_TOOLS_VERSION.tar.gz"
cd clojure-tools
clojure_lib_dir="$install_dir/lib/clojure"
mkdir -p "$clojure_lib_dir/libexec"
cp ./*.jar "$clojure_lib_dir/libexec"
cp deps.edn "$clojure_lib_dir"
cp example-deps.edn "$clojure_lib_dir"

sed -i -e 's@PREFIX@'"$clojure_lib_dir"'@g' clojure
mkdir -p "$install_dir/bin"
cp clojure "$install_dir/bin"
cp clj "$install_dir/bin"

cd /tmp
rm -rf "clojure-tools-$CLOJURE_TOOLS_VERSION.tar.gz"
rm -rf "clojure-tools"
echo "Installed clojure to $install_dir/bin"
