Stirling-Tools / Stirling-PDF

#1 Locally hosted web application that allows you to perform various operations on PDF files
https://stirlingpdf.com
MIT License
46.51k stars 3.79k forks source link

[Feature Request]: Automated install script for people wanting non-docker #1861

Open Frooodle opened 2 months ago

Frooodle commented 2 months ago

Feature Description

Generate a Automated install script for people wanting non-docker

Why is this feature valuable?

No response

Suggested Implementation

Mostly AI generated and needs testing (i think python side needs work and root vs non root users)

#!/bin/bash

set -e

# Stirling-PDF Installation Script
# Version: 2.4.0

# Function to log messages
log_message() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

# Function to detect if script is run with sudo
check_sudo() {
    if [ "$EUID" -ne 0 ]; then
        echo "Please run this script with sudo or as root."
        exit 1
    fi
}

# Function to detect the operating system
detect_os() {
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        OS=$NAME
    elif type lsb_release >/dev/null 2>&1; then
        OS=$(lsb_release -si)
    elif [ -f /etc/lsb-release ]; then
        . /etc/lsb-release
        OS=$DISTRIB_ID
    elif [ -f /etc/debian_version ]; then
        OS=Debian
    elif [ -f /etc/fedora-release ]; then
        OS=Fedora
    elif [ -f /etc/centos-release ]; then
        OS=CentOS
    elif [ "$(uname)" == "Darwin" ]; then
        OS=macOS
    else
        OS=$(uname -s)
    fi
    echo $OS
}

# Function to install packages based on the OS
install_packages() {
    log_message "Starting package installation"
    case $1 in
        Ubuntu|Debian)
            apt-get update
            apt-get install -y git make g++ openjdk-21-jdk
            if [ "$INSTALL_TYPE" = "full" ]; then
                apt-get install -y automake autoconf libtool libleptonica-dev pkg-config zlib1g-dev python3 python3-pip python3-venv libreoffice-writer libreoffice-calc libreoffice-impress unpaper ocrmypdf
            fi
            ;;
        Fedora|CentOS)
            dnf install -y git make gcc-c++ java-21-openjdk
            if [ "$INSTALL_TYPE" = "full" ]; then
                dnf install -y automake autoconf libtool leptonica-devel pkg-config zlib-devel python3 python3-pip python3-virtualenv libreoffice-writer libreoffice-calc libreoffice-impress unpaper ocrmypdf
            fi
            ;;
        macOS)
            brew install git make gcc openjdk@21
            if [ "$INSTALL_TYPE" = "full" ]; then
                brew install automake autoconf libtool leptonica pkg-config zlib python@3.8 libreoffice unpaper ocrmypdf
            fi
            ;;
        *)
            echo "Unsupported OS. Please install the required packages manually."
            exit 1
            ;;
    esac
    log_message "Finished package installation"
}

# Function to build and install jbig2enc
install_jbig2enc() {
    if [ "$INSTALL_TYPE" = "full" ]; then
        log_message "Starting jbig2enc installation"
        mkdir -p /tmp/jbig2enc
        cd /tmp/jbig2enc
        git clone https://github.com/agl/jbig2enc.git .
        ./autogen.sh
        ./configure
        make
        make install
        cd /
        rm -rf /tmp/jbig2enc
        log_message "Finished jbig2enc installation"
    fi
}

# Function to clone and build Stirling-PDF
build_stirling_pdf() {
    log_message "Starting Stirling-PDF build"
    mkdir -p /tmp/stirling-pdf
    cd /tmp/stirling-pdf
    git clone https://github.com/Stirling-Tools/Stirling-PDF.git .
    chmod +x ./gradlew
    if [ "$SECURITY_ENABLED" = true ]; then
        export DOCKER_ENABLE_SECURITY=true
    fi
    ./gradlew build
    log_message "Finished Stirling-PDF build"
}

