#!/usr/bin/with-contenv bashio

LAUNCHER="sudo -u abc php /data/config/www/nextcloud/occ" || bashio::log.info "/data/config/www/nextcloud/occ not found"
    if ! bashio::fs.file_exists '/data/config/www/nextcloud/occ'; then
        LAUNCHER=$(find / -name "occ" -print -quit)
    fi || bashio::log.info "occ not found"

# Make sure there is an Nextcloud installation
if ! [ "$($LAUNCHER -V)" ]; then
    bashio::log.warning "It seems there is no Nextcloud server installed. Please restart the addon after initialization of the user."
    exit 0
fi

############
# BASED ON #
#################################################################################
# https://raw.githubusercontent.com/nextcloud/vm/master/apps/fulltextsearch.sh  #
# T&M Hansson IT AB © - 2021, https://www.hanssonit.se/                         #
# SwITNet Ltd © - 2021, https://switnet.net/                                    #
#################################################################################

if bashio::config.true 'Full_Text_Search' && [ occ fulltextsearch:index ] &>/dev/null; then
    echo "Full Text Search is already working"
else
    echo "Installing Full Text Search"
    # Reset Full Text Search to be able to index again, and also remove the app to be able to install it again
    occ fulltextsearch:reset &>/dev/null || true
    APPS=(fulltextsearch fulltextsearch_elasticsearch files_fulltextsearch)
    for app in "${APPS[@]}"; do
        # If app exists, remove it
        [ ! -z $($LAUNCHER app:getpath $app) ] && $LAUNCHER app:remove $app &>/dev/null
    done

    # Get Full Text Search app for nextcloud
    for app in "${APPS[@]}"; do
        echo "... installing apps : $app"
        $LAUNCHER app:install $app >/dev/null
        $LAUNCHER app:enable $app >/dev/null
    done
    chown -R abc:abc $NEXTCLOUD_PATH/apps

    # Final setup
    echo "... settings apps"
    #occ fulltextsearch:configure '{"search_platform":"ElasticSearchPlatform"}'
    HOST=$(bashio::network.ipv4_address)
    HOST=${HOST%/*}
    $LAUNCHER fulltextsearch_elasticsearch:configure "{\"elastic_host\":\"http://$HOST:9200\"}"
    $LAUNCHER fulltextsearch_elasticsearch:configure "{\"elastic_index\":\"my_index\"}"
    $LAUNCHER fulltextsearch_elasticsearch:configure "{\"analyzer_tokenizer\":\"standard\"}"

    # Is server detected
    if [ curl $HOST:9200 ] &>/dev/null; then
        # Wait further for cache for index to work
        countdown "Waiting for a few seconds before indexing starts..." "10"
        if $LAUNCHER fulltextsearch:index </dev/null; then
            bashio::log.info "Full Text Search was successfully installed!"
        fi

    else
        bashio::log.warning "Elasticsearch can't connect. Please manually define its server in the options"
    fi

fi
