#!/bin/bash
# This is a helper script to perform common tasks with regolith looks.

set -Eeu -o pipefail

# Load common functions
source /usr/lib/regolith/regolith-session-common.sh

if [ "$#" -lt 1 ]; then
    echo "Usage: regolith-look <command>"
    echo "Actions: "
    echo "    refresh - Update the current session look based on Xresources values"
    echo "    list - Display currently installed looks in the /usr/share/regolith-look directory"
    echo "    set <look> - Write a global override file in the user dir to specify the desired look"
    exit 1
fi

COMMAND=$1

WM_RELOAD="i3-msg -q restart"

load_sway_resources() {
    load_standard_trawlres
    load_regolith_trawlres
    load_regolith_trawlres
    trawl_sway_cleanup
}

load_i3_resources() {
    load_standard_xres # Load first to allow override of default look
    load_regolith_xres # Find all Xresource partials and merge them
    load_standard_xres # Re-apply any values that may have been overwritten

    # Change the quotes in workspace names from double to to single.
    # Due to a limitation of the preprocessor they have double quotes.
    # The i3-wm workspace command fails with double quotes in the name
    xres_i3_cleanup
}

refresh() {
    if [[ $XDG_SESSION_TYPE == "wayland" ]]; then
        WM_RELOAD="swaymsg reload"
        load_sway_resources
        swaymsg -t send_tick '{"status": "reload_pending"}'
    else
	    load_i3_resources
    fi

    # Determine if Look loader script is available
    LOADER_PATH="$($RESOURCE_GETTER regolith.look.loader)"
    if [ -n  "$LOADER_PATH" ]; then
        # Load the look-specific script that has a function called load_look()
        source "$LOADER_PATH"
        # Call look function to update UI
        load_look

        # restart window manager after merging Xresources
        $WM_RELOAD
    else
        echo "No look loader script specified, no UI changes"
    fi
}

# Upsert Xresource key regolith.look.path and refresh session
set_look() {
    if [ ! -d "$LOOK_PATH" ]; then
        echo "Aborting, invalid look directory: $LOOK_PATH"
        exit 1
    fi

    # Unset any existing values
    if [ -e "$USER_XRESOURCE_OVERRIDE_FILE" ]; then
	# remove existing declaration comments
        sed -i '/^!regolith.look.path/d' "$USER_XRESOURCE_OVERRIDE_FILE"

        # disable existing declaration
        sed -i 's/^regolith.look.path/\!regolith.look.path/g' "$USER_XRESOURCE_OVERRIDE_FILE"
    fi

    # add new declaration
    echo "regolith.look.path: $LOOK_PATH" >> "$USER_XRESOURCE_OVERRIDE_FILE"

    if [[ $XDG_SESSION_TYPE == "wayland" ]]; then
        echo "setting for wayland"
        trawldb -I "$USER_XRESOURCE_SEARCH_PATH" --merge "$USER_XRESOURCE_OVERRIDE_FILE"
    else
        xrdb -I"$USER_XRESOURCE_SEARCH_PATH" -merge "$USER_XRESOURCE_OVERRIDE_FILE"
    fi
    # The new look must be loaded into Xresources before refresh occurs
    refresh
}

if [ "$COMMAND" == "refresh" ]; then
    refresh
elif [ "$COMMAND" == "set" ]; then
    LOOK_NAME=$2
    if [ -z "$LOOK_NAME" ]; then
        echo "Must specify a look to set."
        exit 1
    else
        LOOK_PATH="$DEFAULT_LOOK_ROOT/$LOOK_NAME"

        if [ -d "$LOOK_PATH" ]; then
            set_look
        else
            LOOK_PATH="$USER_LOOK_ROOT/$LOOK_NAME"
            if [ -d "$LOOK_PATH" ]; then
                set_look
            else
                echo "Unable to find look $LOOK_NAME in $DEFAULT_LOOK_PATH or $USER_LOOK_PATH"
            fi
        fi
    fi
elif [ "$COMMAND" == "list" ]; then
    if [ -d "$USER_LOOK_ROOT" ]; then
        find {"$DEFAULT_LOOK_ROOT","$USER_LOOK_ROOT"} -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | sort
    else
        find "$DEFAULT_LOOK_ROOT" -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | sort
    fi
else
    echo "Unrecognized command: $COMMAND"
    exit 1
fi