# Function to move Stirling-PDF files to the desired location
move_stirling_pdf() {
    log_message "Starting moving Stirling-PDF files"
    mkdir -p /opt/Stirling-PDF
    mv /tmp/stirling-pdf/build/libs/Stirling-PDF-*.jar /opt/Stirling-PDF/
    if [ "$INSTALL_TYPE" = "full" ]; then
        mv /tmp/stirling-pdf/scripts /opt/Stirling-PDF/
    fi
    rm -rf /tmp/stirling-pdf
    log_message "Finished moving Stirling-PDF files"
}

# Function to setup user and permissions
setup_user_and_permissions() {
    log_message "Starting user and permissions setup"
    local OS=$(detect_os)
    local USER_NAME="stirling-pdf"
    local INSTALL_DIR="/opt/Stirling-PDF"

    if [ "$OS" = "macOS" ]; then
        # On macOS, use the current user
        USER_NAME=$SUDO_USER
    else
        # On Unix-like systems, create a new user if it doesn't exist
        if ! id -u $USER_NAME >/dev/null 2>&1; then
            if useradd -r -s /sbin/nologin -d $INSTALL_DIR $USER_NAME; then
                echo "Created user $USER_NAME for Stirling-PDF"
            else
                echo "Failed to create user $USER_NAME. Using $SUDO_USER instead."
                USER_NAME=$SUDO_USER
            fi
        fi
    fi

    # If $SUDO_USER is empty (running as root), use root as fallback
    if [ -z "$USER_NAME" ]; then
        USER_NAME="root"
        echo "Using root user for Stirling-PDF"
    fi

    # Set ownership and permissions
    chown -R $USER_NAME:$USER_NAME $INSTALL_DIR
    chmod -R 755 $INSTALL_DIR

    # Export USER_NAME for use in other functions
    export STIRLING_USER=$USER_NAME
    log_message "Finished user and permissions setup"
}

# Function to set up Python virtual environment and install packages
setup_python_env() {
    if [ "$INSTALL_TYPE" = "full" ]; then
        log_message "Starting Python environment setup"
        python3 -m venv /opt/Stirling-PDF/venv
        chown -R $STIRLING_USER:$STIRLING_USER /opt/Stirling-PDF/venv
        sudo -u $STIRLING_USER /opt/Stirling-PDF/venv/bin/pip install --upgrade pip
        sudo -u $STIRLING_USER /opt/Stirling-PDF/venv/bin/pip install uno opencv-python-headless unoconv pngquant WeasyPrint
        log_message "Finished Python environment setup"
    fi
}

# Function to provide information about OCR language packs
ocr_lang_pack_info() {
    echo "To install additional OCR language packs, use the following commands based on your system:"
    case $OS in
        Ubuntu|Debian)
            echo "sudo apt-get install tesseract-ocr-<lang>"
            echo "For example, to install German: sudo apt-get install tesseract-ocr-deu"
            echo "To see available language packs: apt-cache search tesseract-ocr"
            ;;
        Fedora|CentOS)
            echo "sudo dnf install tesseract-langpack-<lang>"
            echo "For example, to install German: sudo dnf install tesseract-langpack-deu"
            echo "To see available language packs: dnf search tesseract-langpack"
            ;;
        macOS)
            echo "brew install tesseract-lang"
            echo "This will install all available language packs. You can't select individual languages with Homebrew."
            ;;
    esac
}

# Function to create a desktop entry
create_desktop_entry() {
    local USER_NAME=$([ "$OS" = "macOS" ] && echo $SUDO_USER || echo "stirling-pdf")

    mkdir -p /home/$SUDO_USER/.local/share/applications
    cat > /home/$SUDO_USER/.local/share/applications/Stirling-PDF.desktop <<EOF
[Desktop Entry]
Name=Stirling PDF
GenericName=Launch StirlingPDF and open its WebGUI
Category=Office
Exec=bash -c "xdg-open http://localhost:8080 && systemctl --user start stirlingpdf.service"
Icon=/opt/Stirling-PDF/docs/stirling-transparent.svg
Keywords=pdf
Type=Application
NoDisplay=false
Terminal=false
EOF
    chown $SUDO_USER:$SUDO_USER /home/$SUDO_USER/.local/share/applications/Stirling-PDF.desktop
    log_message "Desktop entry created"
}

