#!/bin/bash

set -e

if [ "$UID" != 0 ]; then
    echo "Error: must run this script as root (e.g. with sudo)" >&2
    exit 1
fi

if [ "$#" != 1 ] || [[ "$1" == -* ]]; then
    echo "Error: usage: $0 https://url/for/mainnet/data.mdb"
    exit 1
fi

if ! [ -e /var/lib/oxen ]; then
    echo "/var/lib/oxen does not exist; did you properly install the oxend package?"
    exit 1
fi

if ! [ -e /var/lib/oxen/lmdb ]; then
    mkdir /var/lib/oxen/lmdb
    chown _loki:_loki /var/lib/oxen/lmdb
fi

node_active=
ss_active=
if systemctl -q is-active oxen-node.service; then node_active=y; fi
if systemctl -q is-active oxen-storage-server.service; then ss_active=y; fi

url_lmdb="$1"
url_ons="${url_lmdb%/*}/ons.db"
url_sqlite="${url_lmdb%/*}/sqlite.db"
echo "Ready to download blockchain data from $1, …/ons.db, …/sqlite.db"
if [ -e /var/lib/oxen/lmdb/data.mdb ]; then
    echo -e "\nWill overwrite existing blockchain files:\n"
    ls -l --si /var/lib/oxen/lmdb/data.mdb
    for f in /var/lib/oxen/{ons,sqlite}.db; do
        if [ -e "$f" ]; then
            ls -l --si "$f"
        fi
    done
    echo ""
fi
if [ -n "$node_active" ]; then
    echo -n "oxen-node.service"
    if [ -n "$ss_active" ]; then echo -n " and oxen-storage-server.service"; fi
    echo " will be stopped, then restarted when the lmdb download is complete"
else
    echo "oxen-node.service is not currently running and will be left stopped.  (Do not attempt to start it or the storage server until the download is complete!)"
fi

echo

read -p "Press enter to continue, Ctrl-C to abort"

if [ -n "$node_active" ]; then
    if [ -n "$ss_active" ]; then
        systemctl stop oxen-node.service oxen-storage-server.service
    else
        systemctl stop oxen-node.service
    fi
fi

curl "$url_lmdb" >/var/lib/oxen/lmdb/data.mdb
chown _loki:_loki /var/lib/oxen/lmdb/data.mdb

downloads=(/var/lib/oxen/lmdb/data.mdb)


if [ -e /var/lib/oxen/ons.db ]; then
    rm -f /var/lib/oxen/ons.db*
fi
if curl "$url_ons" >/var/lib/oxen/ons.db; then
    chown _loki:_loki /var/lib/oxen/ons.db
    downloads+=(/var/lib/oxen/ons.db)
else
    echo "Failed to download ons.db; perhaps it is not available alongside data.mdb at the given URL?" >&2
    echo "oxend startup will have to rescan the ONS database which will take a few minutes" >&2
fi

if [ -e /var/lib/oxen/sqlite.db ]; then
    rm -f /var/lib/oxen/sqlite.db*
fi
if curl "$url_sqlite" >/var/lib/oxen/sqlite.db; then
    chown _loki:_loki /var/lib/oxen/sqlite.db
    downloads+=(/var/lib/oxen/sqlite.db)
else
    echo "Failed to download sqlite.db; perhaps it is not available alongside data.mdb at the given URL?" >&2
    echo "oxend startup will have to recompute batching rewards which will take a few minutes" >&2
fi

echo "Downloaded blockchain files:"
ls -l --si "${downloads[@]}"

if [ -n "$node_active" ]; then
    if [ -n "$ss_active" ]; then
        echo "Starting oxen-node.service, oxen-storage-server.service"
        systemctl start oxen-node.service oxen-storage-server.service
    else
        echo "Starting oxen-node.service"
        systemctl start oxen-node.service
    fi
fi
