#!/bin/bash # Script to set up ActivityWatch on Arch Linux with i3 # Handles installation, startup, autostart, and i3blocks status # Handles sudo privileges automatically set -e # Exit on any error # Source common library for shared functions SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" # shellcheck source=../lib/common.sh source "$SCRIPT_DIR/../lib/common.sh" # Function to check and request sudo privileges for package installation check_sudo() { if [[ $EUID -ne 0 ]] && [[ $1 == "install" ]]; then echo "Package installation requires sudo privileges." echo "Requesting sudo access..." exec sudo "$0" "$@" fi } # Get the actual user (even when running with sudo) set_actual_user_vars echo "ActivityWatch Setup for Arch Linux + i3" echo "=======================================" echo "Current Date: $(date)" echo "User: $ACTUAL_USER" echo "Target user: $ACTUAL_USER" echo "User home: $USER_HOME" # Function to check if ActivityWatch is installed check_activitywatch_installed() { echo "" echo "1. Checking ActivityWatch Installation..." echo "========================================" # Check if activitywatch-bin is installed via pacman if pacman -Qi activitywatch-bin &>/dev/null; then echo "✓ activitywatch-bin package is installed" return 0 fi # Check if aw-qt binary exists in common locations local common_paths=( "/usr/bin/aw-qt" "/usr/local/bin/aw-qt" "$USER_HOME/.local/bin/aw-qt" "$USER_HOME/activitywatch/aw-qt" ) for path in "${common_paths[@]}"; do if [[ -x $path ]]; then echo "✓ ActivityWatch found at: $path" return 0 fi done echo "✗ ActivityWatch not found" return 1 } # Function to install ActivityWatch install_activitywatch() { echo "" echo "2. Installing ActivityWatch..." echo "=============================" # Check if we need sudo for installation check_sudo "install" echo "Installing activitywatch-bin from AUR..." # Check if an AUR helper is available local aur_helpers=("yay" "paru" "makepkg") local helper_found="" for helper in "${aur_helpers[@]}"; do if command -v "$helper" &>/dev/null; then helper_found="$helper" break fi done if [[ -n $helper_found && $helper_found != "makepkg" ]]; then echo "Using AUR helper: $helper_found" if [[ $EUID -eq 0 ]]; then # Running as root, need to install as user sudo -u "$ACTUAL_USER" "$helper_found" -S --noconfirm activitywatch-bin else "$helper_found" -S --noconfirm activitywatch-bin fi else echo "No AUR helper found. Installing manually with makepkg..." install_activitywatch_manual fi echo "✓ ActivityWatch installation completed" } # Function to manually install ActivityWatch via makepkg install_activitywatch_manual() { local temp_dir="/tmp/activitywatch-install" local original_user="$ACTUAL_USER" # Create temp directory mkdir -p "$temp_dir" cd "$temp_dir" # Download PKGBUILD if command -v git &>/dev/null; then sudo -u "$original_user" git clone https://aur.archlinux.org/activitywatch-bin.git . else echo "Installing git..." pacman -S --noconfirm git sudo -u "$original_user" git clone https://aur.archlinux.org/activitywatch-bin.git . fi # Build and install package sudo -u "$original_user" makepkg -si --noconfirm # Cleanup cd / rm -rf "$temp_dir" } # Function to check if ActivityWatch is running check_activitywatch_running() { echo "" echo "3. Checking ActivityWatch Status..." echo "==================================" # Check for aw-qt process if pgrep -f "aw-qt" >/dev/null; then echo "✓ ActivityWatch (aw-qt) is running" return 0 fi # Check for aw-server process if pgrep -f "aw-server" >/dev/null; then echo "✓ ActivityWatch server is running" return 0 fi echo "✗ ActivityWatch is not running" return 1 } # Function to start ActivityWatch start_activitywatch() { echo "" echo "4. Starting ActivityWatch..." echo "===========================" # Find aw-qt executable local aw_qt_path="" if command -v aw-qt &>/dev/null; then aw_qt_path="$(which aw-qt)" elif [[ -x "/usr/bin/aw-qt" ]]; then aw_qt_path="/usr/bin/aw-qt" else echo "✗ Could not find aw-qt executable" return 1 fi echo "Starting ActivityWatch as user: $ACTUAL_USER" echo "Using aw-qt from: $aw_qt_path" # Start as the actual user in the background if [[ $EUID -eq 0 ]]; then # Running as root, start as user sudo -u "$ACTUAL_USER" env DISPLAY=:0 "$aw_qt_path" & else # Running as user "$aw_qt_path" & fi # Give it time to start sleep 3 if check_activitywatch_running >/dev/null 2>&1; then echo "✓ ActivityWatch started successfully" else echo "! ActivityWatch may be starting (check system tray)" fi } # Function to setup autostart setup_autostart() { echo "" echo "5. Setting Up Autostart..." echo "=========================" local autostart_dir="$USER_HOME/.config/autostart" local desktop_file="$autostart_dir/activitywatch.desktop" local i3_config="$USER_HOME/.config/i3/config" # Method 1: XDG Autostart (works with most desktop environments) if [[ $EUID -eq 0 ]]; then sudo -u "$ACTUAL_USER" mkdir -p "$autostart_dir" else mkdir -p "$autostart_dir" fi # Create desktop file for autostart cat >"$desktop_file" <