#!/bin/bash 
#
# This file is part of the ALT project, module Alt-Customize-Branding.
# Copyright (c) BaseALT Ltd 2020 Pavel Moseev <mars@altlinux.org>
#
# This is free software; you can use this application under the GNU
# General Public License, version 3. See the file "COPYING" for the
# exact licensing terms.

export PATH=$PATH:/sbin:/usr/sbin
appname=alt-customize-branding
imgTempFolder="/tmp/$appname"
imgFolder="/var/lib/$appname"
defaultBranding=branding-xalt-kworkstation

if [ -f $imgFolder/alt-customize-branding-settings.ini ]; then
    # 0. Save name of previous theme if exists
    prevThemeName=$(awk -F "=" '/ThemeName/ {print $2}' $imgFolder/alt-customize-branding-settings.ini)
    echo "Previous theme: " $prevThemeName
fi

if [ -d "$imgTempFolder" ]; then
    # 1. Copy all files from "/tmp/$appname/" dir to "/var/lib/$appname/"
    rm -rf $imgFolder/*
    cp -ar $imgTempFolder/. $imgFolder/
    # 2. Delete "/tmp/$appname/":
    rm -R $imgTempFolder
else
    echo "Directory $imgTempFolder not exists!"
fi

# 3. Read config file '/var/lib/$appname/alt-customize-branding-settings.ini':
if [ -f $imgFolder/alt-customize-branding-settings.ini ]; then
    bigPngImage=$(awk -F "=" '/BigPngImage/ {print $2}' $imgFolder/alt-customize-branding-settings.ini)
    resizedPngImage=$(awk -F "=" '/ResizedPngImage/ {print $2}' $imgFolder/alt-customize-branding-settings.ini)
    resizedJpgImage=$(awk -F "=" '/ResizedJpgImage/ {print $2}' $imgFolder/alt-customize-branding-settings.ini)
    echo "bigPngImage: " $bigPngImage
    echo "resizedPngImage: " $resizedPngImage
    echo "resizedJpgImage: " $resizedJpgImage
else
    bigPngImage="newimage.png"
    resizedPngImage="newimage800x600.png"
    resizedJpgImage="newimage800x600.jpg"
fi
# Try to write previous theme name to .ini-file
sed -i "s/ThemeName=.*/ThemeName=$prevThemeName/" $imgFolder/alt-customize-branding-settings.ini

# 4. Copy design directory:
if [ -d "/usr/share/design/$appname" ]; then
    echo "Design directory already exists!"
    # and it is not empty!"
    if [ -z "$(ls -A /usr/share/design/$appname)" ]; then
        echo "Design directory is empty."
        echo "Start copying design directory."
        cp -ar /usr/share/design/current/. /usr/share/design/$appname/
    fi
else
    echo "Start copying design directory."
    cp -ar /usr/share/design/current/. /usr/share/design/$appname/
fi

