#!/bin/bash

# menu similar to the super + p menu on windows
# used to quickly change settings when docking a laptop

# get list of monitor names
getdisplays() {
    xrandr | grep '[^s]connected' | grep -o '^[^ ]*'
}

getlayouts() {
    autorandr --list | grep -v "^instantos$" | sed "s/^/{blue icon=view-dashboard} custom: /g"
}

# instantmenu formatted list of monitors
monitorchoices() {
    echo "{blue icon=monitor-multiple} Extend
{blue icon=content-copy} Duplicate
$(getdisplays | sed 's/^/{blue icon=monitor} /' | sed 's/$/ only/')
$(getlayouts)"

}

mainmenu() {

    echo "{heading highlight} PROJECT
$(monitorchoices)
{blue icon=view-dashboard} Default layout
{green icon=tune} Set default action
{red icon=x} Close menu" | instantmenu \
        --lines 20 \
        --line-height auto \
        --position center \
        --width auto \
        --border-width 4 \
        --placeholder search \
        --insensitive
}

defaultmenu() {
    echo "{heading highlight} Default projection settings
$(monitorchoices)
{blue icon=help-circle} Ask
{red icon=ban} None
{blue icon=arrow-left} Back" | instantmenu \
        --lines 20 \
        --line-height auto \
        --position center \
        --width auto \
        --border-width 4 \
        --placeholder search \
        --insensitive
}

restartdunst() {
    sleep 2
    if pgrep dunst; then
        echo "restarting dunst"
        pkill dunst
        sleep 1
        dunst &
    fi
}

choicetomonitor() {
    sed 's/ only$//g' <<<"$1" | sed 's/^only://'
}

# set action to execute when a new monitor is connected
setdefault() {
    DEFAULTLOOPING="true"
    while [ -n "$DEFAULTLOOPING" ]; do
        DEFAULTCHOICE="$(defaultmenu)"

        if [ -z "$DEFAULTCHOICE" ]; then
            continue
        else
            unset DEFAULTLOOPING
        fi

        case "$DEFAULTCHOICE" in
            *Extend)
                iconf automon e
                ;;
            *only)
                iconf automon s
                if [ -n "$(choicetomonitor "$DEFAULTCHOICE")" ]; then
                    iconf automon "only:$DEFAULTCHOICE"
                fi
                ;;
            *Duplicate)
                iconf automon c
                ;;
            *None)
                iconf automon n
                ;;
            *Ask)
                iconf -d automon
                ;;
            *) # Back
                instantdisper &
                exit
                ;;
        esac
    done
}

# turn off all monitors except $1
onlymonitor() {
    getdisplays | grep -v "$1" | while read -r line; do
        xrandr --output "$line" --off
    done
    xrandr --output "$1" --auto
}

# apply saved setting
applydefault() {
    if ! iconf automon; then
        echo "no default monitor setting"
        return
    fi

    AUTOMON="$(iconf automon)"
    if grep -q '^only:' <<<"$AUTOMON"; then
        onlymonitor "${AUTOMON//only:/}"
        return
    fi

    autorandr "$AUTOMON" && restartdunst &
}

if [ -n "$1" ]; then
    case "$1" in
        apply)
            if iconf automon; then
                applydefault
                exit
            fi
            ;;
        setdefault)
            setdefault
            applydefault
            exit
            ;;
        settings)
            echo "starting instantdisper"
            ;;
    esac
fi

LOOPING="true"
while [ -n "$LOOPING" ]; do
    CHOICE="$(mainmenu)"
    unset LOOPING
    case "$CHOICE" in
        *"Default layout")
            if ! autorandr --list | grep -q '^instantos$'; then
                autorandr horizontal
            else
                autorandr instantos
            fi
            restartdunst &
            ;;
        *Extend)
            echo "using extended layout"
            autorandr horizontal
            restartdunst &
            ;;
        *only)
            onlymonitor "$(choicetomonitor "$CHOICE")"
            restartdunst &
            ;;
        *Duplicate)
            echo "duplicating monitors"
            autorandr common
            restartdunst &
            ;;
        *action)
            echo "setting default action"
            setdefault
            applydefault
            ;;
        *"custom: "*)
            autorandr "$(sed 's/^custom: //g' <<<"$CHOICE")"
            ;;
        *menu | '')
            exit
            ;;
        *)
            echo "choice $CHOICE"
            LOOPING="true"
            ;;
    esac

done
