#!/bin/bash
# PupControl Icon Manager for the wayland desktop of SpaceFM
# 251112 radky: adapted from wizard's wayland desktop control panel
# 260101 radky: add Partition Label, Save and Help options

export TEXTDOMAIN=pupcontrol
export OUTPUT_CHARSET=UTF-8

APPDIR=/usr/local/PupControl

# stop conky display
function conky_stop() {
    [ -n "$(pidof conky 2>/dev/null)" ] && killall conky &
}

# lock desktop icons
function desktop_icons_lock() {
    chattr +i ~/.config/zzzfm/desktop0
}

# unlock desktop icons
function desktop_icons_unlock() {
    chattr -i ~/.config/zzzfm/desktop0
}

# drive icon position
function drive_icon_position() {
if [ ! -e ~/.config/autostart/zzzfm.desktop ] || [ -e ~/.config/zzzfm/particons_off ]; then
    yad --title="Alert" \
    --borders=20 \
    --text-align=center \
    --text="\nDesktop drive icons are not enabled\n" \
    --buttons-layout=center \
    --button="OK:0"
else
    spacefm-gridrow
fi
}

export -f conky_stop desktop_icons_lock desktop_icons_unlock drive_icon_position

# dialog for desktop icon management
yad 2>/dev/null \
    --title="Icons" \
    --borders=10 \
    --text-align=center \
    --text="<b>Manage desktop icons</b>\n" \
    --button="Theme":1 \
    --button="Padding":2 \
    --button="DPI Scale":3 \
    --button="Layout":4 \
    --button="Lock":5 \
    --button="Drive Icons":6 \
    --button="Save":7 \
    --button="Help":8
    ret=$?
    if [ "$ret" == 1 ]; then
        icon_switcher &
        exit
    elif [ "$ret" == 2 ]; then
        zzzfm --show-pref=1 &
        exit
    elif [ "$ret" == 3 ]; then
        wdisplays-wrapper
    elif [ "$ret" == 4 ]; then
        yad 2>/dev/null --title="Desktop Application Icons" \
            --borders=10 \
            --text-align=center \
            --text="Choose a layout for desktop application icons. 