# 5. Copy grub dirs:
# Read file "/boot/grub/grub.cfg"
# Find string "set theme=($root)/boot/grub/themes/%theme/theme.txt"
# Read %theme name
if [[ -z $prevThemeName ]]; then
    while IFS= read -r line
    do
        if [[ "$line" == *"set theme="* ]]; then
            str1='set theme=($root)/boot/grub/themes/'
            str3='/theme.txt'
            str2len=$((${#line}-${#str1}-${#str3}))
            prevThemeName=${line:${#str1}:$str2len}
        fi
    done < /boot/grub/grub.cfg
fi

prevThemeName=${prevThemeName:-$defaultBranding}

# Copy all files from this dir to new dir:
if [[ "$prevThemeName" == "$appname" ]]; then
    echo "This grub theme already exists!"
else
    cp -ar /boot/grub/themes/$prevThemeName/. /boot/grub/themes/$appname/
    sed -i "s/ThemeName=.*/ThemeName=$prevThemeName/" $imgFolder/alt-customize-branding-settings.ini
fi

# File "/boot/grub/grub.cfg" - automatically generated by grub-mkconfig
# using templates from /etc/grub.d and settings from /etc/sysconfig/grub2
. shell-config
shell_config_set /etc/sysconfig/grub2 GRUB_THEME /boot/grub/themes/$appname/theme.txt
shell_config_set /etc/sysconfig/grub2 GRUB_COLOR_NORMAL white/light-blue
shell_config_set /etc/sysconfig/grub2 GRUB_COLOR_HIGHLIGHT black/light-gray
shell_config_set /etc/sysconfig/grub2 GRUB_BACKGROUND /boot/grub/themes/$appname/grub.png
# generate file "/boot/grub/grub.cfg"
/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg

# 6. Copy plymouth dirs:
#Copy files from Plymouth dir to new theme dir
if [[ "$prevThemeName" == "$appname" ]]; then
    echo "This plymouth theme already exists!"
else
    cp -ar /usr/share/plymouth/themes/$prevThemeName/. /usr/share/plymouth/themes/$appname/
fi

# Rename /usr/share/plymouth/themes/$appname/$prevThemeName.plymouth to /usr/share/plymouth/themes/$appname/$appname.plymouth
oldplyname="/usr/share/plymouth/themes/$appname/$prevThemeName.plymouth"
newplyname="/usr/share/plymouth/themes/$appname/$appname.plymouth"
if [[ -f $oldplyname && ! -f $newplyname ]]; then
    mv $oldplyname $newplyname
else
    rm -f $oldplyname
fi

# if file exists:
# Modify file /usr/share/plymouth/themes/$appname/$appname.plymouth
if [ -f $newplyname ]; then
    sed -i -e "s|ImageDir=.*|ImageDir=/usr/share/plymouth/themes/$appname|" /usr/share/plymouth/themes/$appname/$appname.plymouth
    sed -i -e "s|ScriptFile=.*|ScriptFile=/usr/share/plymouth/themes/$appname/theme.script|" /usr/share/plymouth/themes/$appname/$appname.plymouth
else
    echo " *.plymouth file not exists!"
fi

# Change theme name in file 'plymouthd.conf':
[ -f "/etc/plymouth/plymouthd.conf" ] && sed -i "s/Theme=.*/Theme=$appname/" /etc/plymouth/plymouthd.conf || :

# 7. Save images:
#grub. Img Resized to 800x600 pix
cp $imgFolder/$resizedPngImage /boot/grub/themes/$appname/grub.png # newimage800x600.png
cp $imgFolder/$resizedJpgImage /boot/grub/themes/$appname/grub.jpg # newimage800x600.jpg
cp $imgFolder/$resizedPngImage /boot/grub/themes/$appname/boot.png # newimage800x600.png
cp $imgFolder/$resizedJpgImage /usr/share/plymouth/themes/$appname/grub.jpg
#design
cp $imgFolder/$bigPngImage /usr/share/design/$appname/backgrounds/xdm.png
cp $imgFolder/$bigPngImage /usr/share/design/$appname/backgrounds/default.png
cp $imgFolder/$bigPngImage /usr/share/design/$appname/backgrounds/default-wide.png
#plymouth
cp $imgFolder/$bigPngImage /usr/share/plymouth/themes/$appname/background16x9.png #newimage.png
cp $imgFolder/$resizedPngImage /usr/share/plymouth/themes/$appname/background4x3.png #newimage800x600.png

# 8. Change desktop wallpaper:
location="/usr/share/design/$appname/backgrounds"
wallpaper=default.png

dbus-send --session --dest=org.kde.plasmashell --type=method_call /PlasmaShell org.kde.PlasmaShell.evaluateScript 'string:
var Desktops = desktops();
for (i=0;i<Desktops.length;++i) {
    d = Desktops[i];
    d.wallpaperPlugin = "org.kde.image";
    d.currentConfigGroup = Array("Wallpaper",
                                 "org.kde.image",
                                 "General");
    d.writeConfig("Image", "file://'''$location'''/'${wallpaper}'");
}'

# 9. Alternatives:
alternativesFile="/etc/alternatives/packages.d/$appname" # Create new file
if [ -f $alternativesFile ]; then
    #rm -f $alternativesFile
    echo -n > $alternativesFile
    echo "Alternatives already exists!"
fi
echo "/usr/share/design/current	/usr/share/design/$appname 	000014000000" >> $alternativesFile

# 10. Update alternatives:
alternatives-update

# 11. make-initrd:
make-initrd
