#!/bin/bash

# get wekan/mongo settings
source $SNAP/bin/wekan-read-settings

if [ "true" == "${DISABLE_MONGODB}" ]; then
    echo "mongodb is disabled. Stop service"
    snapctl stop --disable ${SNAP_NAME}.mongodb
    exit 0
fi

# make sure we have set minimum env variables for locale
if [ -z "$LANG" ]; then
    export LANG=en_US.UTF-8
fi

export LC_ALL=C
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu

# When starting MongoDB, if logfile exist, delete it, because now uses syslog instead of logfile,
# because syslog usually already has log rotation.
# https://github.com/wekan/wekan-snap/issues/92
if test -f "$SNAP_COMMON/mongodb.log"; then
   rm -f "$SNAP_COMMON/mongodb.log"
fi

# Alternative: When starting MongoDB, and using logfile, truncate log to last 1000 lines of text.
# 1) If file exists:
#if test -f "$SNAP_COMMON/mongodb.log"; then
#    # 2) Copy last 1000 lines to variable loglast1000lines.
#    loglast1000lines=$(tail -1000 "$SNAP_COMMON/mongodb.log")
#    # 3) Copy variable to replace original MongoDB log.
#    echo "$loglast1000lines" > "$SNAP_COMMON/mongodb.log"
#    # 4) Set variable to be empty.
#    loglast1000lines=""
#fi

if [ -z "$MONGO_URL" ]; then

    # start mongo deamon
    BIND_OPTIONS=""
    if [ "nill" != "$MONGODB_BIND_UNIX_SOCKET" ] && [ "x" != "x${MONGODB_BIND_UNIX_SOCKET}" ]; then
        BIND_OPTIONS+=" --unixSocketPrefix  $MONGODB_BIND_UNIX_SOCKET"
    fi
    if [ "x" != "x${MONGODB_BIND_IP}" ]; then
        BIND_OPTIONS+=" --bind_ip $MONGODB_BIND_IP"
    fi
    if [ "x" != "x${MONGODB_PORT}" ]; then
        BIND_OPTIONS+=" --port $MONGODB_PORT"
    fi
    echo "mongodb bind options: $BIND_OPTIONS"

    if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then
        echo "Sending mongodb logs to syslog"
        mongod --dbpath $SNAP_COMMON --syslog --journal $BIND_OPTIONS --quiet
        exit 0
    fi

    if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then
        echo "Sending mongodb logs to $SNAP_COMMON"
        mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/mongodb.log --logappend --journal $BIND_OPTIONS --quiet
    fi

    if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then
        echo "Sending mongodb logs to /dev/null"
        mongod --dbpath $SNAP_COMMON --logpath /dev/null --journal $BIND_OPTIONS --quiet
    fi

    # Drop indexes on database upgrade, when starting MongoDB
    #mongo wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
    mongo wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' $BIND_OPTIONS
    # Delete incomplete uploads so that they would not prevent starting WeKan
    mongo wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' $BIND_OPTIONS

else

    if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then
        echo "Sending mongodb logs to syslog"
        mongod --dbpath $SNAP_COMMON --syslog --journal $MONGO_URL --quiet
    fi

    if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then
        echo "Sending mongodb logs to $SNAP_COMMON"
        mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/mongodb.log --logappend --journal $MONGO_URL --quiet
    fi

    if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then
        echo "Sending mongodb logs to /dev/null"
        mongod --dbpath $SNAP_COMMON --logpath /dev/null --journal $MONGO_URL --quiet
    fi

    # Drop indexes on database upgrade, when starting MongoDB
    #mongo wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
    mongo wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.2" });' $BIND_OPTIONS
   # Delete incomplete uploads so that they would not prevent starting WeKan
    mongo wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' $BIND_OPTIONS

fi
