invoice-system/setup.sh

99 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
echo "============================================"
echo "Quote & Invoice System Setup"
echo "Bay Area Affiliates, Inc."
echo "============================================"
echo ""
# Check if PostgreSQL is installed
if ! command -v psql &> /dev/null; then
echo "❌ PostgreSQL is not installed. Please install PostgreSQL first."
exit 1
fi
echo "✓ PostgreSQL found"
echo ""
# Get database credentials
read -p "Enter PostgreSQL database name [quotes_db]: " DB_NAME
DB_NAME=${DB_NAME:-quotes_db}
read -p "Enter PostgreSQL username [quoteuser]: " DB_USER
DB_USER=${DB_USER:-quoteuser}
read -sp "Enter PostgreSQL password: " DB_PASSWORD
echo ""
read -p "Enter PostgreSQL host [localhost]: " DB_HOST
DB_HOST=${DB_HOST:-localhost}
read -p "Enter PostgreSQL port [5432]: " DB_PORT
DB_PORT=${DB_PORT:-5432}
read -p "Enter application port [3000]: " APP_PORT
APP_PORT=${APP_PORT:-3000}
echo ""
echo "Creating database and user..."
# Create database (as postgres user)
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME;" 2>/dev/null
sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';" 2>/dev/null
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;" 2>/dev/null
sudo -u postgres psql -d $DB_NAME -c "GRANT ALL ON SCHEMA public TO $DB_USER;" 2>/dev/null
echo "✓ Database setup complete"
echo ""
# Create .env file
echo "Creating .env file..."
cat > .env << EOF
DB_HOST=$DB_HOST
DB_PORT=$DB_PORT
DB_USER=$DB_USER
DB_PASSWORD=$DB_PASSWORD
DB_NAME=$DB_NAME
PORT=$APP_PORT
NODE_ENV=production
EOF
echo "✓ .env file created"
echo ""
# Run database migrations
echo "Running database migrations..."
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f init.sql
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f add_invoices.sql
echo "✓ Database tables created"
echo ""
# Install Node.js dependencies
echo "Installing Node.js dependencies..."
npm install
echo "✓ Dependencies installed"
echo ""
# Create uploads directory
mkdir -p public/uploads
echo "✓ Upload directory created"
echo ""
echo "============================================"
echo "Setup Complete! 🎉"
echo "============================================"
echo ""
echo "To start the server:"
echo " npm start"
echo ""
echo "To start in development mode:"
echo " npm run dev"
echo ""
echo "Access the application at:"
echo " http://localhost:$APP_PORT"
echo ""
echo "============================================"