#!/bin/bash
# TODO: Dotfiles may be in a different place
if [ ! -f "$HOME/.dotfiles-config" ]; then
    echo "$HOME/.dotfiles-config not found, please run dotfiles/install.sh to create it"
    exit 1
fi
source "$HOME/.dotfiles-config"
source "$DOTFILES_DIR/functions.sh"

# XAUTHORITY file may be in /tmp or it may be ~/.XAUTHORITY
if [ -f "$HOME/.Xauthority" ]; then
    export XAUTHORITY="$HOME/.Xauthority"
else
    auth_path="$(find /tmp -maxdepth 1 -name "xauth_*")"
    if [ ! -z "$auth_path" ]; then
        export XAUTHORITY="$auth_path"
    else
        echo "No Xauthority file found. Exiting"
        exit 1
    fi
fi
wallpapers_path="$HOME/wallpapers"
wallpapers_vertical_path="$HOME/wallpapers-vertical"

if [ ! -d "$wallpapers_path" ]; then
    echo "ERROR: Wallpaper directory not found!"
    echo "Please create wallpapers directory in $wallpapers_path"
    exit 1
fi

if program_installed xrandr; then
    display_count="$(xrandr | grep ' connected ' | wc -l)"
    display_order="$(xrandr --listmonitors | tail -n +2 | sed 's/[\+\*]//g' | awk '{ print $2}')"
    # Build feh command
    feh_args=("--bg-fill")
    for i in $(seq 1 "$display_count"); do
        display_name="$(echo "$display_order" | sed "${i}q;d")"
        # Get information about the current display, need verbose to always return rotation
        # and remove 'primary' for consistent column placement
        display_info="$(xrandr --query --verbose | grep "$display_name" | sed 's/primary //')"
        # Get the selected resolution for the display
        resolution="$(echo "$display_info" | awk '{ print $3 }' | sed 's/\+.*//')"
        # Get the rotation for each display
        rotation="$(echo "$display_info" | awk '{ print $5 }')"
        path="$wallpapers_path"
        resolution_segment=""
        if [ "$rotation" != "normal" ] && [ "$rotation" != "inverted" ]; then
            if [ ! -d "$wallpapers_vertical_path" ]; then
                echo "$rotation"
                echo "ERROR: Vertical display detected, but vertical wallpapers directory not found!"
                echo "Please create vertical wallpapers directory in $wallpapers_vertical_path"
                exit 1
            fi
            path="$wallpapers_vertical_path"
        fi
        # If we have a resolution-specific path for the display, use it
        if [ -d "$path/1080p" ]; then
            resolution_segment="/1080p"
        fi
        if [ -d "$path/4k" ]; then
            if [ "$resolution" = "3840x2160" ] || [ "$resolution" = "2160x3840" ]; then
                resolution_segment="/4k"
            fi
        fi
        display_wallpaper="$(shuf -en1 "$path$resolution_segment"/*)"
        feh_args+=("$display_wallpaper")
    done
    echo "feh ${feh_args[@]}"
    feh "${feh_args[@]}"
else
    # Default to ~/wallpapers/1080p or ~/wallpapers for first display if no xrandr
    if [ -d "$path/1080p" ]; then
        feh --bg-fill "$(shuf -en1 $wallpapers_path/1080p/*)"
    else
        feh --bg-fill "$(shuf -en1 $wallpapers_path/*)"
    fi
fi
