#!/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 )" dest=/opt/kjv-api echo "Attempting to install dependancies" apt install python3-flask sqlite3 libhunspell-dev python3-hunspell uwsgi-core uwsgi-plugin-python3 python3-requests python3-dotenv curl rsync if id kjv >/dev/null 2>&1; then echo "User kjv already exists, leaving it alone" else echo "Creating new user kjv with no login shell" useradd -r -s /bin/false kjv fi # Stop a running install so files are not swapped underneath it if systemctl is-active --quiet kjv-api.service; then echo "Stopping running kjv-api service for the update" systemctl stop kjv-api.service fi mkdir -p "$dest" # Never overwrite local state on a re-run: the .env, the runtime databases # (seq/shortcodes/sermons, including any -wal/-shm files), logs, and dev cruft. # Files are only added or updated, never deleted. if [[ "$script_dir" == "$dest" ]]; then echo "Running from $dest, nothing to copy" else echo "Copying server files to $dest (existing .env, databases and logs are kept)" rsync -rlt \ --exclude='.git/' --exclude='__pycache__/' --exclude='*.pyc' \ --exclude='/.env' \ --exclude='/data/seq.db*' --exclude='/data/shortcodes.db*' --exclude='/data/sermons.db*' \ --exclude='/logs/*' \ "${script_dir}"/ "$dest"/ fi mkdir -p "$dest/logs" "$dest/data" if [[ ! -f "$dest/.env" ]]; then echo "Creating $dest/.env from .env.example (edit it to set real values)" cp "$dest/.env.example" "$dest/.env" else echo "Keeping existing $dest/.env" fi # Admins manage the install through the kjv group (the service's own group). admin_user="${KJV_ADMIN_USER:-$SUDO_USER}" if [[ -n "$admin_user" && "$admin_user" != root ]] && id "$admin_user" >/dev/null 2>&1; then echo "Adding $admin_user to group kjv (log out and back in to apply)" usermod -aG kjv "$admin_user" else echo "No admin user detected; set KJV_ADMIN_USER=name to add one to the kjv group" fi echo "Setting permissions (code group-writable by kjv; .env, logs and data limited)" chown -R root:kjv "$dest" find "$dest" -type d -exec chmod 2775 {} + find "$dest" -type f -exec chmod 664 {} + chmod 775 "$dest"/*.sh chmod 640 "$dest/.env" # SQLite needs a writable directory for journal files chown kjv:kjv "$dest/logs" "$dest/data" chmod 750 "$dest/logs" "$dest/data" chown -R kjv:kjv "$dest/logs" chmod -R u+rwX,g+rX,o-rwx "$dest/logs" # Runtime-written databases belong to kjv; shipped read-only databases stay root-owned for db in "$dest"/data/seq.db* "$dest"/data/shortcodes.db*; do if [[ -f "$db" ]]; then chown kjv:kjv "$db" chmod 640 "$db" fi done echo "Installing systemd service task" unit=/etc/systemd/system/kjv-api.service if [[ -f "$unit" ]] && ! cmp -s "$dest/install/kjv-api.service" "$unit"; then echo "Existing service file differs, saving it as $unit.bak" cp "$unit" "$unit.bak" fi cp "$dest/install/kjv-api.service" "$unit" systemctl daemon-reload echo "Enabling auto-start and starting kjv-api" systemctl enable kjv-api.service systemctl restart kjv-api.service echo "Waiting for the service to initialize" sleep 3 echo "If you see a random Bible verse below in json format, your installation was successful:" echo echo curl -XGET 127.0.0.1:1611/random echo echo YEL='\033[0;33m' NC='\033[0m' # No Color echo -e "${YEL}You must now set your webserver as a reverse proxy for your domain and point it to 127.0.0.1:1611 ${NC}" 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 /opt/kjv-api/html/. There's no fancy libraries." echo " - Logs are located in two places (application logs and uwsgi logs)." echo " - /opt/kjv-api/logs/" echo " - journalctl -e -u kjv-api.service" echo " - If this install failed (you didn't get a Bible verse above) - try running /opt/kjv-api/kjv-api-test.sh" echo " after stopping the kjv-api.service with systemctl, it should give you a traceback in your console."