#!/bin/sh
set -e
VERSION="$1"
test -n "$VERSION" || exit 1

dist="dist/basex-$VERSION"
src="release/BaseX943.zip"
dest="$dist/lib/basex-${VERSION}"
if ! test -d "$dest"; then
  tmp="$(mktemp -d)"
  mkdir -p "$(dirname "$dest")"
  unzip -d "$tmp" "$src"
  mv "$tmp/basex/" "$dest"
  rm -r "$tmp"
fi

printf '.PHONY: all install clean
VERSION = %s
PREFIX ?= /usr/local

bin = bin/basex bin/basexclient bin/basexgui bin/basexhttp bin/basexhttpstop bin/basexserver bin/basexserverstop
wrapped_noprefix = libexec/basex-$(VERSION)/basex
wrapped = $(PREFIX)/$(wrapped_noprefix)

all: $(bin) $(wrapped_noprefix)
clean:
	rm -rf bin libexec
install: $(bin) $(wrapped_noprefix)
	mkdir -p $(PREFIX)/bin $(PREFIX)/lib/basex-$(VERSION) $(PREFIX)/libexec/basex-$(VERSION)
	install bin/* $(PREFIX)/bin/
	install libexec/basex-$(VERSION)/* $(PREFIX)/libexec/basex-$(VERSION)/
	rm -Rf $(PREFIX)/lib/basex-$(VERSION)
	cp -R lib/basex-$(VERSION) $(PREFIX)/lib/basex-$(VERSION)

$(wrapped_noprefix):
	mkdir -p "$$(dirname "$@")"
	sed s@.PREFIX@$(PREFIX)@ basex.template > $@
bin:
	mkdir -p $@
bin/basex: bin
	echo $(wrapped) org.basex.BaseX "$$@" > $@
bin/basexclient: bin
	echo $(wrapped) org.basex.BaseXClient "$$@" > $@
bin/basexgui: bin
	echo $(wrapped) org.basex.BaseXGUI "$$@" > $@
bin/basexhttp: bin
	echo $(wrapped) org.basex.BaseXHTTP "$$@" > $@
bin/basexhttpstop: bin
	echo $(wrapped) org.basex.BaseXHTTP "$$@" stop > $@
bin/basexserver: bin
	echo $(wrapped) org.basex.BaseXServer "$$@" > $@
bin/basexserverstop: bin
	echo $(wrapped) org.basex.BaseXServer "$$@" stop > $@
' "${VERSION}" > "$dist/Makefile"

mkdir -p "$dist/libexec/basex-${VERSION}"
printf '#!/bin/sh
MAIN=$PREFIX/lib/basex-%s
CP="$MAIN/BaseX.jar:$MAIN/lib/custom/*:$MAIN/lib/*:$CLASSPATH"
BASEX_JVM="-Xmx2g $BASEX_JVM"
exec java -cp "$CP" $BASEX_JVM "$@"
' "${VERSION}" > "$dist/basex.template"

echo 'WELCOME! ---------------------------------------------------------------

BaseX is a light-weight, high-performance and scalable XML Database
engine and XPath/XQuery 3.1 Processor, which includes full support for
the W3C Update and Full Text extensions. An interactive and user-
friendly GUI frontend gives you great insight into your XML documents.

INSTALL ----------------------------------------------------------------

Build and install like this.

  make
  make install

Some configuration possibilities are documented below.

The above will install to the prefix /usr/local. You can set
a different prefix. For example

  make clean
  make PREFIX=/usr
  make PREFIX=/usr install

Since the java executable is sometimes installed in an unconventional
directory, you might want to set the path to the executable rather than
relying on it being in the PATH. 

  sed -i s@java@/usr/local/jdk-11/bin/java@ basex.template
  make clean install

HELP -------------------------------------------------------------------

 Please find more help in our online documentation:

   http://docs.basex.org

 BaseX is an Open Source project and BSD-licensed. You are welcome
 to join our basex-talk mailing list and give all kinds of feedback:

   http://basex.org/open-source

 You may as fork our BaseX repository and send pull requests:

   https://github.com/BaseXdb/BaseX/issues

 Have fun,
 Your BaseX Team

------------------------------------------------------------------------
' > "$dist/README"

cd dist
tar czf "basex-$VERSION.tar.gz" "basex-$VERSION"
