#!/bin/bash

# Expense Tracker Startup Script for Unraid
# This script should be placed in the same directory as your app

echo "========================================="
echo "Starting Expense Tracker"
echo "========================================="
echo ""

# Check if Node.js is installed
if ! command -v node &> /dev/null
then
    echo "ERROR: Node.js is not installed!"
    echo "Please install Node.js 18+ on your Unraid server"
    exit 1
fi

echo "Node.js version: $(node --version)"
echo ""

# Check if we're in the right directory
if [ ! -f "package.json" ]; then
    echo "ERROR: package.json not found!"
    echo "Please run this script from the expense-tracker directory"
    exit 1
fi

# Create uploads directory if it doesn't exist
if [ ! -d "public/uploads" ]; then
    echo "Creating uploads directory..."
    mkdir -p public/uploads
    chmod 755 public/uploads
fi

# Check if node_modules exists
if [ ! -d "node_modules" ]; then
    echo "Installing dependencies..."
    npm ci --production
fi

# Check if .next exists
if [ ! -d ".next" ]; then
    echo "ERROR: .next folder not found!"
    echo "Please run 'npm run build' before deploying"
    exit 1
fi

echo "Starting server on port 3000..."
echo "Access your app at: http://$(hostname -I | awk '{print $1}'):3000"
echo ""
echo "Press Ctrl+C to stop"
echo ""

# Start the server
npm start