# Function to create a systemd service
create_systemd_service() {
    mkdir -p /etc/systemd/system
    cat > /etc/systemd/system/stirlingpdf.service <<EOF
[Unit]
Description=Stirling-PDF service
After=network.target

[Service]
Type=simple
User=$STIRLING_USER
Group=$STIRLING_USER
WorkingDirectory=/opt/Stirling-PDF
Environment="DBUS_SESSION_BUS_ADDRESS=unix:path=/tmp/stirling-pdf-dbus"
ExecStart=/bin/bash -c 'source /opt/Stirling-PDF/venv/bin/activate && exec java -jar /opt/Stirling-PDF/Stirling-PDF-*.jar'
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable stirlingpdf.service
    log_message "Systemd service created and enabled"
}

# Function to create environment file
create_env_file() {
    log_message "Starting environment file creation"
    cat > /opt/Stirling-PDF/.env <<EOF
# Custom environment variables
# APP_HOME_NAME="Stirling PDF"
# SYSTEM_DEFAULTLOCALE="en-US"
EOF
    log_message "Finished environment file creation"
}

# Main installation process
main() {
    check_sudo

    OS=$(detect_os)
    log_message "Detected OS: $OS"

    # Prompt for installation type with default
    read -p "Do you want to install lite or full Stirling PDF? (full/lite) [Default: full]: " install_choice
    install_choice=${install_choice:-full}

    case $install_choice in
        full|Full|FULL) INSTALL_TYPE="full" ;;
        lite|Lite|LITE) INSTALL_TYPE="lite" ;;
        *) echo "Invalid choice. Using default: full"; INSTALL_TYPE="full" ;;
    esac

    # Prompt for security version with default
    read -p "Do you want to install the security version of Stirling-PDF with login? (y/n) [Default: n]: " security_choice
    security_choice=${security_choice:-n}

    case $security_choice in
        [Yy]* ) SECURITY_ENABLED=true ;;
        * ) SECURITY_ENABLED=false ;;
    esac

    log_message "Starting Stirling-PDF installation"

    install_packages $OS
    install_jbig2enc
    build_stirling_pdf
    move_stirling_pdf
    setup_user_and_permissions
    setup_python_env
    create_env_file

    if [ "$OS" != "macOS" ]; then
        log_message "Creating desktop entry"
        create_desktop_entry
        log_message "Creating systemd service"
        create_systemd_service
    fi

    log_message "Stirling-PDF installation completed successfully!"

    echo "You can now start the application using the following method:"

    if [ "$OS" = "macOS" ]; then
        echo "Run the application manually (service management not available on macOS):"
        echo "source /opt/Stirling-PDF/venv/bin/activate && java -jar /opt/Stirling-PDF/Stirling-PDF-*.jar"
    else
        echo "1. Use the desktop entry (if you're in a graphical environment)"
        echo "2. Start the systemd service: sudo systemctl start stirlingpdf.service"
    fi

    echo ""
    echo "Additional notes:"
    echo "- Environment variables can be set in /opt/Stirling-PDF/.env"
    echo "- Stirling-PDF is running as user: $STIRLING_USER"

    if [ "$SECURITY_ENABLED" = true ]; then
        echo "- Security version installed. Please set up your login credentials."
    fi

    echo ""
    ocr_lang_pack_info
}

# Run the main installation process
main

Additional Information

No response

No Duplicate of the Feature

Akhil-2020171 commented 2 months ago

where to add this script to test it?

Frooodle commented 2 months ago

where to add this script to test it?

A new unix based installation