#!/bin/bash

# Check for root privileges
if [[ $EUID -ne 0 ]]; then
  echo "This install script requires root privileges. Please run with sudo."
  exit 1
fi

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

echo "Attempting to install dependancies"
apt install python3-flask sqlite3 libhunspell-dev python3-hunspell uwsgi-core uwsgi-plugin-python3 python3-requests curl

echo "Creating new user kjv with no login shell"
useradd -r -s /bin/false kjv

echo "Copying server files to /opt/kjv-api"
mkdir /opt/kjv-api
cp -r "${script_dir}"/. /opt/kjv-api


echo "Setting ownership to kjv user"
chmod -R kjv:kjv /opt/kjv-api

echo "Installing systemd service task"
cp /opt/kjv-api/install/kjv-api.service /etc/systemd/system/kjv-api.service

echo "Enabling auto-start and starting kjv-api"
systemctl enable kjv-api.service
systemctl start kjv-api.service


echo "If you see a random Bible verse below in json format, your installation was successful:"
curl -XGET 127.0.0.1:1611/random

echo "You must now set your webserver as a reverse proxy for your domain and point it to 127.0.0.1:1611"
echo "See the `install` folder for example configuration files."
echo
echo "Notes:"
echo " - Thank you for running this software to bring the Bible to people."
echo " - You can change the pages (it's not fancy) in html/. There's no fancy libraries."
echo
