#!/usr/bin/with-contenv bashio

# Make sure there is an Nextcloud installation
if ! [ "$(occ -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/                                    #
#################################################################################

#######################
# Download the script #
#######################
#SCRIPT="/sbin/fetch_lib"
#wget -q -O $SCRIPT https://raw.githubusercontent.com/nextcloud/vm/master/lib.sh
#source $SCRIPT

######################
# Correct the script #
######################
#sed -i "s|SCRIPTS=/var/scripts|SCRIPTS=/sbin|g" $SCRIPT
#sed -i "s|NCPATH=/var/www/nextcloud|NCPATH=/data/config/www/nextcloud|g" $SCRIPT
#sed -i "s|BACKUP=/mnt/NCBACKUP|BACKUP=/data/NCBACKUP|g" $SCRIPT

# Test RAM size (2GB min) + CPUs (min 2)
#ram_check 3 FullTextSearch
#cpu_check 2 FullTextSearch

# Get all needed variables from the library
#ncdb
#nc_update
#es_install

# Check for errors + debug code and abort if something isn't right
# 1 = ON
# 0 = OFF
#DEBUG=0
#debug_mode

# Must be root
#root_check

# Nextcloud 18 is required.
#lowest_compatible_nc 18

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 $(occ app:getpath $app) ] && occ app:remove $app
    done

    # Get Full Text Search app for nextcloud
    for app in "${APPS[@]}"; do
        echo "... installing apps : $app"
        occ app:install $app >/dev/null
        occ 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_ip%/*}
    occ fulltextsearch_elasticsearch:configure "{\"elastic_host\":\"http://$HOST:9200\"}"
    occ fulltextsearch_elasticsearch:configure "{\"elastic_index\":\"my_index\"}"
    occ fulltextsearch_elasticsearch:configure "{\"analyzer_tokenizer\":\"standard\"}"

    # Is server detected
    if [ curl $HOST:9100 ] &>/dev/null; then
        # Wait further for cache for index to work
        countdown "Waiting for a few seconds before indexing starts..." "10"
        if occ 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
