#!/bin/bash
set -e

COMMIT_MESSAGE='.'
if [ "$1" == "-m" ]
then
    if [ "$2" == "" ]
    then
        echo "commit message required after -m"
        exit 1
    fi
    COMMIT_MESSAGE="$2"
else
    status="$(git status --porcelain)"
    if [ "$status" != "" ]
    then
        echo "uncommitted changes"
        exit 1
    fi
fi

# Pick a version number.

git fetch origin
TAG_NUM=`git tag | grep -Po '(?<=^v0\.)\d+$' | sort -n | tail -n1`
if [ "$TAG_NUM" == "" ]
then
    echo "couldn't find latest tag number"
    exit 1
fi
TAG="v0.$TAG_NUM"

NEW_GH_TAG=0
if gh release view $TAG >/dev/null
then
    TAG_NUM=$(($TAG_NUM + 1))
    TAG="v0.$TAG_NUM"
    NEW_GH_TAG=1
fi

# Write version number to Cargo.toml
BASEDIR=$(dirname "$0")
sed -i.bak "s/^version = \"0\.[0-9]\+\.0\" # \[\[this version number is written by release\.sh\]\]$/version = \"0.$TAG_NUM.0\" # [[this version number is written by release.sh]]/" "$BASEDIR/Cargo.toml" && rm "$BASEDIR/Cargo.toml.bak"

# Pass build time to the env!() in main.rs, passed-through by cargo.
export NND_BUILD_TIME=`date --utc +"%Y-%m-%d %H:%M:%S-%Z"`

# Build in all the modes, with musl and glibc.
for target in "" "--target=x86_64-unknown-linux-musl"
do
    echo "target: $target"
    RUST_BACKTRACE=1 cargo test $target
    for profile in "" "--profile=dbgo" "-r"
    do
        echo "profile: $profile"
        cargo build $target $profile
    done
done

# Commit the version number bump. This is required by crates.io. We usually piggyback it to some useful commit.
git status
git commit -am "$COMMIT_MESSAGE"
git push

# Copy to my machines.

scp target/x86_64-unknown-linux-musl/dbgo/nnd dev:bin/new-nnd && scp target/x86_64-unknown-linux-musl/release/nnd dev:bin/new-nnd-release && ssh dev 'mv bin/new-nnd bin/nnd; mv bin/new-nnd-release bin/nnd-release;'

cp target/x86_64-unknown-linux-musl/dbgo/nnd ../../nnd-release/nnd-new && mv ../../nnd-release/nnd-new ../../nnd-release/nnd && cp target/x86_64-unknown-linux-musl/release/nnd ../../nnd-release/nnd-release-new && mv ../../nnd-release/nnd-release-new ../../nnd-release/nnd-release

# Push to itch.
#butler push target/x86_64-unknown-linux-musl/release/nnd al13n/nnd:linux

# Push to github.

cp target/x86_64-unknown-linux-musl/dbgo/nnd nnd-dbgo
trap 'rm nnd-dbgo' EXIT

gh release create "$TAG" --notes "" target/x86_64-unknown-linux-musl/release/nnd nnd-dbgo

rm nnd-dbgo
trap - EXIT

# Push to crates.io
cargo publish

echo "all done!"
