#!/bin/bash

# Ask for sudo password once
sudo -v || exit 1

preview_theme() {
    local theme="$1"
    local original_theme

    original_theme=$(sudo plymouth-set-default-theme)

    if ! sudo plymouth-set-default-theme "$theme"; then
        qarma --error \
            --title="Error" \
            --text="Failed to select the Plymouth theme."
        return 1
    fi

    sudo plymouthd
    sudo plymouth show-splash
    sleep 5
    sudo plymouth quit

    sudo plymouth-set-default-theme "$original_theme"
}

apply_theme() {
    local theme="$1"
    local tmpfile

    if ! sudo plymouth-set-default-theme "$theme"; then
        qarma --error \
            --title="Error" \
            --text="Failed to change the Plymouth theme."
        return 1
    fi

    tmpfile=$(mktemp)

    (
        echo "20"
        echo "# Setting Plymouth theme to $theme ..."
        sleep 1

        echo "40"
        echo "# Rebuilding initrd ..."

        if sudo dracut -f >"$tmpfile" 2>&1; then
            echo "100"
            echo "# Done"
            exit 0
        else
            exit 1
        fi
    ) |
    qarma --progress \
        --title="Plymouth Manager" \
        --text="Updating Plymouth ..." \
        --percentage=0 \
        --auto-close \
        --width=450

    if [ ${PIPESTATUS[0]} -ne 0 ]; then
        qarma --text-info \
            --title="dracut error" \
            --filename="$tmpfile" \
            --width=700 \
            --height=500

        rm -f "$tmpfile"
        return 1
    fi

    rm -f "$tmpfile"

    qqarma --info \
        --title="Plymouth Manager" \
        --text="✓ Plymouth theme changed successfully.

New theme:

$theme

The change will be visible after the next reboot."
}

while true; do

    current_theme=$(sudo plymouth-set-default-theme)
    themes=$(sudo plymouth-set-default-theme -l)

    selected_theme=$(
        printf '%s\n' "$themes" |
        qarma --list \
    --title="Plymouth Manager" \
    --text="✓ Current theme: $current_theme

Select a Plymouth theme:" \
            --column="Theme" \
            --print-column=1 \
            --width=450 \
            --height=400
    )

    [ $? -ne 0 ] && exit 0
    [ -z "$selected_theme" ] && exit 0

       action=$(
        printf '%s\n' \
            "Preview" \
            "Apply" \
            "Cancel" |
        qarma --list \
            --title="Plymouth Manager" \
            --text="Selected theme:

▶ $selected_theme

Choose what you want to do:" \
            --column="Action" \
            --print-column=1 \
            --width=400 \
            --height=300
    )

    # If the window is closed, go back to the theme list
    [ $? -ne 0 ] && continue
    [ -z "$action" ] && continue

    case "$action" in

        "Preview")
            preview_theme "$selected_theme"
            continue
            ;;

        "Apply")
            qarma --question \
                --title="Apply Plymouth Theme" \
                --text="Do you want to apply:

▶ $selected_theme

as the new Plymouth theme?"

            if [ $? -eq 0 ]; then
                apply_theme "$selected_theme"
                exit 0
            fi

            continue
            ;;

        "Cancel")
            continue
            ;;

    esac

done
