#!/bin/bash
# Creates an "Install SolydXK" icon on the live session tested with:
# Xfce, LXDE, LXqt, Gnome, KDE, Mate, Cinnamon

# We query xdg-user-dir because the Desktop directory has different
# names for different languages
DESKTOP=$(xdg-user-dir DESKTOP)

# Variables
CUR_DATE=$(date +%m%d)
DT=${XDG_CURRENT_DESKTOP:0:1}
DT=${DT,,}
YEARS=$(($(date +%Y) - 2013))

# Create ~/Desktop just in case this runs before the xdg folder
# creation script.
mkdir -p "$DESKTOP"

# Copy a .desktop file to the desktop
function copy_desktop() {
    ORG=$1
    [ ! -e "$ORG" ] && exit 0
    
    DEST="$DESKTOP/$(basename $ORG)"
    echo "Create $DEST"
    
    if [ ! -z "$(which pcmanfm-qt)" ]; then
        # Among the SolydXK desktop environments, LXDE is the only one that uses 
        # desktop links on the desktop pointing to the actual desktop files...
        cat > "$DEST" << EOF
[Desktop Entry]
Type=Link
$(grep ^Name= $ORG)
$(grep ^Icon= $ORG)
URL=$ORG
EOF
    else
        cp "$ORG" "$DEST"
        chmod g+w,a+x "$DEST"
        # Xfce: prevent Untrusted Application Launcher dialog
        gio set -t string "$DEST" metadata::xfce-exe-checksum $(sha256sum "$DEST" | awk '{print $1}')
        gio set -t string "$DEST" metadata::trust "true"
    fi
}

function copy_w95() {
    W95DTS=$(ls /usr/share/calamares-settings-solydxk/w95/*.desktop | grep -E "[a-z]{2}\.desktop|_${DT}\.desktop")
    for W95DT in $W95DTS; do
        copy_desktop "$W95DT"
    done
}

function show_years() {
    [ -z "$(which zenity)" ] && exit 0
    sleep 5
    
    ICON_PATH=$(dpkg -S solydxk.svg | head -n 1 | cut -d' ' -f 2)
    zenity \
    --info \
    --window-icon="$ICON_PATH" \
    --icon-name=solydxk \
    --text="<span size=\"x-large\">Today is SolydXK's ${YEARS}th birthday.</span>\n\nTo celebrate this occasion we show you the wallpaper we used in our first release.\n\nThank you for using <b>SolydXK</b>." \
    --title="SolydXK $YEARS years old"
}

function set_xfce_wallpaper {
    # Check if image exists and xfconf-query is installed
    BG=$1
    if [ ! -e "$BG" ] || [ -z "$(which xfconf-query)" ]; then
        return
    fi
    
    # Wait until workspace has been created   
    MONITOR=$(LANG=C xrandr | grep " connected" | awk '{print $1}')
    CNT=0
    while [ -z "$MON_CHECK" ]; do
        MON_CHECK=$(xfconf-query -c xfce4-desktop -l | grep -E -i "$MONITOR/workspace.*/last-image")
        CNT=$((CNT+1))
        [ $CNT -gt 5 ] && break
        [ -z "$MON_CHECK" ] && sleep 1
    done
    
    # Set background image
    for MON in $MON_CHECK; do
        echo "Set $MON: $BG"
        xfconf-query --channel xfce4-desktop --property $MON --set "$BG"
        xfconf-query --channel xfce4-desktop --property ${MON%/*}/image-style --set 5
    done
}

# Copy the install SolydXK desktop file to the desktop
copy_desktop "/usr/share/applications/solydxk-installer.desktop"

# Easter Egg :)
# Change desktop on certain dates
BG="/usr/share/calamares-settings-solydxk/${CUR_DATE}-${DT}.png"
if [ ! -e "$BG" ]; then
    BG="/usr/share/calamares-settings-solydxk/${CUR_DATE}-a.png"
fi
if [ ! -e "$BG" ]; then
    exit 0
fi

set_xfce_wallpaper "$BG"
if [ ! -z "$(which plasma-apply-wallpaperimage)" ]; then
    # KDE Plasma
    plasma-apply-wallpaperimage "$BG"
elif [ ! -z "$(which pcmanfm-qt)" ]; then
    # Lxqt
    pcmanfm-qt --set-wallpaper "$BG" --wallpaper-mode=stretch
fi
[ $CUR_DATE == 0401 ] && copy_w95
[ $CUR_DATE == 0228 ] && show_years
