#!/bin/bash # PDF Sort and Merge Application - Ubuntu Installation Script # This script installs and configures the Flask web application for PDF processing set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${BLUE}[INFO]${NC} $1" } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if running as root if [[ $EUID -eq 0 ]]; then log_error "This script should not be run as root. Please run as a regular user with sudo privileges." exit 1 fi # Check Ubuntu version UBUNTU_VERSION=$(lsb_release -rs) log_info "Detected Ubuntu version: $UBUNTU_VERSION" # Update system log_info "Updating system packages..." sudo apt update && sudo apt upgrade -y # Install Python 3.9+ and pip log_info "Installing Python and pip..." sudo apt install -y python3 python3-pip python3-venv python3-dev # Install system dependencies for PDF processing log_info "Installing system dependencies..." sudo apt install -y build-essential libpoppler-cpp-dev pkg-config # Install Node.js and npm (for potential frontend assets) log_info "Installing Node.js and npm..." curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install -y nodejs # Create application directory APP_DIR="$HOME/pdf-sort-merge" log_info "Creating application directory: $APP_DIR" mkdir -p "$APP_DIR" cd "$APP_DIR" # Clone or download the application # Note: Replace with your actual repository URL REPO_URL="https://github.com/yourusername/pdf-sort-merge.git" log_info "Cloning application repository..." if command -v git &> /dev/null; then sudo apt install -y git git clone "$REPO_URL" . else log_warning "Git not available. Please manually download and extract the application files to $APP_DIR" exit 1 fi # Create Python virtual environment log_info "Creating Python virtual environment..." python3 -m venv venv source venv/bin/activate # Install Python dependencies log_info "Installing Python dependencies..." pip install --upgrade pip pip install -r requirements.txt # Create .env file with Azure AD configuration log_info "Configuring Azure AD authentication..." echo "" echo "==================================================================" echo "AZURE AD CONFIGURATION REQUIRED" echo "==================================================================" echo "You need to provide your Azure AD app registration details." echo "If you don't have them, follow these steps:" echo "1. Go to https://portal.azure.com" echo "2. Navigate to Azure Active Directory > App registrations" echo "3. Create a new registration or use existing one" echo "4. Note the Application (client) ID and Directory (tenant) ID" echo "5. Create a client secret under Certificates & secrets" echo "6. Add redirect URI: http://your-server-ip:5000/auth/callback" echo "==================================================================" echo "" # Prompt for Azure AD credentials read -p "Enter Azure AD Tenant ID (Directory ID): " AZURE_TENANT_ID read -p "Enter Azure AD Client ID (Application ID): " AZURE_CLIENT_ID read -s -p "Enter Azure AD Client Secret: " AZURE_CLIENT_SECRET echo "" read -p "Enter server IP/domain (for redirect URI) [localhost]: " SERVER_HOST SERVER_HOST=${SERVER_HOST:-localhost} # Create .env file with real values cat > .env << EOF # Microsoft Entra (Azure AD) Configuration # Get these values from your Azure AD app registration in the Azure portal # Azure AD Tenant ID (Directory ID) AZURE_TENANT_ID=$AZURE_TENANT_ID # Azure AD Client ID (Application ID) AZURE_CLIENT_ID=$AZURE_CLIENT_ID # Azure AD Client Secret AZURE_CLIENT_SECRET=$AZURE_CLIENT_SECRET # Azure AD Authority URL (usually https://login.microsoftonline.com/{tenant_id}) AZURE_AUTHORITY=https://login.microsoftonline.com/$AZURE_TENANT_ID # Redirect URI (must match what's configured in Azure AD app registration) AZURE_REDIRECT_URI=http://$SERVER_HOST:5000/auth/callback # Scopes to request (openid, profile, email are common) AZURE_SCOPES=openid profile email # Flask Configuration SECRET_KEY=$(openssl rand -hex 32) EOF log_success "Azure AD configuration saved to .env file" # Create systemd service file log_info "Creating systemd service..." SERVICE_FILE="/etc/systemd/system/pdf-sort-merge.service" sudo tee "$SERVICE_FILE" > /dev/null << EOF [Unit] Description=PDF Sort and Merge Flask Application After=network.target [Service] Type=simple User=$USER WorkingDirectory=$APP_DIR Environment=PATH=$APP_DIR/venv/bin ExecStart=$APP_DIR/venv/bin/python $APP_DIR/app.py Restart=always RestartSec=5 [Install] WantedBy=multi-user.target EOF # Set proper permissions sudo chmod 644 "$SERVICE_FILE" # Create log directory log_info "Creating log directory..." sudo mkdir -p /var/log/pdf-sort-merge sudo chown $USER:$USER /var/log/pdf-sort-merge # Create application data directories log_info "Creating application data directories..." mkdir -p results static templates # Set up firewall (optional) log_info "Configuring firewall..." sudo ufw allow 5000/tcp || log_warning "Failed to configure firewall - you may need to do this manually" # Enable and start service log_info "Enabling and starting service..." sudo systemctl daemon-reload sudo systemctl enable pdf-sort-merge sudo systemctl start pdf-sort-merge # Check service status sleep 3 if sudo systemctl is-active --quiet pdf-sort-merge; then log_success "Service started successfully!" else log_error "Service failed to start. Check logs with: sudo journalctl -u pdf-sort-merge -f" fi # Create management script log_info "Creating management script..." cat > manage.sh << 'EOF' #!/bin/bash case "$1" in start) sudo systemctl start pdf-sort-merge echo "Service started" ;; stop) sudo systemctl stop pdf-sort-merge echo "Service stopped" ;; restart) sudo systemctl restart pdf-sort-merge echo "Service restarted" ;; status) sudo systemctl status pdf-sort-merge ;; logs) sudo journalctl -u pdf-sort-merge -f ;; update) cd /home/$USER/pdf-sort-merge source venv/bin/activate git pull pip install -r requirements.txt sudo systemctl restart pdf-sort-merge echo "Application updated" ;; *) echo "Usage: $0 {start|stop|restart|status|logs|update}" exit 1 ;; esac EOF chmod +x manage.sh # Final instructions log_success "Installation completed!" echo "" echo "==================================================================" echo "NEXT STEPS:" echo "1. Edit the .env file with your Azure AD credentials:" echo " nano $APP_DIR/.env" echo "" echo "2. Configure your Azure AD app registration with redirect URI:" echo " http://your-server-ip:5000/auth/callback" echo "" echo "3. Access the application at:" echo " http://localhost:5000" echo "" echo "MANAGEMENT COMMANDS:" echo " Start service: sudo systemctl start pdf-sort-merge" echo " Stop service: sudo systemctl stop pdf-sort-merge" echo " Check status: sudo systemctl status pdf-sort-merge" echo " View logs: sudo journalctl -u pdf-sort-merge -f" echo " Update app: ./manage.sh update" echo "==================================================================" # Check if .env needs configuration if grep -q "your-tenant-id-here" .env; then log_warning "IMPORTANT: Configure your .env file before using the application!" fi