#!/bin/sh

PRGNAM=plasma-workspace-data-gernot
VERSION=2025.08.15
BUILD=1
ARCH=all

HOMEPAGE="https://gernot-walzl.at/"

MAINTAINER="Gernot Walzl"
EMAIL="gernot.walzl@gmx.at"
DATE="2025-08-15"

DESC="KDE Plasma - Look and Feel - Gernot's Breeze
 Breeze Twilight adapted by Gernot Walzl"


# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# exit on error
set -e

# install
rm -rf "$PKG"
mkdir -p "$PKG/usr/share/doc/$PRGNAM"

ORIGIN_DESKTOP="/usr/share/plasma/look-and-feel/org.kde.breezetwilight.desktop"
ORIGIN_PANEL="/usr/share/plasma/layout-templates/org.kde.plasma.desktop.defaultPanel/contents/layout.js"

PKG_DESKTOP="$PKG/usr/share/plasma/look-and-feel/at.gernot-walzl.desktop"
mkdir -p "$PKG_DESKTOP"
cat > "$PKG_DESKTOP/metadata.json" <<EOF
{
    "KPackageStructure": "Plasma/LookAndFeel",
    "KPlugin": {
        "Authors": [
            {
                "Email": "$EMAIL",
                "Name": "$MAINTAINER"
            }
        ],
        "Category": "",
        "Description": "Breeze Twilight adapted by Gernot Walzl",
        "Id": "at.gernot-walzl.desktop",
        "License": "GPLv2+",
        "Name": "Gernot's Breeze",
        "ServiceTypes": [
            "Plasma/LookAndFeel"
        ],
        "Version": "2.0",
        "Website": "$HOMEPAGE"
    },
    "X-Plasma-MainScript": "defaults"
}
EOF

mkdir -p "$PKG_DESKTOP/contents"
sed 's/^ColorScheme=BreezeLight$/ColorScheme=BreezeClassic/' \
  "$ORIGIN_DESKTOP/contents/defaults" \
  > "$PKG_DESKTOP/contents/defaults"
cp -r "$ORIGIN_DESKTOP/contents/previews" "$PKG_DESKTOP/contents"

mkdir -p "$PKG_DESKTOP/contents/layouts"
sed 's/org.kde.plasma.icontasks/org.kde.plasma.taskmanager/' "$ORIGIN_PANEL" \
  > "$PKG_DESKTOP/contents/layouts/org.kde.plasma.desktop-layout.js"
cat >> "$PKG_DESKTOP/contents/layouts/org.kde.plasma.desktop-layout.js" <<EOF
var widgets = panel.widgets();
for (var idx = 0; idx < widgets.length; idx++) {
    var widget = widgets[idx];
    if (widget.type == "org.kde.plasma.digitalclock") {
        widget.currentConfigGroup = ["Appearance"];
        widget.writeConfig("dateFormat", "isoDate");
    }
}

var desktops = desktopsForActivity(currentActivity());
for (var idx = 0; idx < desktops.length; idx++) {
    var desktop = desktops[idx];
    desktop.wallpaperPlugin = "org.kde.slideshow";
    desktop.currentConfigGroup = ["Wallpaper", "org.kde.slideshow", "General"];
    desktop.writeConfig("SlideInterval", 3600);
    desktop.writeConfig("SlidePaths", "/usr/share/wallpapers/");
    if (idx == 0) {
        var weather = desktop.addWidget("org.kde.plasma.weather");
        weather.currentConfigGroup = ["WeatherStation"];
        weather.writeConfig("updateInterval", 60);
        weather.writeConfig("source", "wettercom|weather|Graz, Steiermark, AT|ATAT20125;Graz");
    }
}
EOF

# copy build script
cat "$CWD/$0" > "$PKG/usr/share/doc/$PRGNAM/$PRGNAM.DebBuild"

# changelog
cat > "$PKG/usr/share/doc/$PRGNAM/changelog.Debian" <<EOF
$PRGNAM ($VERSION-$BUILD) unstable; urgency=medium

  * Created a Debian package.

 -- $MAINTAINER <$EMAIL>  $(date -d$DATE -R)
EOF
gzip -n9 "$PKG/usr/share/doc/$PRGNAM/changelog.Debian"

# copyright
cat > "$PKG/usr/share/doc/$PRGNAM/copyright" <<EOF
Files: *
Copyright: 2006-2021, KDE Visual Design Group <plasma-devel@kde.org>
           2024, $MAINTAINER <$EMAIL>
License: GPL-2+
EOF

# DEBIAN/control
mkdir -p "$PKG/DEBIAN"
cat > "$PKG/DEBIAN/control" <<EOF
Package: $PRGNAM
Version: $VERSION-$BUILD
Section: kde
Priority: optional
Architecture: $ARCH
Depends: plasma-workspace-data
Installed-Size: $(du -s "$PKG" | cut -f1)
Maintainer: $MAINTAINER <$EMAIL>
Description: $DESC
Homepage: $HOMEPAGE
EOF

# DEBIAN/md5sums
cd "$PKG"
find . -type d -name 'DEBIAN' -prune -o -type f -printf '%P\0' \
  | sort -z | xargs -0 md5sum \
  > "$PKG/DEBIAN/md5sums"

# build a deb
cd "$TMP"
fakeroot dpkg-deb -b "$PKG" "$OUTPUT"

# clean
cd "$CWD"
rm -rf "$PKG"
