#!/bin/bash

set -e -u

CURRENT_VERSION=$1
RELEASE_VERSION=$2

for f in bin/lein bin/lein-pkg bin/lein.bat project.clj leiningen-core/project.clj; do
    sed -i s/$CURRENT_VERSION/$RELEASE_VERSION/ $f
done

rm -rf target classes leiningen-core/target leiningen-core/classes
rm -rf $HOME/.lein/self-installs/leiningen-$RELEASE_VERSION-standalone.jar

cd leiningen-core
sed -i s/\;\;:aot/:aot/ project.clj
lein1 clean, install
sed -i s/:aot/\;\;:aot/ project.clj
cd ..

bin/lein with-profile release uberjar
cp target/leiningen-$RELEASE_VERSION-standalone.jar $HOME/.lein/self-installs

cp bin/lein /tmp/lein-$RELEASE_VERSION
cd /tmp

if [ ! -r test-project ]; then
    ./lein-$RELEASE_VERSION new test-project
fi

cd test-project

time ../lein-$RELEASE_VERSION run -m clojure.main/main -e nil
time ../lein-$RELEASE_VERSION run -m clojure.main/main -e nil
time ../lein-$RELEASE_VERSION run -m clojure.main/main -e nil

echo "If these are under 3 seconds then you should be set to upload"
echo "target/leiningen-$RELEASE_VERSION-standalone.jar to S3."
echo "s3c me put -P target/leiningen-$RELEASE_VERSION-standalone.jar s3://leiningen/downloads/"
echo "Also deploy this release of leiningen-core to Clojars."

cd -

git commit -a -m "Release $RELEASE_VERSION"
git tag -s $RELEASE_VERSION -m "Release $RELEASE_VERSION"

# Still manual for now:
# cd leiningen-core; lein do clean, deploy clojars, marg
# deploy leiningen itself
# scp docs/uberdoc.html philhag@leiningen.org:leiningen.org/reference.html
# git push && git push --tags
# git checkout stable && git merge master && git push
# Drop version back to SNAPSHOT