\nThe current application icons will be replaced 
including save2flash and custom icon layouts.
\nThe selected layout will be locked to prevent
modification, followed by restart of Wayland.\n" \
            --button="No Icons":1 \
            --button="Minimal":2 \
            --button="Modern":3 \
            --button="Retro":4 \
            --button="Cancel":0
            ret=$?
            if [ "$ret" == 1 ]; then
                conky_stop
                desktop_icons_unlock # unlock desktop icons
                rm -f ~/Desktop/* ~/.config/zzzfm/row-calc ~/.config/zzzfm/row-sel
                particons-row
                echo -n > ~/.config/zzzfm/desktop0
                desktop_icons_lock # lock ~/Desktop folder
                restartwm
                particons
                sleep 5
                spacefm-gridrow
                exit
            elif [ "$ret" == 2 ]; then
                conky_stop
                desktop_icons_unlock # unlock desktop icons
                rm -f ~/Desktop/* ~/.config/zzzfm/row-calc ~/.config/zzzfm/row-sel
                particons-row

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=File
Icon=/usr/local/lib/X11/pixmaps/home48.png
Exec=defaultfilemanager' > ~/Desktop/file.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Terminal
Icon=/usr/local/lib/X11/pixmaps/console48.png
Exec=defaultterminal' > ~/Desktop/terminal.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Browse
Icon=/usr/local/lib/X11/pixmaps/www48.png
Exec=defaultbrowser' > ~/Desktop/browse.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Settings
Icon=/usr/local/lib/X11/pixmaps/configuration48.png
Exec=pupcontrol' > ~/Desktop/settings.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Trash
Icon=/usr/local/lib/X11/pixmaps/trashcan_empty48.png
Exec=spacefm ~/.local/share/Trash/files' > ~/Desktop/trash.desktop

                echo '1=file.desktop
2=terminal.desktop
3=browse.desktop
4=settings.desktop
5=trash.desktop' > ~/.config/zzzfm/desktop0

                desktop_icons_lock # lock new icons
                restartwm
                particons
                sleep 5
                spacefm-gridrow
                exit
            elif [ "$ret" == 3 ]; then
                conky_stop
                desktop_icons_unlock # unlock desktop icons
                rm -f ~/Desktop/* ~/.config/zzzfm/row-calc ~/.config/zzzfm/row-sel
                particons-row

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=File
Icon=/usr/local/lib/X11/pixmaps/home48.png
Exec=defaultfilemanager' > ~/Desktop/file.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Write
Icon=/usr/local/lib/X11/pixmaps/word48.png
Exec=defaultwordprocessor' > ~/Desktop/write.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Browse
Icon=/usr/local/lib/X11/pixmaps/www48.png
Exec=defaultbrowser' > ~/Desktop/browse.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Terminal
Icon=/usr/local/lib/X11/pixmaps/console48.png
Exec=defaultterminal' > ~/Desktop/terminal.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Help
Icon=/usr/local/lib/X11/pixmaps/help48.png
Exec=defaultbrowser file:///usr/share/doc/index.html' > ~/Desktop/help.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Calc
Icon=/usr/local/lib/X11/pixmaps/spread48.png
Exec=defaultspreadsheet' > ~/Desktop/calc.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Email
Icon=/usr/local/lib/X11/pixmaps/email48.png
Exec=defaultemail' > ~/Desktop/email.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Run
Icon=/usr/share/icons/run48.png
Exec=defaultrun' > ~/Desktop/run.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Mount
Icon=/usr/local/lib/X11/pixmaps/drive48.png
Exec=pmount' > ~/Desktop/mount.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Edit
Icon=/usr/local/lib/X11/pixmaps/edit48.png
Exec=defaulttexteditor' > ~/Desktop/edit.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Chat
Icon=/usr/local/lib/X11/pixmaps/chat48.png
Exec=defaultchat' > ~/Desktop/chat.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Zip
Icon=/usr/local/lib/X11/pixmaps/archive48.png
Exec=defaultarchiver' > ~/Desktop/archive.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Install
Icon=/usr/share/icons/install48.png
Exec=dotpup' > ~/Desktop/install.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Paint
Icon=/usr/local/lib/X11/pixmaps/paint48.png
Exec=defaultpaint' > ~/Desktop/paint.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Connect
Icon=/usr/local/lib/X11/pixmaps/connect48.png
Exec=defaultconnect' > ~/Desktop/connect.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Lock
Icon=/usr/local/lib/X11/pixmaps/lock-screen48.png
Exec=puplock' > ~/Desktop/lock.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Setup
Icon=/usr/local/lib/X11/pixmaps/configuration48.png
Exec=pupcontrol' > ~/Desktop/setup.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Play
Icon=/usr/local/lib/X11/pixmaps/multimedia48.png
Exec=defaultmediaplayer' > ~/Desktop/play.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Plan
Icon=/usr/local/lib/X11/pixmaps/date48.png
Exec=osmo-wrapper' > ~/Desktop/contact.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Trash
Icon=/usr/local/lib/X11/pixmaps/trashcan_empty48.png
Exec=spacefm ~/.local/share/Trash/files' > ~/Desktop/trash.desktop

ypix=$(wlr-randr | grep current | cut -d'x' -f2 | cut -d' ' -f1)
yicons=$(cat ~/.config/zzzfm/row-calc)
[ -z "$yicons" ] && yicons=$(($ypix / 108))
col2="$yicons"
col3=$(($yicons * 2))
col4=$(($yicons * 3))
col5=$(($yicons * 4))
help=$(($col2 + 1))
calc=$(($col2 + 2))
email=$(($col2 + 3))
run=$(($col2 + 4))
mount=$(($col3 + 1))
edit=$(($col3 + 2))
chat=$(($col3 + 3))
archive=$(($col3 + 4))
install=$(($col4 + 1))
paint=$(($col4 + 2))
connect=$(($col4 + 3))
lock=$(($col4 + 4))
setup=$(($col5 + 1))
play=$(($col5 + 2))
contact=$(($col5 + 3))
trash=$(($col5 + 4))

                echo "1=file.desktop
2=write.desktop
3=browse.desktop
4=terminal.desktop
$help=help.desktop
$calc=calc.desktop
$email=email.desktop
$run=run.desktop
$mount=mount.desktop
$edit=edit.desktop
$chat=chat.desktop
$archive=archive.desktop
$install=install.desktop
$paint=paint.desktop
$connect=connect.desktop
$lock=lock.desktop
$setup=setup.desktop
$play=play.desktop
$contact=contact.desktop
$trash=trash.desktop" > ~/.config/zzzfm/desktop0

                desktop_icons_lock # lock new icons
                restartwm
                particons
                sleep 5
                spacefm-gridrow
                exit
            elif [ "$ret" == 4 ]; then
                conky_stop
                desktop_icons_unlock # unlock desktop icons
                rm -f ~/Desktop/* ~/.config/zzzfm/row-calc ~/.config/zzzfm/row-sel
                particons-row

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=File
Icon=/usr/local/lib/X11/pixmaps/home48.png
Exec=defaultfilemanager' > ~/Desktop/file.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Write
Icon=/usr/local/lib/X11/pixmaps/word48.png
Exec=defaultwordprocessor' > ~/Desktop/write.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Browse
Icon=/usr/local/lib/X11/pixmaps/www48.png
Exec=defaultbrowser' > ~/Desktop/browse.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Plan
Icon=/usr/local/lib/X11/pixmaps/date48.png
Exec=osmo-wrapper' > ~/Desktop/contact.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Connect
Icon=/usr/local/lib/X11/pixmaps/connect48.png
Exec=defaultconnect' > ~/Desktop/connect.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Help
Icon=/usr/local/lib/X11/pixmaps/help48.png
Exec=defaultbrowser file:///usr/share/doc/index.html' > ~/Desktop/help.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Calc
Icon=/usr/local/lib/X11/pixmaps/spread48.png
Exec=defaultspreadsheet' > ~/Desktop/calc.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Email
Icon=/usr/local/lib/X11/pixmaps/email48.png
Exec=defaultemail' > ~/Desktop/email.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Play
Icon=/usr/local/lib/X11/pixmaps/multimedia48.png
Exec=defaultmediaplayer' > ~/Desktop/play.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Mount
Icon=/usr/local/lib/X11/pixmaps/drive48.png
Exec=pmount' > ~/Desktop/mount.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Paint
Icon=/usr/local/lib/X11/pixmaps/paint48.png
Exec=defaultpaint' > ~/Desktop/paint.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Chat
Icon=/usr/local/lib/X11/pixmaps/chat48.png
Exec=defaultchat' > ~/Desktop/chat.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Install
Icon=/usr/share/icons/install48.png
Exec=dotpup' > ~/Desktop/install.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Edit
Icon=/usr/local/lib/X11/pixmaps/edit48.png
Exec=defaulttexteditor' > ~/Desktop/edit.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Setup
Icon=/usr/local/lib/X11/pixmaps/configuration48.png
Exec=pupcontrol' > ~/Desktop/setup.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Terminal
Icon=/usr/local/lib/X11/pixmaps/console48.png
Exec=defaultterminal' > ~/Desktop/terminal.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Lock
Icon=/usr/local/lib/X11/pixmaps/lock-screen48.png
Exec=puplock' > ~/Desktop/lock.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Zip
Icon=/usr/local/lib/X11/pixmaps/archive48.png
Exec=defaultarchiver' > ~/Desktop/archive.desktop

                echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=Trash
Icon=/usr/local/lib/X11/pixmaps/trashcan_empty48.png
Exec=spacefm ~/.local/share/Trash/files' > ~/Desktop/trash.desktop

ypix=$(wlr-randr | grep current | cut -d'x' -f2 | cut -d' ' -f1)
yicons=$(cat ~/.config/zzzfm/row-calc)
[ -z "$yicons" ] && yicons=$(($ypix / 108))
col2="$yicons"
col3=$(($yicons * 2))
col4=$(($yicons * 3))
col5=$(($yicons * 4))
col6=$(($yicons * 5))
col7=$(($yicons * 6))
col8=$(($yicons * 7))
help=$(($col2 + 1))
calc=$(($col2 + 2))
email=$(($col2 + 3))
play=$(($col2 + 4))
mount=$(($col3 + 1))
paint=$(($col3 + 2))
chat=$(($col3 + 3))
install=$(($col4 + 1))
edit=$(($col4 + 2))
setup=$(($col5 + 1))
terminal=$(($col5 + 2))
lock=$(($col6 + 1))
archive=$(($col7 + 1))
trash=$(($col8 + 1))

                echo "1=file.desktop
2=write.desktop
3=browse.desktop
4=contact.desktop
5=connect.desktop
$help=help.desktop
$calc=calc.desktop
$email=email.desktop
$play=play.desktop
$mount=mount.desktop
$paint=paint.desktop
$chat=chat.desktop
$install=install.desktop
$edit=edit.desktop
$setup=setup.desktop
$terminal=terminal.desktop
$lock=lock.desktop
$archive=archive.desktop
$trash=trash.desktop" > ~/.config/zzzfm/desktop0

                desktop_icons_lock # lock new icons
                restartwm
                particons
                sleep 5
                spacefm-gridrow
                exit
            fi
            $APPDIR/desktop-icons
            exit
    elif [ "$ret" == 5 ]; then
        # check if desktop0 is locked (i.e. check if immutable attribute is set)
        locked=$(lsattr ~/.config/zzzfm/desktop0 | grep -o -- 'i-')
        if [ -z $locked ]; then
            lockstate="unlocked"
        else
            lockstate="locked"  
        fi       

        # display window
        yad 2>/dev/null --title="Icon Lockstate" \
            --borders=10 \
            --text-align=center \
            --text="<b>Icon layout is $lockstate</b>\n" \
            --button="Lock":1 \
            --button="Unlock":2 \
            --button="Cancel":0 
            ret=$?
            # set attr for desktop0
            if [ "$ret" == 1 ]; then
                desktop_icons_lock
                $APPDIR/desktop-icons
                exit
            elif [ "$ret" == 2 ]; then
                desktop_icons_unlock
                $APPDIR/desktop-icons
                exit
            fi     
            $APPDIR/desktop-icons
            exit
    elif [ "$ret" == 6 ]; then
        # drive icon status
        yad 2>/dev/null --title="Drive Icons" \
            --borders=10 \
            --text-align=center \
            --text="\n <b>Configure drive icons</b>\n" \
            --button="On/Off":1 \
            --button="Labels":2 \
            --button="Position on Screen":3 \
            --button="Refresh Screen":4 \
            --button="Cancel":0 
            ret=$?
            if [ "$ret" == 1 ]; then
                # check if drive icons are on/off
                [ -f ~/.config/zzzfm/particons_off ] && driveicons=off || driveicons=on
                if [ "$driveicons" = "off" ]; then
                    drive_icons="Disabled"
                else
                    drive_icons="Enabled"  
                fi 
                # display window
                yad 2>/dev/null --title="Drive Icons On/Off" \
                --borders=10 \
                --text-align=center \
                --text="\n Drives icons are currently <b>$drive_icons</b>\n\nThe Enable button restarts Wayland\n" \
                --button="Enable":1 \
                --button="Disable":2 \
                --button="Cancel":0 
                ret=$?
                if [ "$ret" == 1 ]; then
                    rm ~/.config/zzzfm/particons_off
                    restartwm
                    particons
                    exit
                elif [ "$ret" == 2 ]; then
                    touch ~/.config/zzzfm/particons_off
                    rm ~/Desktop/particon*.desktop
                    $APPDIR/desktop-icons
                    exit
                fi
                $APPDIR/desktop-icons
                exit 
            elif [ "$ret" == 2 ]; then
                # check if drive labels are enabled
                touch ~/.config/zzzfm/drive-labels
                . ~/.config/zzzfm/drive-labels
                if [ "$DRIVE_LABELS" = "true" ]; then
                    drive_labels="Enabled"
                else
                    drive_labels="Disabled"  
                fi 
                # display window
                yad 2>/dev/null --title="Drive Labels" \
                --borders=10 \
                --text-align=center \
                --text="\n Drives labels are currently <b>$drive_labels</b>\n" \
                --button="Enable":1 \
                --button="Disable":2 \
                --button="Cancel":0
                ret=$?
                if [ "$ret" == 1 ]; then
                    echo "DRIVE_LABELS=true" > ~/.config/zzzfm/drive-labels
                    particons
                    $APPDIR/desktop-icons
                    exit
                elif [ "$ret" == 2 ]; then
                    echo "DRIVE_LABELS=false" > ~/.config/zzzfm/drive-labels
                    particons
                    $APPDIR/desktop-icons
                    exit
                fi
                $APPDIR/desktop-icons
                exit
            elif [ "$ret" == 3 ]; then
                # set screen position of drive icons
                drive_icon_position
                exit
            elif [ "$ret" == 4 ]; then
                # rescan screen
                rm -f ~/.config/zzzfm/row-calc ~/.config/zzzfm/row-sel
                particons
                desktop_icons_dialog
                exit
            fi       
            $APPDIR/desktop-icons
            exit
    elif [ "$ret" == 7 ]; then
        # save or restore icon layout
        yad 2>/dev/null --title="Save/Restore" \
            --borders=10 \
            --text-align=center \
            --text="<b>Save or Restore icon layout</b>\n" \
            --button="Save":1 \
            --button="Restore":2 \
            --button="Cancel":0 
            ret=$?
            if [ "$ret" == 1 ]; then
                mkdir -p ~/.config/spacefm/backups/Desktop
                touch ~/.config/spacefm/backups/desktop0
                grep -v '=particon' ~/.config/zzzfm/desktop0 > ~/.config/spacefm/backups/desktop0
                cp -f ~/Desktop/* ~/.config/spacefm/backups/Desktop
                rm -f ~/.config/spacefm/backups/Desktop/particon*.desktop
                yad 2>/dev/null --title="Backup" \
                --borders=10 \
                --center \
                --text " \n          <b>Icon layout saved !</b>          \n" \
                --no-buttons \
                --timeout 2
                $APPDIR/desktop-icons
                exit
            elif [ "$ret" == 2 ]; then
                desktop_icons_unlock
                cp -f ~/.config/spacefm/backups/desktop0 ~/.config/zzzfm/desktop0
                rm -f ~/Desktop/*
                cp -f  ~/.config/spacefm/backups/Desktop/* ~/Desktop
                desktop_icons_lock
                particons
                $APPDIR/desktop-icons
                exit
            fi     
            $APPDIR/desktop-icons
            exit
    elif [ "$ret" == 8 ]; then
        # help dialog
        mdview ~/.config/spacefm/docs/help.md
        $APPDIR/desktop-icons
        exit
    fi
    exit
