#!/bin/sh -e
#
# 2019 Timothée Floure (timothee.floure@ungleich.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#

# Function to compare version strings. Returns success (0) if the version
# given by stdin is higher than the version provided by the argument.
#
# Taken from the cdist core type __sensible_editor.
version_ge() {
	awk -F '[^0-9.]' -v target="${1:?}" '
	function max(x, y) { return x > y ? x : y; }
	BEGIN {
		getline;
		nx = split($1, x, ".");
		ny = split(target, y, ".");
		for (i = 1; i <= max(nx, ny); ++i) {
			diff = int(x[i]) - int(y[i]);
			if (diff < 0) exit 1;
			else if (diff > 0) exit 0;
			else continue;
		}
	}'
}


VERSION=$(cat "$__object/parameter/version")
INSTALL_DIR=$(cat "$__object/parameter/install_dir")
OWNER=$(cat "$__object/parameter/owner")

# tarball name changed due to application renaming
if echo "$VERSION" | version_ge 1.7.14; then
    src="element-v$VERSION"
else
    src="riot-v$VERSION"
fi
archive="$src.tar.gz"
url="https://github.com/vector-im/riot-web/releases/download/v$VERSION/$archive"

# tar and curl are installed by the __matrix-riot manifest. mktemp is usually
# provided by coreutils and assumed installed.
cat << EOF
set -e

# Ensure that coreutils is installed.
if [ ! -x \$(which mktemp) ]; then
  echo "mktemp is not available on the remote host." >&2
  exit 1
fi

# Create temporary working directory.
tmpdir=\$(mktemp -d)
custom_files_dir="\$tmpdir/custom_files"
cd \$tmpdir

# Download and extract sources.
curl -L '$url' > $archive
tar xf $archive

# Backup files deployed by __matrix_element.
mkdir -p \$custom_files_dir
for file in $INSTALL_DIR/cdist/*; do
  cp "\$file" "\$custom_files_dir"
done

# Deploy sources and restore configuration.
rm -r '$INSTALL_DIR'
mv '$src' '$INSTALL_DIR'

for file in \$custom_files_dir/*; do
    cp "\$file" '$INSTALL_DIR'
done

# Chown deployed files to requested owner.
chown -R '$OWNER' '$INSTALL_DIR'

# Remove temporary working directory.
cd /
rm -r \$tmpdir
EOF
