#!/bin/sh -e
# __nextcloud/manifest


# Version compare function original from __sensible_editor
#
# Arguments:
#  1: version of which $2 should be checked against
#  2: version which should be bigger than or equal with $1
#
# Return code:
#  0: $1 is bigger than $2
#  1-n: $1 is smaller than or equal $2
version_ge() {
    printf "%s" "$1" | awk -F '[^0-9.]' -v target="$2" '
    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) continue
            exit (diff < 0)
        }
        exit 1
    }'; return $?
}


# Check support status
os="$(cat "$__global/explorer/os")"

case "$os" in
    debian|ubuntu)
        # PHP main
        __package php-cli
        # to unpack the package
        __package bzip2
        # install misc packages for nextcloud
        __package ffmpeg

        # PHP modules
        for package in php-gd php-json php-mysql php-curl php-mbstring php-intl \
                        php-imagick php-xml php-zip php-bz2 php-bcmath php-gmp
        do
            require="__package/php-cli" __package $package
        done

        # check support database additions (but don't remove junk of old ones)
        case "$(cat "$__object/parameter/database-type")" in
            sqlite|sqlite3)
                __package php-sqlite3
                ;;
            mysql|mariadb)
                __package php-mysql
                ;;
            pgsql|postgres|postgresql)
                __package php-pgsql
                ;;
        esac
        ;;

    # unkown distro - what to install?
    *)
        printf "unkown %s, don't know what to install ..\n" "$os" >&2
        echo "checkout the __nextcloud/manifest to contribute a working package list" >&2
        exit 1
        ;;
esac


# Get the user and group
mode="$(cat "$__object/parameter/mode")"
user="$(cat "$__object/parameter/user")"
group="$(cat "$__object/parameter/group")"

# Get the installation directory
installdir="/$__object_id"

# Set permissions after the nextcloud installation/upgrade is done
# FIXME maybe less strict if some parameter is not given by the user?
#       permissions also partily set via the gencode-remote
require="__nextcloud/$__object_id" __directory "$installdir" \
    --mode "$mode" --owner "$user" --group "$group"


# Get version information
version_is="$( cat "$__object/explorer/version" )"
version_should="$( cat "$__object/parameter/version" )"
# The version URI
nextcloud_uri="https://download.nextcloud.com/server/releases/nextcloud-${version_should}.tar.bz2"
nextcloud_sum="${nextcloud_uri}.sha256"


# Check if there is a current installation. It depends where the upstream
# tarball should be unpacked (directly or moved in a later stage).
if [ "$version_is" ]; then
    # Only set and check the version if a upgrade is allowed.
    #  if this block will be skipped, no upgrade will be done
    if ! [ -f "$__object/parameter/install-only" ]; then
        # Block downgrades as there are may caused from the automatic upgrader
        # if the current version is higher than the version that should be installed
        if version_ge "$version_is" "$version_should"; then
            # it's an error if the current version is higher than the one that should be installed
            printf "The current nextcloud version '%s' is higher than the version that should be installed (%s)\n" \
               "$version_is" "$version_should" >&2
            printf "Please bump the nextcloud version to '%s' or higher!\n" "$version_is" >&2
            exit 2
        fi

        # Set destination to a temporary directory
        destination="$(dirname "$installdir")/.$(basename "$installdir")"
    fi
else
    # Set destination to the real destination
    destination="$installdir"
fi

# Install/Upgrade the nextcloud version if there is a destination set.
#  it checks if it is necessary and intended to upgrade
if [ "$destination" ] && [ "$version_is" != "$version_should" ]; then
    # download it to the tmp directory
    # FIXME --sum is currently rather a hack, see cdist#844
    __download "/tmp/nextcloud-${version_should}.tar.bz2" \
       --url "$nextcloud_uri" \
       --sum "sha256:$(curl -sS -L "$nextcloud_sum" | awk '{print $1}')"

    # after this, unpack it from /tmp to $destination
    require="__download/tmp/nextcloud-${version_should}.tar.bz2" \
        __unpack "/tmp/nextcloud-${version_should}.tar.bz2" \
            --tar-strip 1 \
            --destination "$destination"
fi
