# Install dependencies as root:
if [[ "$USER" == "root" ]]; then
	RELPIPE_DEPENDENCIES=(g++ make cmake mercurial pkg-config)
	RELPIPE_DEPENDENCIES+=(libxerces-c-dev)                                     # needed only for relpipe-in-xml module
	RELPIPE_DEPENDENCIES+=(guile-2.2-dev)                                       # needed only for relpipe-tr-scheme module
	RELPIPE_DEPENDENCIES+=(gawk)                                                # needed only for relpipe-tr-awk module
	RELPIPE_DEPENDENCIES+=(libxml++2.6-dev)                                     # needed only for relpipe-in-xmltable module
	RELPIPE_DEPENDENCIES+=(unixodbc-dev)                                        # needed only for relpipe-tr-sql module
	RELPIPE_DEPENDENCIES+=(libsqliteodbc)                                       # needed only for relpipe-tr-sql module if used with SQLite
	RELPIPE_DEPENDENCIES+=(odbc-postgresql)                                     # needed only for relpipe-tr-sql module if used with PostgreSQL
	RELPIPE_DEPENDENCIES+=(libjack-jackd2-dev)                                  # needed only for relpipe-in-jack and relpipe-out-jack modules; Jack1 should also work
	RELPIPE_DEPENDENCIES+=(libxi-dev libxtst-dev)                               # needed only for relpipe-in-x11 and relpipe-out-x11
	RELPIPE_DEPENDENCIES+=(libzbar-dev libmagick++-dev)                         # needed only for relpipe-in-barcode and the barcode-reader streamlet
	RELPIPE_DEPENDENCIES+=(libgumbo-dev)                                        # needed only for relpipe-in-htmltable
	RELPIPE_DEPENDENCIES+=(libyaml-dev)                                         # needed only for relpipe-in-yaml, relpipe-in-yamltable, relpipe-in-json and relpipe-in-jsontable
	RELPIPE_DEPENDENCIES+=(libvmime-dev)                                        # needed only for relpipe-in-mimetable
	apt install "${RELPIPE_DEPENDENCIES[@]}"
	exit;
fi

# Run rest of installation as a non-root user:
export RELPIPE_VERSION="v0.18"
export RELPIPE_VERSION="tip" # FIXME: v0.18
export RELPIPE_SRC=~/src
export RELPIPE_BUILD=~/build
export RELPIPE_INSTALL=~/install
export PKG_CONFIG_PATH="$RELPIPE_INSTALL/lib/pkgconfig/:$PKG_CONFIG_PATH"
export PATH="$RELPIPE_INSTALL/bin:$PATH"

rm -rf "$RELPIPE_BUILD"/relpipe-*
mkdir -p "$RELPIPE_SRC" "$RELPIPE_BUILD" "$RELPIPE_INSTALL"

# Helper functions:
relpipe_download() { for m in "$@"; do cd "$RELPIPE_SRC" && ([[ -d "relpipe-$m.cpp" ]] && hg pull -R "relpipe-$m.cpp" && hg update -R "relpipe-$m.cpp" "$RELPIPE_VERSION" || hg clone -u "$RELPIPE_VERSION" https://hg.globalcode.info/relpipe/relpipe-$m.cpp) || break; done; }
relpipe_install()  { for m in "$@"; do cd "$RELPIPE_BUILD" && mkdir -p relpipe-$m.cpp && cd relpipe-$m.cpp && cmake -DCMAKE_INSTALL_PREFIX:PATH="$RELPIPE_INSTALL" "$RELPIPE_SRC/relpipe-$m.cpp" && make && make install || break; done; }

RELPIPE_LIBS=(lib-common lib-reader lib-writer lib-cli lib-xmlwriter)
RELPIPE_TOOLS=(in-cli in-fstab in-xml in-xmltable in-csv in-filesystem in-recfile out-nullbyte out-ods out-tabular out-xml out-csv out-asn1 out-recfile tr-cut tr-grep tr-sed tr-validator tr-scheme tr-awk tr-sql in-jack out-jack)
RELPIPE_TOOLS+=(in-asn1table in-barcode in-htmltable in-ini in-initable in-mimetable in-x11 in-yaml in-yamltable out-ini out-x11 out-yaml tr-xpath) # since v0.18
# RELPIPE_TOOLS+=(out-gui.qt tr-python) # not enabled by default


# Download all sources:
relpipe_download "${RELPIPE_LIBS[@]}" "${RELPIPE_TOOLS[@]}"

# Optional: At this point, we have all dependencies and sources downloaded, so we can disconnect this computer from the internet in order to verify that our build process is sane, deterministic and does not depend on any external resources.

# Build and install libraries:
relpipe_install "${RELPIPE_LIBS[@]}"

# Build and install tools:
relpipe_install "${RELPIPE_TOOLS[@]}"

# Load Bash completion scripts:
for c in "$RELPIPE_SRC"/relpipe-*/bash-completion.sh ; do . "$c"; done

# Enable streamlet examples:
export RELPIPE_IN_FILESYSTEM_STREAMLET_PATH="$RELPIPE_SRC"/relpipe-in-filesystem.cpp/streamlet-examples/

# Clean-up:
unset -f relpipe_install
unset -f relpipe_download
unset -v RELPIPE_VERSION
unset -v RELPIPE_SRC
unset -v RELPIPE_BUILD
unset -v RELPIPE_INSTALL

# List available JACK ports (the JACK daemon should be running):
relpipe-in-jack --list-ports true --list-midi-messages false | relpipe-out-tabular
