#!/bin/bash

MODULENAME=003-kde-5.23.5

source "$PWD/../builder-utils/setflags.sh"

SetFlags "$MODULENAME"

source "$BUILDERUTILSPATH/cachefiles.sh"
source "$BUILDERUTILSPATH/downloadfromslackware.sh"
source "$BUILDERUTILSPATH/genericstrip.sh"
source "$BUILDERUTILSPATH/helper.sh"

if ! isRoot; then
	echo "Please enter admin's password below:"
	su -c "$0 $1"
	exit
fi

### create module folder

mkdir -p $MODULEPATH/packages > /dev/null 2>&1
cd $MODULEPATH

### download packages from slackware repository

DownloadFromSlackware

### packages that require specific stripping

currentPackage=qt5
mkdir $MODULEPATH/${currentPackage} && cd $MODULEPATH/${currentPackage}
mv $MODULEPATH/packages/${currentPackage}-[0-9]* .
installpkg qt5*.txz || exit 1
packageFileName=$(ls * -a | rev | cut -d . -f 2- | rev)
ROOT=./ installpkg ${currentPackage}-*.txz
mkdir ${currentPackage}-stripped
cp --parents -P usr/lib$SYSTEMBITS/libQt5Concurrent.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Core.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5DBus.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5DesignerComponents.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Designer.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5EglFSDeviceIntegration.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5EglFsKmsSupport.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Gui.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Help.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Multimedia.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Network.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5OpenGL.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5PrintSupport.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5QmlModels.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Qml.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5QmlWorkerScript.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5QuickControls2.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5QuickParticles.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5QuickShapes.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Quick.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5QuickTemplates2.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5QuickTest.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5QuickWidgets.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Script.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5ScriptTools.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Sensors.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5SerialPort.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Sql.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Svg.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Test.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5TextToSpeech.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5WaylandClient.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5WaylandCompositor.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5WebChannel.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Widgets.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5X11Extras.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5XcbQpa.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5XmlPatterns.* "${currentPackage}-stripped"
cp --parents -P usr/lib$SYSTEMBITS/libQt5Xml.* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/bin/qdbus "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/bearer/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/egldeviceintegrations/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/generic/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/iconengines/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/imageformats/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/platforminputcontexts/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/platforms/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/printsupport/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/qmltooling/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/sensors/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/sqldrivers/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/texttospeech/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/wayland-decoration-client/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/wayland-graphics-integration-client/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/wayland-graphics-integration-server/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/wayland-shell-integration/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/plugins/xcbglintegrations/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/Qt/labs/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtAudioEngine/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtGraphicalEffects/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtMultimedia/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtQml/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtQuick/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtQuick.2/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtSensors/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtTest/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtWayland/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtWebChannel/* "${currentPackage}-stripped"
cp --parents -R usr/lib$SYSTEMBITS/qt5/qml/QtWebSockets/* "${currentPackage}-stripped"
rm ${currentPackage}-stripped/usr/lib$SYSTEMBITS/*.prl
cd ${currentPackage}-stripped
makepkg ${MAKEPKGFLAGS} $MODULEPATH/packages/${packageFileName}_stripped.txz > /dev/null 2>&1
rm -fr $MODULEPATH/${currentPackage}

### packages outside slackware repository

# required by featherpad
installpkg $MODULEPATH/packages/hunspell*.txz || exit 1

currentPackage=featherpad
sh $SCRIPTPATH/extras/${currentPackage}/${currentPackage}.SlackBuild || exit 1
rm -fr $MODULEPATH/${currentPackage}

currentPackage=audacious
QT=5 sh $SCRIPTPATH/../common/audacious/${currentPackage}.SlackBuild || exit 1
installpkg $MODULEPATH/packages/${currentPackage}*.txz
rm -fr $MODULEPATH/${currentPackage}

currentPackage=audacious-plugins
QT=5 sh $SCRIPTPATH/../common/audacious/${currentPackage}.SlackBuild || exit 1
rm -fr $MODULEPATH/${currentPackage}

# kde deps
for package in \
	extra-cmake-modules \
	kimageformats \
; do
sh $SCRIPTPATH/deps/${package}/${package}.SlackBuild || exit 1
installpkg $MODULEPATH/packages/${package}-*.txz || exit 1
find $MODULEPATH -mindepth 1 -maxdepth 1 ! \( -name "packages" \) -exec rm -rf '{}' \; 2>/dev/null
done

# only required for building
rm $MODULEPATH/packages/extra-cmake-modules*.txz

### fake root

cd $MODULEPATH/packages && ROOT=./ installpkg *.t?z
rm *.t?z

### install additional packages, including porteux utils

InstallAdditionalPackages

### copy wallpaper

mv $MODULEPATH/packages/usr/share/kf5/infopage/body-background.png $MODULEPATH/packages/usr/share/wallpapers

### fix some .desktop files

sed -i "s|Graphics;||g" $MODULEPATH/packages/usr/share/applications/org.kde.okular.desktop
sed -i "s|image/png|image/png;image/jxl|g" $MODULEPATH/packages/usr/share/applications/org.kde.gwenview.desktop

### copy build files to 05-devel

CopyToDevel

### copy language files to 08-multilanguage

CopyToMultiLanguage

### module clean up

cd $MODULEPATH/packages/

{
rm usr/bin/systemmonitor
rm usr/share/applications/org.kde.dolphinsu.desktop
rm usr/share/applications/org.kde.plasma.emojier.desktop
rm usr/share/icons/breeze/breeze-icons.rcc
rm usr/share/icons/breeze-dark/breeze-icons-dark.rcc
rm usr/share/plasma/avatars/*

rm -fr etc/kde/xdg/autostart/baloo_file.desktop
rm -fr etc/kde/xdg/autostart/kaccess.desktop
rm -fr etc/kde/xdg/autostart/xembedsniproxy.desktop
rm -fr usr/share/chromium
rm -fr usr/share/emoticons/EmojiOne
rm -fr usr/share/featherpad
rm -fr usr/share/gdb
rm -fr usr/share/gdm
rm -fr usr/share/gnome
rm -fr usr/share/google-chrome
rm -fr usr/share/icons/breeze_cursors
rm -fr usr/share/icons/Breeze_Snow
rm -fr usr/share/icons/KDE_Classic
rm -fr usr/share/icons/Oxygen_Black
rm -fr usr/share/icons/Oxygen_Blue
rm -fr usr/share/icons/Oxygen_White
rm -fr usr/share/icons/Oxygen_Yellow
rm -fr usr/share/icons/Oxygen_Zion
rm -fr usr/share/katepart*
rm -fr usr/share/kde4
rm -fr usr/share/kf*/kdoctools
rm -fr usr/share/kf*/locale
rm -fr usr/share/ksplash/Themes/Classic
rm -fr usr/share/phonon*
rm -fr usr/share/plasma/desktoptheme/air
rm -fr usr/share/plasma/desktoptheme/oxygen
rm -fr usr/share/plasma/emoji
rm -fr usr/share/plasma/look-and-feel/org.kde.oxygen
rm -fr usr/share/plasma/nightcolor
rm -fr usr/share/sddm/themes/breeze/preview*
rm -fr usr/share/sddm/themes/elarun
rm -fr usr/share/sddm/themes/maldives
rm -fr usr/share/sddm/themes/maya
rm -fr usr/share/sddm/translations
rm -fr usr/share/themes/Breeze-Dark/gtk-4.0
rm -fr usr/share/themes/Breeze/gtk-4.0
rm -fr usr/share/wallpapers/Next

[ "$SYSTEMBITS" == 64 ] && find usr/lib/ -mindepth 1 -maxdepth 1 ! \( -name "python*" \) -exec rm -rf '{}' \; 2>/dev/null
find usr/share/plasma/avatars/photos -mindepth 1 ! \( -name "Air Balloon.png" -o -name "Air Balloon.png.license" -o -name "Astronaut.png" -o -name "Astronaut.png.license" \) -exec rm -rf '{}' \; 2>/dev/null
} >/dev/null 2>&1

GenericStrip

# move out things that don't support aggressive stripping
mv $MODULEPATH/packages/usr/lib${SYSTEMBITS}/libgwenviewlib.so* $MODULEPATH/
AggressiveStripAll
mv $MODULEPATH/libgwenviewlib.so* $MODULEPATH/packages/usr/lib${SYSTEMBITS}

### copy cache files

PrepareFilesForCacheDE

### generate cache files

GenerateCachesDE

### kde specific mime cache

rm -fr $PORTEUXBUILDERPATH/caches/mime/packages
cp -r $PORTEUXBUILDERPATH/caches/mime $MODULEPATH/packages/usr/share/

### finalize

Finalize
