- Rename Go module from github.com/cclohmar/ReceiptNext to NextExpense - Update all import paths across 7 Go source files - Update templates (titles, headings, branding) - Update static files (manifest.json, sw.js, CSS) - Update config (Makefile, install.sh, .env.example) - Update README with new name and URLs - Rename service file receiptnext.service -> nextexpense.service - Update install paths, service names, log paths in install.sh
444 lines
17 KiB
Bash
Executable file
444 lines
17 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# NextExpense Installer
|
|
# =====================
|
|
#
|
|
# Usage:
|
|
# curl -fsSL https://git.lohmar.co.uk/cclohmar/NextExpense/raw/branch/main/install.sh | bash
|
|
# bash install.sh — Full install
|
|
# bash install.sh -update — Pull latest, rebuild, restart
|
|
#
|
|
# Installs to /opt/nextexpense/ and sets up a systemd service.
|
|
# Uses sudo internally only for operations that require it.
|
|
#
|
|
|
|
set -euo pipefail
|
|
|
|
INSTALL_DIR="/opt/nextexpense"
|
|
SERVICE_NAME="nextexpense"
|
|
REPO_URL="https://git.lohmar.co.uk/cclohmar/NextExpense.git"
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
CYAN='\033[0;36m'
|
|
NC='\033[0m'
|
|
|
|
info() { echo -e "${GREEN}[✓]${NC} $1"; }
|
|
warn() { echo -e "${YELLOW}[!]${NC} $1"; }
|
|
err() { echo -e "${RED}[✗]${NC} $1"; }
|
|
ask() { echo -en "${CYAN}→${NC} $1"; }
|
|
|
|
# Helper: run a command with sudo if not already root
|
|
sudo_if() {
|
|
if [ "$EUID" -eq 0 ]; then
|
|
"$@"
|
|
else
|
|
sudo "$@"
|
|
fi
|
|
}
|
|
|
|
# ──────────────────────────────────────────────────────────────────
|
|
# UPDATE MODE
|
|
# ──────────────────────────────────────────────────────────────────
|
|
if [ "${1:-}" = "-update" ]; then
|
|
echo ""
|
|
echo " ╔═══════════════════════════════════════╗"
|
|
echo " ║ NextExpense — Update Mode ║"
|
|
echo " ╚═══════════════════════════════════════╝"
|
|
echo ""
|
|
|
|
# Cache sudo credentials upfront so subsequent sudo_if calls don't prompt.
|
|
if [ "$EUID" -ne 0 ]; then
|
|
sudo -v 2>/dev/null || { err "This update requires sudo access. Please run: sudo ./install.sh -update"; exit 1; }
|
|
fi
|
|
|
|
if [ ! -d "$INSTALL_DIR" ]; then
|
|
err "$INSTALL_DIR does not exist. Run install.sh without -update first."
|
|
exit 1
|
|
fi
|
|
|
|
cd "$INSTALL_DIR"
|
|
|
|
if [ -d .git ]; then
|
|
info "Pulling latest code..."
|
|
sudo_if git pull
|
|
else
|
|
warn "Not a git repository — re-cloning..."
|
|
cd /tmp
|
|
rm -rf nextexpense-update
|
|
sudo_if git clone "$REPO_URL" nextexpense-update
|
|
sudo_if rsync -a --delete nextexpense-update/ "$INSTALL_DIR/"
|
|
rm -rf nextexpense-update
|
|
cd "$INSTALL_DIR"
|
|
fi
|
|
|
|
# Determine the app user from the existing service file (most reliable).
|
|
APP_USER=""
|
|
if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]; then
|
|
APP_USER=$(grep -Po '^User=\K.*' "/etc/systemd/system/${SERVICE_NAME}.service" 2>/dev/null || true)
|
|
fi
|
|
# Fallback: check the owner of the install directory or binary.
|
|
if [ -z "$APP_USER" ]; then
|
|
APP_USER=$(stat -c '%U' "$INSTALL_DIR/app" 2>/dev/null || stat -c '%U' "$INSTALL_DIR" 2>/dev/null || true)
|
|
fi
|
|
if [ -z "$APP_USER" ] || [ "$APP_USER" = "root" ]; then
|
|
APP_USER="${SUDO_USER:-$(whoami)}"
|
|
fi
|
|
# Reset ownership so the app user can write files.
|
|
sudo_if chown -R "${APP_USER}:${APP_USER}" "$INSTALL_DIR" 2>/dev/null || true
|
|
# Ensure key directories have correct permissions.
|
|
sudo_if chmod 755 "$INSTALL_DIR" "$INSTALL_DIR/templates" "$INSTALL_DIR/static" 2>/dev/null || true
|
|
sudo_if chmod 775 "$INSTALL_DIR/storage" 2>/dev/null || true
|
|
|
|
# Backup the database before making any changes.
|
|
if [ -f "$INSTALL_DIR/expenses.db" ]; then
|
|
BACKUP_DIR="$INSTALL_DIR/backups"
|
|
sudo_if mkdir -p "$BACKUP_DIR"
|
|
BACKUP_FILE="$BACKUP_DIR/expenses-$(date +%Y%m%d-%H%M%S).db"
|
|
sudo_if cp "$INSTALL_DIR/expenses.db" "$BACKUP_FILE"
|
|
# Restore file ownership so the app user can still write.
|
|
sudo_if chown $(stat -c "%U:%G" "$INSTALL_DIR/expenses.db") "$BACKUP_FILE" 2>/dev/null || true
|
|
info "Database backed up to $BACKUP_FILE"
|
|
fi
|
|
|
|
# Copy updated templates, static assets, and config files.
|
|
info "Updating templates and static assets..."
|
|
sudo_if rsync -a --delete templates/ "$INSTALL_DIR/templates/" 2>/dev/null || true
|
|
sudo_if rsync -a --delete static/ "$INSTALL_DIR/static/" 2>/dev/null || true
|
|
sudo_if cp install.sh "$INSTALL_DIR/" 2>/dev/null || true
|
|
|
|
# Stop service before rebuilding so the old binary can be removed.
|
|
info "Stopping service..."
|
|
sudo_if systemctl stop "$SERVICE_NAME" 2>/dev/null || true
|
|
|
|
# Ensure Go is installed for building from source.
|
|
if ! command -v go &>/dev/null; then
|
|
info "Installing Go..."
|
|
if command -v apt-get &>/dev/null; then
|
|
sudo_if apt-get update -qq && sudo_if apt-get install -y -qq golang-go 2>&1 | tail -1
|
|
elif command -v dnf &>/dev/null; then
|
|
sudo_if dnf install -y golang 2>&1 | tail -1
|
|
elif command -v apk &>/dev/null; then
|
|
sudo_if apk add go 2>&1 | tail -1
|
|
else
|
|
err "No package manager found. Please install Go 1.23+ manually."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
info "Rebuilding binary..."
|
|
export GOMODCACHE="${INSTALL_DIR}/.go/mod"
|
|
export GOPATH="${INSTALL_DIR}/.go"
|
|
export GOCACHE="${INSTALL_DIR}/.go/build"
|
|
mkdir -p "${GOMODCACHE}" "${GOCACHE}" 2>/dev/null || sudo_if mkdir -p "${GOMODCACHE}" "${GOCACHE}"
|
|
sudo_if chown -R "${APP_USER}" "${INSTALL_DIR}/.go" "${INSTALL_DIR}/app" 2>/dev/null || true
|
|
sudo_if rm -f app
|
|
CGO_ENABLED=0 go build -buildvcs=false -ldflags="-s -w" -o app .
|
|
|
|
# Fix ownership of all files (build cache may be root-owned from earlier builds).
|
|
sudo_if chown -R "${APP_USER}:${APP_USER}" "$INSTALL_DIR" 2>/dev/null || true
|
|
|
|
info "Starting service..."
|
|
sudo_if systemctl start "$SERVICE_NAME"
|
|
sleep 2
|
|
|
|
if systemctl is-active --quiet "$SERVICE_NAME"; then
|
|
info "Update complete — NextExpense is running"
|
|
else
|
|
warn "Service did not start. Check: systemctl status $SERVICE_NAME"
|
|
fi
|
|
exit 0
|
|
fi
|
|
|
|
# ──────────────────────────────────────────────────────────────────
|
|
# FRESH INSTALL
|
|
# ──────────────────────────────────────────────────────────────────
|
|
echo ""
|
|
echo " ╔═══════════════════════════════════════╗"
|
|
echo " ║ NextExpense Installer ║"
|
|
echo " ╚═══════════════════════════════════════╝"
|
|
echo ""
|
|
|
|
# Cache sudo credentials upfront.
|
|
if [ "$EUID" -ne 0 ]; then
|
|
sudo -v 2>/dev/null || { err "This installer requires sudo access. Please run: sudo ./install.sh"; exit 1; }
|
|
fi
|
|
|
|
# ── Install dependencies ───────────────────────────────────────────
|
|
if ! command -v git &>/dev/null; then
|
|
info "Installing git..."
|
|
if command -v apt-get &>/dev/null; then
|
|
sudo_if apt-get update -qq && sudo_if apt-get install -y -qq git 2>&1 | tail -1
|
|
elif command -v dnf &>/dev/null; then
|
|
sudo_if dnf install -y git 2>&1 | tail -1
|
|
elif command -v apk &>/dev/null; then
|
|
sudo_if apk add git 2>&1 | tail -1
|
|
else
|
|
err "Please install git manually, then re-run install.sh"
|
|
exit 1
|
|
fi
|
|
fi
|
|
if ! command -v curl &>/dev/null; then
|
|
info "Installing curl..."
|
|
if command -v apt-get &>/dev/null; then
|
|
sudo_if apt-get install -y -qq curl 2>&1 | tail -1
|
|
elif command -v dnf &>/dev/null; then
|
|
sudo_if dnf install -y curl 2>&1 | tail -1
|
|
elif command -v apk &>/dev/null; then
|
|
sudo_if apk add curl 2>&1 | tail -1
|
|
fi
|
|
fi
|
|
# python3 is used for parsing release JSON
|
|
if ! command -v python3 &>/dev/null; then
|
|
info "Installing python3..."
|
|
if command -v apt-get &>/dev/null; then
|
|
sudo_if apt-get install -y -qq python3 2>&1 | tail -1
|
|
elif command -v dnf &>/dev/null; then
|
|
sudo_if dnf install -y python3 2>&1 | tail -1
|
|
elif command -v apk &>/dev/null; then
|
|
sudo_if apk add python3 2>&1 | tail -1
|
|
fi
|
|
fi
|
|
|
|
# ── 1. Clone repo ────────────────────────────────────────────────
|
|
if [ -d "$INSTALL_DIR" ]; then
|
|
warn "$INSTALL_DIR already exists — pulling latest..."
|
|
# Mark the repo as safe — it was created by root via sudo
|
|
sudo_if git config --global --add safe.directory "$INSTALL_DIR" 2>/dev/null || true
|
|
cd "$INSTALL_DIR"
|
|
sudo_if git pull
|
|
else
|
|
info "Cloning repository to $INSTALL_DIR..."
|
|
sudo_if git clone "$REPO_URL" "$INSTALL_DIR"
|
|
cd "$INSTALL_DIR"
|
|
fi
|
|
# Fix ownership so the build/pull steps can write as the invoking user
|
|
sudo_if chown -R "$(whoami):$(whoami)" "$INSTALL_DIR" 2>/dev/null || true
|
|
|
|
# ── 2. Build or download binary ──────────────────────────────────
|
|
ARCH="$(uname -m)"
|
|
case "$ARCH" in
|
|
x86_64) ARCH="amd64" ;;
|
|
aarch64) ARCH="arm64" ;;
|
|
*) err "Unsupported architecture: $ARCH"; exit 1 ;;
|
|
esac
|
|
|
|
# Ensure Go is installed for building from source.
|
|
if ! command -v go &>/dev/null; then
|
|
info "Installing Go..."
|
|
if command -v apt-get &>/dev/null; then
|
|
sudo_if apt-get update -qq && sudo_if apt-get install -y -qq golang-go 2>&1 | tail -1
|
|
elif command -v dnf &>/dev/null; then
|
|
sudo_if dnf install -y golang 2>&1 | tail -1
|
|
elif command -v apk &>/dev/null; then
|
|
sudo_if apk add go 2>&1 | tail -1
|
|
else
|
|
err "No package manager found. Please install Go 1.23+ manually."
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
info "Building binary from source (pure Go, no CGO)..."
|
|
export GOMODCACHE="${INSTALL_DIR}/.go/mod"
|
|
export GOPATH="${INSTALL_DIR}/.go"
|
|
export GOCACHE="${INSTALL_DIR}/.go/build"
|
|
mkdir -p "${GOMODCACHE}" "${GOCACHE}" 2>/dev/null || sudo_if mkdir -p "${GOMODCACHE}" "${GOCACHE}"
|
|
sudo_if chown -R "$(whoami):$(whoami)" "${INSTALL_DIR}/.go" "${INSTALL_DIR}/app" 2>/dev/null || true
|
|
sudo_if rm -f app
|
|
CGO_ENABLED=0 go build -buildvcs=false -ldflags="-s -w" -o app .
|
|
info "Binary ready: $INSTALL_DIR/app"
|
|
|
|
# ── 3. Create runtime directories ────────────────────────────────
|
|
sudo_if mkdir -p storage
|
|
sudo_if chmod 755 templates static storage
|
|
|
|
# ── 4. AI Provider ───────────────────────────────────────────────
|
|
echo ""
|
|
echo " ── AI Provider ──"
|
|
echo " Which AI should process receipt images?"
|
|
echo " 1) Google Gemini (cloud API, needs API key)"
|
|
echo " 2) OpenAI / Compatible (also works with Ollama, LocalAI, etc.)"
|
|
echo ""
|
|
ask " Choose [1-2] (default: 1): "
|
|
read -r AI_CHOICE
|
|
AI_CHOICE="${AI_CHOICE:-1}"
|
|
|
|
case "$AI_CHOICE" in
|
|
2)
|
|
AI_PROVIDER="openai"
|
|
ask " API key (or press Enter for Ollama): "
|
|
read -r OPENAI_API_KEY
|
|
ask " Model [gpt-4o-mini]: "
|
|
read -r AI_MODEL
|
|
AI_MODEL="${AI_MODEL:-gpt-4o-mini}"
|
|
ask " Base URL [https://api.openai.com/v1]: "
|
|
read -r AI_BASE_URL
|
|
AI_BASE_URL="${AI_BASE_URL:-https://api.openai.com/v1}"
|
|
;;
|
|
*)
|
|
AI_PROVIDER="gemini"
|
|
ask " Gemini API key: "
|
|
read -r GEMINI_API_KEY
|
|
;;
|
|
esac
|
|
|
|
# ── 5. SMTP Configuration ────────────────────────────────────────
|
|
echo ""
|
|
echo " ── Email (SMTP) ──"
|
|
echo " Required for sending OTP codes and expense reports."
|
|
echo " Leave blank to skip (OTP codes will be logged to console)."
|
|
echo ""
|
|
ask " SMTP host: "
|
|
read -r SMTP_HOST
|
|
if [ -n "$SMTP_HOST" ]; then
|
|
ask " SMTP port [587]: "
|
|
read -r SMTP_PORT
|
|
SMTP_PORT="${SMTP_PORT:-587}"
|
|
ask " SMTP user: "
|
|
read -r SMTP_USER
|
|
ask " SMTP password: "
|
|
read -r SMTP_PASS
|
|
fi
|
|
|
|
# ── 6. General settings ──────────────────────────────────────────
|
|
echo ""
|
|
echo " ── General ──"
|
|
echo " If the app is behind a proxy (e.g. dev.lohmar.co.uk), enter the domain."
|
|
echo " Otherwise leave blank to use localhost."
|
|
echo ""
|
|
ask " Domain name [localhost]: "
|
|
read -r DOMAIN
|
|
DOMAIN="${DOMAIN:-localhost}"
|
|
ask " HTTPS? (y/N): "
|
|
read -r USE_HTTPS
|
|
if [[ "$USE_HTTPS" =~ ^[Yy]$ ]]; then
|
|
BASE_URL="https://${DOMAIN}"
|
|
else
|
|
BASE_URL="http://${DOMAIN}"
|
|
fi
|
|
ask " Web server port [8080]: "
|
|
read -r PORT
|
|
PORT="${PORT:-8080}"
|
|
if [ "$DOMAIN" = "localhost" ]; then
|
|
BASE_URL="http://localhost:${PORT}"
|
|
fi
|
|
|
|
# ── 7. Write .env ────────────────────────────────────────────────
|
|
info "Creating .env..."
|
|
sudo_if tee "$INSTALL_DIR/.env" > /dev/null << ENVEOF
|
|
# NextExpense Configuration
|
|
# Generated by install.sh on $(date)
|
|
|
|
PORT=${PORT}
|
|
BASE_URL=${BASE_URL}
|
|
AI_PROVIDER=${AI_PROVIDER}
|
|
ENVEOF
|
|
|
|
case "$AI_PROVIDER" in
|
|
openai)
|
|
sudo_if tee -a "$INSTALL_DIR/.env" > /dev/null << ENVEOF
|
|
OPENAI_API_KEY=${OPENAI_API_KEY}
|
|
AI_MODEL=${AI_MODEL}
|
|
AI_BASE_URL=${AI_BASE_URL}
|
|
ENVEOF
|
|
;;
|
|
gemini)
|
|
sudo_if tee -a "$INSTALL_DIR/.env" > /dev/null << ENVEOF
|
|
GEMINI_API_KEY=${GEMINI_API_KEY}
|
|
ENVEOF
|
|
;;
|
|
esac
|
|
|
|
if [ -n "$SMTP_HOST" ]; then
|
|
sudo_if tee -a "$INSTALL_DIR/.env" > /dev/null << ENVEOF
|
|
SMTP_HOST=${SMTP_HOST}
|
|
SMTP_PORT=${SMTP_PORT}
|
|
SMTP_USER=${SMTP_USER}
|
|
SMTP_PASS=${SMTP_PASS}
|
|
ENVEOF
|
|
fi
|
|
|
|
sudo_if chmod 600 "$INSTALL_DIR/.env"
|
|
info "Configuration saved to $INSTALL_DIR/.env"
|
|
|
|
# ── 8. Determine app user ──────────────────────────────────────────
|
|
# Detect the real user (the one who invoked the script, not root via sudo)
|
|
APP_USER="${SUDO_USER:-$(whoami)}"
|
|
# If still root, create a dedicated system user
|
|
if [ "$APP_USER" = "root" ]; then
|
|
APP_USER="${SERVICE_NAME}"
|
|
if ! id -u "${APP_USER}" &>/dev/null 2>&1; then
|
|
info "Creating system user '${APP_USER}'..."
|
|
sudo_if useradd --system --no-create-home --home-dir "${INSTALL_DIR}" --shell /usr/sbin/nologin "${APP_USER}"
|
|
fi
|
|
else
|
|
info "Using existing user '${APP_USER}'"
|
|
fi
|
|
|
|
# ── 9. Set ownership ───────────────────────────────────────────────
|
|
sudo_if chown -R "${APP_USER}:${APP_USER}" "${INSTALL_DIR}"
|
|
sudo_if chmod 755 "${INSTALL_DIR}" "${INSTALL_DIR}/templates" "${INSTALL_DIR}/static"
|
|
# Storage needs write permission for uploaded receipts
|
|
sudo_if chmod 775 "${INSTALL_DIR}/storage"
|
|
|
|
# ── 10. Systemd service ────────────────────────────────────────────
|
|
info "Creating systemd service..."
|
|
sudo_if tee "/etc/systemd/system/${SERVICE_NAME}.service" > /dev/null << SERVEOF
|
|
[Unit]
|
|
Description=NextExpense — AI-Powered Expense Tracker
|
|
Documentation=https://git.lohmar.co.uk/cclohmar/NextExpense
|
|
After=network.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=${APP_USER}
|
|
Group=${APP_USER}
|
|
WorkingDirectory=${INSTALL_DIR}
|
|
ExecStart=${INSTALL_DIR}/app
|
|
Restart=always
|
|
RestartSec=5
|
|
EnvironmentFile=-${INSTALL_DIR}/.env
|
|
StandardOutput=append:/var/log/nextexpense.log
|
|
StandardError=append:/var/log/nextexpense.log
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
SERVEOF
|
|
|
|
sudo_if systemctl daemon-reload
|
|
sudo_if systemctl enable "${SERVICE_NAME}"
|
|
info "Service created: /etc/systemd/system/${SERVICE_NAME}.service"
|
|
|
|
# ── 9. Start ─────────────────────────────────────────────────────
|
|
info "Starting NextExpense..."
|
|
sudo_if systemctl restart "${SERVICE_NAME}" 2>/dev/null || true
|
|
sleep 2
|
|
|
|
echo ""
|
|
echo " ╔═══════════════════════════════════════╗"
|
|
echo " ║ Installation Complete! ║"
|
|
echo " ╚═══════════════════════════════════════╝"
|
|
echo ""
|
|
echo " Install: $INSTALL_DIR"
|
|
echo " Binary: $INSTALL_DIR/app"
|
|
echo " Config: $INSTALL_DIR/.env"
|
|
echo " Templates: $INSTALL_DIR/templates/"
|
|
echo " Static: $INSTALL_DIR/static/"
|
|
echo " Storage: $INSTALL_DIR/storage/"
|
|
echo " Service: systemctl status $SERVICE_NAME"
|
|
echo " Logs: journalctl -u $SERVICE_NAME -f"
|
|
echo ""
|
|
|
|
if systemctl is-active --quiet "${SERVICE_NAME}"; then
|
|
info "NextExpense is running on port ${PORT}"
|
|
echo " Open http://localhost:${PORT} (or your server IP)"
|
|
else
|
|
warn "Service did not start. Check: systemctl status ${SERVICE_NAME}"
|
|
journalctl -u "${SERVICE_NAME}" -n 20 --no-pager
|
|
fi
|
|
|
|
echo ""
|
|
echo " ── Update ──"
|
|
echo " To update later: bash install.sh -update"
|
|
echo ""
|