#!/bin/bash
set -euo pipefail
# ==============================================================================
# Script Name: install_maim_rk3588.sh
# Purpose: Automates the build and installation of 'maim' and 'slop' on Debian 11.
# Target System: ARM64 Rockchip RK3588 (Bare-metal)
# Execution Context: User home directory
# Tutorial Source: VPC-3588 Maim Installation Guide
# ==============================================================================
# --- Configuration Variables ---
SLOP_REPO_URL="https://github.com/naelstrof/slop.git"
MAIM_REPO_URL="https://github.com/naelstrof/maim.git"
INSTALL_PREFIX="/usr/local" # Tutorial specifies /usr/local (Note: Overwrites system files)
CPU_CORES=8 # RK3588 has 8 cores
# --- Helper Functions ---
# Function: cleanup
# Description: Removes temporary build directories on script exit.
cleanup() {
local exit_code=$?
echo "----------------------------------------------------------------"
if [ -n "${TEMP_BUILD_DIR:-}" ] && [ -d "$TEMP_BUILD_DIR" ]; then
echo "Cleaning up temporary directory: $TEMP_BUILD_DIR"
rm -rf "$TEMP_BUILD_DIR"
fi
if [ $exit_code -eq 0 ]; then
echo "Script finished successfully."
else
echo "Script failed with exit code $exit_code."
fi
}
# Trap cleanup on EXIT, SIGINT (Ctrl+C), and SIGTERM
trap cleanup EXIT SIGINT SIGTERM
# Function: ensure_package_installed
# Description: Checks if a package is installed; installs it if missing.
ensure_package_installed() {
local package_name="$1"
if ! dpkg -s "$package_name" >/dev/null 2>&1; then
echo "Package '$package_name' is missing. Installing..."
if ! sudo apt install -y "$package_name"; then
echo "Error: Failed to install package '$package_name'." >&2
exit 1
fi
else
echo "Package '$package_name' is already installed."
fi
}
# --- Main Script Execution ---
echo "================================================================"
echo " Maim & Slop Build/Install Script for RK3588 (Debian 11) "
echo "================================================================"
# 1. Critical Safety Confirmation
echo "WARNING: This script compiles software and installs it directly to '$INSTALL_PREFIX'."
echo "This action:"
echo " - Requires sudo privileges."
echo " - May overwrite files managed by the system package manager."
echo " - Is based on the specific VPC-3588 tutorial provided."
echo ""
if command -v maim >/dev/null 2>&1; then
echo "NOTICE: 'maim' is already installed at $(command -v maim)."
echo "Proceeding will overwrite the existing installation."
echo ""
fi
read -r -p "Do you want to proceed with the installation? (yes/NO): " confirmation
if [[ "$confirmation" != "yes" ]]; then
echo "Operation cancelled by user." >&2
exit 1
fi
# 2. Create Temporary Build Directory
# Using mktemp as per best practices for secure temporary directory creation
TEMP_BUILD_DIR=$(mktemp -d -t maim_build.XXXXXX)
echo "Created temporary build directory: $TEMP_BUILD_DIR"
# 3. Phase 1: Install Build Dependencies
echo ""
echo ">>> Phase 1: Installing Build Dependencies..."
echo "Updating package lists..."
if ! sudo apt update; then
echo "Error: Failed to update package lists." >&2
exit 1
fi
# List of dependencies from the tutorial
DEPENDENCIES=(
"build-essential"
"git"
"cmake"
"libx11-dev"
"libxext-dev"
"libxfixes-dev"
"libxrandr-dev"
"libxrender-dev"
"libxcomposite-dev"
"libglm-dev"
"libpng-dev"
"libjpeg62-turbo-dev"
"libwebp-dev"
"libicu-dev"
)
for dep in "${DEPENDENCIES[@]}"; do
ensure_package_installed "$dep"
done
# 4. Phase 2: Build and Install 'slop' (Prerequisite)
echo ""
echo ">>> Phase 2: Building and Installing 'slop'..."
# Navigate to temp dir
cd "$TEMP_BUILD_DIR"
echo "Cloning slop repository..."
if ! git clone "$SLOP_REPO_URL"; then
echo "Error: Failed to clone slop repository." >&2
exit 1
fi
cd slop
echo "Configuring slop (CMake)..."
# Tutorial specifies in-source build with /usr/local prefix
if ! cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX" ./; then
echo "Error: CMake configuration for slop failed." >&2
exit 1
fi
echo "Compiling slop (Jobs: $CPU_CORES)..."
if ! make -j"$CPU_CORES"; then
echo "Error: Compilation of slop failed." >&2
exit 1
fi
echo "Installing slop..."
if ! sudo make install; then
echo "Error: Installation of slop failed." >&2
exit 1
fi
# 5. Phase 3: Build and Install 'maim'
echo ""
echo ">>> Phase 3: Building and Installing 'maim'..."
# Return to temp root
cd "$TEMP_BUILD_DIR"
echo "Cloning maim repository..."
if ! git clone "$MAIM_REPO_URL"; then
echo "Error: Failed to clone maim repository." >&2
exit 1
fi
cd maim
echo "Configuring maim (CMake)..."
# CMake should find the SLOP library installed in the previous step
if ! cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX" ./; then
echo "Error: CMake configuration for maim failed." >&2
exit 1
fi
echo "Compiling maim (Jobs: $CPU_CORES)..."
if ! make -j"$CPU_CORES"; then
echo "Error: Compilation of maim failed." >&2
exit 1
fi
echo "Installing maim..."
if ! sudo make install; then
echo "Error: Installation of maim failed." >&2
exit 1
fi
echo "Updating shared library cache (ldconfig)..."
if ! sudo ldconfig; then
echo "Warning: 'ldconfig' command failed. Shared libraries might not be linked correctly." >&2
fi
# 6. Phase 4: Verification
echo ""
echo ">>> Phase 4: Verification..."
if command -v maim >/dev/null 2>&1; then
INSTALLED_VERSION=$(maim --version)
echo "Success! 'maim' has been installed."
echo "Detected Version: $INSTALLED_VERSION"
# Check for X11 Display before attempting screenshot
if [ -n "${DISPLAY:-}" ]; then
TEST_FILE="$HOME/maim_test_$(date +%s).png"
echo "Taking a test screenshot to: $TEST_FILE"
if maim "$TEST_FILE"; then
echo "Screenshot successful."
else
echo "Warning: Test screenshot failed. Ensure X11 is running correctly." >&2
fi
else
echo "Skipping screenshot test: DISPLAY environment variable not set (Headless environment?)."
fi
else
echo "Error: 'maim' command not found in PATH after installation." >&2
exit 1
fi
echo ""
echo "Installation process completed."
exit 0
Ref.
https://github.com/naelstrof/maim