#!/bin/bash
# This script was auto generated using the code-cleanup package http://forumubuntusoftware.info/viewtopic.php?f=23&t=9480
# Do a search and replace for ultimateedition.info followed by a search and replace REPLACEME with the name of your program & delete this line.

# ==============================================================================
# title			:REPLACEME
# description	:YOURDESCRIPTION
# author		:theemahn <theemahn@theemahn-msa2>
# date			:02/03/2021
# version		:2.0.7
# usage			:REPLACEME --help
# manual		:man REPLACEME
# notes			:See change-log below for further information.
# ==============================================================================
# Change-log: 1.0: unreleased
# ==============================================================================

# Modify the below information to correlate with the software you are designing.
# Version Control
APPNAME="TheeMahn's Theme Builder"
PROGNAME="uetheme"
VERSION="2.1.4"
BUILDDATE="06/29/2025"
WEBSITE="os-builder.com"
AUTHOR="TheeMahn"
EMAIL="<$AUTHOR@${WEBSITE}>"
if [[ -f "ultimate-common" ]]; then
	source ultimate-common
elif [[ -f "/usr/share/ultimate_edition/ultimate-common" ]]; then
	source /usr/share/ultimate_edition/ultimate-common
else
	echo "No Ultimate Edition common source. Please install ultimate-edition-common."
	if [[ "${ISARCH}" ]]; then
		Encapsulate "sudo pacman -Syuu ultimate-edition-common"
	else
		Encapsulate "sudo apt-get install ultimate-edition-common"
	fi
	exit 0;
fi

Exit () {
	if ! [[ "${SUPPRESS}" ]]; then
		Timer "Stop" "${APPNAME}"
		FullBar
	fi
}

trap Exit EXIT

# Turn off end user prompting non Arch
export DEBIAN_FRONTEND=noninteractive
# Logging output - Currently un-implemented
# exec 1> /var/log/tmosb-session.log
# exec 1>> /var/log/tmosb-session.log 2>&1
LOG_FILE="/var/log/tmosb.log"
# exec 1 | tee ${LOG_FILE}
# exec 2 | tee ${LOG_FILE}

# Will I utilize this?  To be determined.
#declare -a DISKSIZES=('KB' 'MB' 'GB' 'TB' 'PB' 'EB' 'ZB' 'YB')
declare -i USPLASH
#declare -i KBS

#Pull info we will use to compare against later
IUSER=${SUDO_USER:-$USER}
IHOME="/home/${IUSER}"

CORE=$(echo "$*" | grep -i "core")
# Declare Integer for root user detection - Set as initial normal user.
declare -i TRUEROOT
TRUEROOT=1000 #Set as 1000 initially, try an cause root access to fail.
IUSER="${SUDO_USER:-${USER}}"
#IHOME="/home/${IUSER}"
TRUEROOT="$(id -u)"

# Multiple OS builds?
declare -i NUMBERTOBUILD
STAGE="${PWD}"

# Quoting...
SINGLE="'"
#QUOTE='"'

# Not required, but optional. No prompting for testing etc. if false.
PROMPT=$(echo "$*" | grep -i "yes")
BUILDALL=$(echo "$*" | grep -i "buildall")

# Get Directory executed in.
CURRENT="${PWD}"

# Set some internal variables and arrays
OS='ultimate-edition'
COS="Ultimate_Edition"
UOS="Ultimate Edition"

#CACHINGFOLDERTEMP="/root/${PROGNAME}/.cache/tmp"
CACHETEST=$(echo "$@" | grep -i "cache=")
REPOSTORMDEFAULT=$(echo "$*" | grep -i "repostorm")
THEMEFOLDER="http://os-builder.com/themes"
ROOTFOLDER="http://os-builder.com/UETOYS"

finish () {
	if [[ "${DEBUG}" ]]; then
		echo "EUID: ${EUID} | USER: ${USER} | IUSER: ${IUSER} | ROOT: ${TRUEROOT} EXIT CODE: $?"
	fi
}
INFOLDER="${PWD}"
# trap ctrl-c and call ctrl_c()
ctrl_c () {
		Encapsulate "** Trapped CTRL-C, exiting."
		RUNNING=$(ps -C repostorm | wc -l)
	   if [[ "${RUNNING}" -gt 1 ]]; then
			Encapsulate "Killing all ${RUNNING} instances of repostorm."
   			sudo killall -9 repostorm
   		fi
   		RUNNING=$(ps -C uetheme | wc -l)
		if [[ "${RUNNING}" -gt 1 ]]; then
			Encapsulate "Killing all ${RUNNING} instances of uetheme."
   			sudo killall -9 uetheme
   		fi
		FullBar
		finish
		exit 1;
}

trap ctrl_c INT
trap finish EXIT
WALLLOCATIONS=();
COMMANDISSUED=$(echo "$*" | tr '[:upper:]' '[:lower:]')
WALLPAPER=$(echo "${COMMANDISSUED}" | grep -i "wallpaper=")
COMMANDISSUED=$(echo "$*")
if [[ "${WALLPAPER}" ]]; then
	WALLPAPER=$(echo "${COMMANDISSUED}" | grep -i 'wallpaper=' | sed 's/^.*wallpaper=//g' | cut -d' ' -f1)
	if [[ "${WALLPAPER}" ]]; then
		if ! [[ "${SUPPRESS}" ]]; then
			Encapsulate "Wallpaper specified as: ${WALLPAPER}"
		fi
	fi
fi
COMMANDISSUED=$(echo "$*" | tr '[:upper:]' '[:lower:]')
THEME=$(echo "${COMMANDISSUED}" | grep -i "theme=")
COMMANDISSUED=$(echo "$*")
PlymouthTheme () {
	Encapsulate "Work in progress"
}

BuildDebs () {
	INFOLDER="${PWD}"
	THEMEVERSION="${2}"
	for EACH in "${THEMETYPES[@]}"
	do
		if [[ -d "Ultimate Edition ${THEMEVERSION}/${EACH}" ]]; then
			#Encapsulate "Building ${EACH}..."
			cd "Ultimate Edition ${THEMEVERSION}/${EACH}" || exit 1;
			if ! [[ "${SUPPRESS}" ]]; then
				sudo repostorm --build --suppress -YES
			else
				#Encapsulate "Building all deb(s) in ${PWD}"
				NOOUT=$(sudo repostorm --build --suppress -YES 2&1>/dev/null)
			fi
			if ! [[ "${SUPPRESS}" ]]; then
				Encapsulate "Repairing Deb."
				sudo repostorm -f --suppress -YES
			else
				#Encapsulate "Repairing Deb(s) in ${PWD}."
				NOOUT=$(sudo repostorm -f --suppress -YES 2&1>/dev/null)
			fi
			if ! [[ "${SUPPRESS}" ]]; then
				sudo repostorm --build -YES
			else
				#Encapsulate "Building all deb(s) in ${PWD}. Build #2"
				NOOUT=$(sudo repostorm --build --suppress -YES  2&1>/dev/null)
				sync;
			fi
			cd "${INFOLDER}" || exit 1;
		fi
	done
	#if [[ -d "Ultimate Edition ${THEMEVERSION}" ]]; then
	#	#Encapsulate "Scanning for successful builds."
	#	cd "Ultimate Edition ${THEMEVERSION}" || exit 1;
	#	find . -name "*.deb" -type f -exec mv {} . \;
	#	Timer "Stop"
	#	FullBar
	#fi
}

Aurorae () {
	Encapsulate "Work in progress"
}

Wallpapers () {
	INFOLDER="${PWD}"
	FLDR="Ultimate Edition ${THEMEVERSION}"
	DEBFOLDER="${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/"
	if ! [[ "${WALLPAPER}" ]]; then
		Error "No wallpaper specified."
		Encapsulate "Example: uetheme --setup 6.6 wallpaper=wallpaper.png theme=themefolder"
		FullBar
		exit 1;
	fi
	if [[ -f "${WALLPAPER}" ]]; then
		INDEX=0
		cp "${WALLPAPER}" "${FLDR}/grub/ultimate-edition-grub-${THEMEVERSION}_all/boot/grub/themes/Ultimate_Edition_${THEMEVERSION}/background.png"
		cd "${FLDR}/grub/ultimate-edition-grub-${THEMEVERSION}_all/boot/grub/themes/Ultimate_Edition_${THEMEVERSION}/" || exit 1;
		ln -s "background.png" "suspend.png"
		cd "${INFOLDER}" || exit 1;
		cp "${WALLPAPER}" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/wallpapers/Ultimate_Edition_${THEMEVERSION}.png"
		cp "${WALLPAPER}" "${FLDR}/plymouth/ultimate-edition-plymouth-${THEMEVERSION}_all/usr/share/plymouth/themes/Ultimate_Edition_${THEMEVERSION}/Ultimate_Edition_${THEMEVERSION}.png"
		cd "${FLDR}/plymouth/ultimate-edition-plymouth-${THEMEVERSION}_all/usr/share/plymouth/themes/Ultimate_Edition_${THEMEVERSION}/" || exit 1;
		ln -s "Ultimate_Edition_${THEMEVERSION}.png" "background.png"
		ln -s "Ultimate_Edition_${THEMEVERSION}.png" "suspend.png"
		cd "${INFOLDER}" || exit 1;
		WALLLOCATIONS=( "usr/share/sddm/themes/Ultimate_Edition_${THEMEVERSION}" "usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/components/artwork" "usr/share/lxqt/themes/Ultimate_Edition_${THEMEVERSION}" "usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/wallpapers" "usr/share/plasma/desktoptheme/Ultimate_Edition_${THEMEVERSION}/wallpapers" "usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/splash/images" "usr/share/sddm/themes/Ultimate_Edition_${THEMEVERSION}/components/artwork" "usr/share/sddm/themes/Ultimate_Edition_${THEMEVERSION}/SlideFiles" );
		DESTFILENAME=( "../../../wallpapers/Ultimate_Edition_${THEMEVERSION}.png" "../../../../../../wallpapers/Ultimate_Edition_${THEMEVERSION}.png" "../../../wallpapers/Ultimate_Edition_${THEMEVERSION}.png" "../../../../../wallpapers/Ultimate_Edition_${THEMEVERSION}.png" "../../../../wallpapers/Ultimate_Edition_${THEMEVERSION}.png" "../../../../../../wallpapers/Ultimate_Edition_${THEMEVERSION}.png" "../../../../../wallpapers/Ultimate_Edition_${THEMEVERSION}.png" "../../../../wallpapers/Ultimate_Edition_${THEMEVERSION}.png" );
		NAMINGCONVENTION=( "Ultimate_Edition_${THEMEVERSION}.png" "background.png" "background.png" "background.png" "Ultimate_Edition_${THEMEVERSION}.png" "background.png" "background.png" "background.png" "background.png" );
		#Encapsulate "Populating Wallpaper to
		#cd "${FLDR}/theme/" || exit 1;
		MAIN="${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/wallpapers/Ultimate_Edition_${THEMEVERSION}.png"
		cp "${WALLPAPER}" "${MAIN}"
		MAIN="usr/share/wallpapers/Ultimate_Edition_${THEMEVERSION}.png"
		cd "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/" || exit 1;
		for EACH in "${WALLLOCATIONS[@]}";
		do
			if ! [[ -d "${EACH}" ]]; then
				mkdir -p "${EACH}"
			fi
			if ! [[ -f "${MAIN}" ]]; then
				if ! [[ "$SUPPRESS" ]]; then
					Encapsulate "Populating Wallpaper:${WALLPAPER} with wallpaper from ${PWD} to ${EACH}/${DESTFILENAME[${INDEX}]}."
				fi
				cp "${WALLPAPER}" "${EACH}/${DESTFILENAME[${INDEX}]}"
			else
				#cd "${EACH}" || exit 1;
				cd "${EACH}" || exit 1;
				if ! [[ "$SUPPRESS" ]]; then
					Encapsulate "${INDEX}. Linking wallpaper:${NAMINGCONVENTION[$INDEX]} (to save space) to ${DESTFILENAME[$INDEX]}"
				fi
				ln -s "${DESTFILENAME[$INDEX]}" "${NAMINGCONVENTION[$INDEX]}"
				cd "${INFOLDER}/${DEBFOLDER}" || exit 1;
				#cd "${INFOLDER}" || exit 1;
			fi
			INDEX=$(( INDEX +1 ))
		done
	else
		Error "Wallpaper: ${WALLPAPER} not found."
		FullBar
		exit 1;
	fi
	if [[ -d "${INFOLDER}" ]]; then
		cd "${INFOLDER}" || exit 1;
	fi
}

Previews () {
	FLDR="Ultimate Edition ${THEMEVERSION}"
	declare -i INDEX;
	declare -a DESTFILENAME=();
	INDEX=0
	cd "${INFOLDER}" || exit 1;
	if ! [[ "$SUPPRESS" ]]; then
		Encapsulate "Setting Initial preview at ${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png"
	fi
	magick "${WALLPAPER}" -resize 320x200! "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png"
	magick "${WALLPAPER}" -resize 320x200! "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/previews/splash.png"
	magick "${WALLPAPER}" -resize 320x200! "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/previews/preview.png"
	cd "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/" || exit 1;
	PREVIEWLOCATIONS=( "usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/artwork" "usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/loginmanager" "usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/loginmanager" "usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/loginmanager" "usr/share/lxqt/themes/Ultimate_Edition_${THEMEVERSION}" );
	RELATIVE=( "../../../../../sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png" "../../../../../sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png" "../../../../../sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png" "../../../../../sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png" "../../../../sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png" "../../../sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png");
	DESTFILENAME=( "background.png" "background.png" "preview.png" "splash.png" "preview.png" "screenshot.png" );
	MAIN="usr/share/sddm/themes/Ultimate_Edition_${THEMEVERSION}/preview.png"
	if ! [[ "$SUPPRESS" ]]; then
		Encapsulate "Generating previews in: ${PWD}"
	fi
	if [[ -f "${MAIN}" ]]; then
		for EACH in "${PREVIEWLOCATIONS[@]}";
		do
			if ! [[ -d "${EACH}" ]]; then
				mkdir -p "${EACH}"
			fi
			if ! [[ -f "${MAIN}" ]]; then
				Error "Wallpaper: ${MAIN}, Not found from ${PWD} exiting."
				exit 1;
			else
				cd "${EACH}" || exit 1;
				if ! [[ "$SUPPRESS" ]]; then
					Encapsulate "${INDEX}. Generating link: ${WALLPAPER} relative to:${RELATIVE[$INDEX]} in ${EACH}"
				fi
				ln -s "${RELATIVE[$INDEX]}" "${DESTFILENAME[$INDEX]}"
				cd "${INFOLDER}/${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/" || exit 1;
				#convert "${MAIN}" "${EACH}/${DESTFILENAME[$INDEX]}"
			fi
			INDEX=$(( INDEX +1 ))
		done
	else
		Error "Wallpaper: ${WALLPAPER} not found."
		FullBar
		exit 1;
	fi
}

KDESPLASH () {
	RES=("1440x900" "1600x900" "1920x1080" "1920x1200" "2560x1440" "3840x2160");
	DISTREL="${2}"
	if [[ "${DISTREL}" ]]; then
		echo "Vesion detected as: ${DISTREL}"
	else
		echo "Version not detected. Exiting."
		exit 1;
	fi

	sourcefile="Ultimate_Edition_${DISTREL}.png"
	destfolder="images/"
#	if [[ -f "$sourcefile" ]]; then
		#Encapsulate "Newer version of KDE detected. Setting up as required."
		#destfolder="usr/share/sddm/themes/breeze/components/artwork/"
 #	   cd "$destfolder" || exit 1;
  #	  for each in "${RES[@]}"
   #	 STRIPPEDSLASH="background.png"
	#	Encapsulate "Converting image $sourcefile in $PWD to 3840×2160! to $destfolder$STRIPPEDSLASH/background.png"
	 #   convert "$sourcefile" -resize 3840x2160! -quality 100 -strip "$destfolder$STRIPPEDSLASH/background.png"
	  #  Encapsulate "Success, exiting."
	   # exit 0;
	#fi
	CONVERSION=$(type -p convert)
	if [[ -d "usr/share/wallpapers" ]]; then
		cd "usr/share/wallpapers" || exit 1;
		mkdir "images"
	else
		Error "Folder not found: usr/share/wallpapers in ${PWD}"
	fi
	if [[ "${CONVERSION}" && -f "${sourcefile}" && -d "$destfolder" ]]; then
		Encapsulate "Moving to: ${destfolder} from ${PWD}"
		cd "$destfolder" || exit 1;
		for EACH in "${RES[@]}"
		do
			#STRIPPEDSLASH=$(echo "$EACH" | sed "s/\///g")
			echo "Converting image $sourcefile to $EACH to $destfolder/$EACH.png"
			convert "../$sourcefile" -resize "$EACH!" -quality 100 -strip "$EACH.png"
		done
		Center "Done."
	else
		if ! [[ "$CONVERSION" ]]; then
			Error "Imagemagick is not installed, ksplash will not be themed."
		fi
		if ! [[ "$sourcefile" ]]; then
			Error "Ultimate_Edition_$DISTREL theme is not installed."
		fi
		if ! [[ "$destfolder" ]]; then
			Error "Ksplash has not been installed, or has not been programmed. Not themeing."
		fi
		Error "Ksplash will not be themed."
	fi
	if [[ -d "${INFOLDER}" ]]; then
		cd "${INFOLDER}" || exit 1;
	fi
}

RenameFolders () {
	if ! [[ "${SUPPRESS}" ]]; then
		Encapsulate "Renaming Folders:"
	fi
	CVERSION="${THEMEVERSION}"
	SHORTVERSION="${CVERSION}"
	#for EACH in "${THEMETYPES[@]}"
	#do
	if ! [[ "${SUPPRESS}" ]]; then
		Center "Processing: ${EACH}"
	fi
	if [[ -d "Ultimate Edition ${THEMEVERSION}" ]]; then
		cd "Ultimate Edition ${THEMEVERSION}" || exit 1;
		if ! [[ "${SUPPRESS}" ]]; then
			Encapsulate "Renaming Folders in ${PWD}"
		fi
		find . -depth -type d -name 'ue*' -execdir mv {} "Ultimate_Edition_${THEMEVERSION}" \;
		if [[ -d "plymouth/ultimate-edition-plymouth-${THEMEVERSION}_all/usr/share/plymouth/themes/Ultimate Edition ${THEMEVERSION}" ]]; then
			if ! [[ "${SUPPRESS}" ]]; then
				Encapsulate "Correcting Plymouth folder naming convention."
			fi
			mv "plymouth/ultimate-edition-plymouth-${THEMEVERSION}_all/usr/share/plymouth/themes/Ultimate Edition ${THEMEVERSION}" "plymouth/ultimate-edition-plymouth-${THEMEVERSION}_all/usr/share/plymouth/themes/Ultimate_Edition_${THEMEVERSION}"
		fi
		if [[ -d "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/themes/Ultimate_Edition_${THEMEVERSION}" ]]; then
			if ! [[ "${SUPPRESS}" ]]; then
				Encapsulate "Correcting Theme folder naming convention."
			fi
			mv "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/themes/Ultimate_Edition_${THEMEVERSION}" "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/themes/Ultimate Edition ${THEMEVERSION}"
		fi
		if [[ -d "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/sddm/themes/Ultimate Edition ${THEMEVERSION}" ]]; then
			if ! [[ "${SUPPRESS}" ]]; then
				Encapsulate "Correcting Theme folder naming convention."
			fi
			mv "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/sddm/themes/Ultimate Edition ${THEMEVERSION}" "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/sddm/themes/Ultimate_Edition_${THEMEVERSION}"
		fi
		if [[ -d "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/aurorae/themes/Ultimate_Edition_${THEMEVERSION}" ]]; then
			if ! [[ "${SUPPRESS}" ]]; then
				Encapsulate "Correcting Theme folder naming convention."
			fi
			mv "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/aurorae/themes/Ultimate_Edition_${THEMEVERSION}" "theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/aurorae/themes/Ultimate Edition ${THEMEVERSION}"
		fi
	fi
	declare -a TYPES=("#ULTIMATE_WALLPAPER" "#ULTIMATE_THEME" "#VERSION" "#NUMBER" "#DOTTED" "#THEMENAME" "#ULTIMATE_LOGO");
	declare -a REPLACEMENT=("Ultimate_Edition_${CVERSION}" "Ultimate_Edition_${CVERSION}" "${CVERSION}" "${CVERSION}" "ultimate.edition.${CVERSION}" "Ultimate_Edition_${CVERSION}" "${CVERSION}-logo");
	if ! [[ "$SUPPRESS" ]]; then
		Center "Pushing core to replicate settings for ${SHORTVERSION}. Stage 1"
	fi
	ARRAY=0
	for ITEM in "${TYPES[@]}"
	do
		FILETOPROCESS=$(grep -r "#${ITEM}" | cut -d ":" -f1)
		for EACH in "${FILETOPROCESS[@]}"
		do
			#echo "ARRAY:$ARRAY"
			REPLACE="${REPLACEMENT[$ARRAY]}"
			if [[ "${DEBUG}" ]]; then
				echo "${EACH} contains a reference to ${ITEM}, setting for ${SHORTVERSION}."
				echo "Item:${ITEM} Replacement:${REPLACE} File:${EACH} in ${PWD}"
			fi
			SUPRESS=$(sed -i "s/#${ITEM}/${REPLACE}/g" "${EACH}" 2>/dev/null)
		done
	ARRAY=$((ARRAY + 1))
	done
	shopt -s nullglob
	declare -a FILETOPROCESS=();
	declare -a TYPES=("#ULTIMATE_WALLPAPER" "#ULTIMATE_THEME" "#VERSION" "#NUMBER" "#DOTTED" "#THEMENAME" "#ULTIMATE_LOGO");
	declare -a REPLACEMENT=("Ultimate_Edition_${SHORTVERSION}" "Ultimate_Edition_${SHORTVERSION}" "${SHORTVERSION}" "${SHORTVERSION}" "ultimate.edition.${SHORTVERSION}" "Ultimate_Edition_${SHORTVERSION}" "${CVERSION}-logo");
	if ! [[ "$SUPPRESS" ]]; then
		Center "Pushing core to replicate settings for ${CVERSION}. Stage 2"
	fi
	INDEX=0
	for ITEM in "${TYPES[@]}"
	do
		if [[ "${DEBUG}" ]]; then
			echo "DEBUGGING in ${PWD}"
		fi
		if ! [[ "${SUPPRESS}" ]]; then
			Encapsulate "Searching: ${ITEM} | Replacing with: ${REPLACEMENT[$INDEX]}"
		fi
		( shopt -s globstar dotglob;
		for file in **; do
			if [[ -f "${file}" ]] && [[ -w "${file}" ]]; then
				sed -i -- "s/${ITEM}/${REPLACEMENT[$INDEX]}/g" "${file}"
			fi
		done
		)
		#find . -type f -exec sed -i "s/${ITEM}/${REPLACEMENT[$INDEX]}/g" {} +
		#find . -exec sed -i -e "s/${ITEM}/${REPLACEMENT[$INDEX]}/g" {} \;
#		SUPPRESS=$(find ./ -type f -exec sed -i "s/${ITEM}/${REPLACEMENT[$INDEX]}/g" {})
		INDEX=$(( INDEX + 1 ))
	done
	#done
	if [[ -d "${INFOLDER}" ]]; then
		cd "${INFOLDER}" || exit 1;
	fi
	if [[ -d "Ultimate Edition ${THEMEVERSION}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.desktop" ]]; then
		if ! [[ "$SUPPRESS" ]]; then
			Encapsulate "Setting up KDE Theme for Ultimate Edition ${THEMEVERSION}."
		fi
		mv "Ultimate Edition ${THEMEVERSION}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.desktop" "Ultimate Edition ${THEMEVERSION}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop"
	fi
}

RenameFiles () {
	FLDR="Ultimate Edition ${THEMEVERSION}"
	#"ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/desktoptheme/Ultimate Edition ${THEMEVERSION}/metadata.desktop" "ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/loginmanager/metadata.desktop"
	declare -a SRCFILES=( "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/aurorae/themes/Ultimate Edition ${THEMEVERSION}/ue.rc" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/color-schemes/ue.colors" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/compiz/ue-logo.png" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/pixmaps/ue-logo.png" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/gnome-background-properties/ue.xml" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/compiz/ueskydome.png" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/kstyle/themes/ue.themerc" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.6.7.desktop/Ultimate Edition #VERSION.desktop" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/aurorae/themes/Ultimate Edition ${THEMEVERSION}/ue.desktop" "${FLDR}/plymouth/ultimate-edition-plymouth-${THEMEVERSION}_all/usr/share/plymouth/themes/Ultimate_Edition_${THEMEVERSION}/ue.plymouth" );
	#"ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/desktoptheme/Ultimate Edition ${THEMEVERSION}/Ultimate Edition ${THEMEVERSION}.desktop" "ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/contents/loginmanager/Ultimate Edition ${THEMEVERSION}.desktop"
	declare -a DESTFILES=( "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/aurorae/themes/Ultimate Edition ${THEMEVERSION}/${FLDR}rc" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/color-schemes/${FLDR}.colors" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/compiz/ue${THEMEVERSION}-logo.png" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/pixmaps/ue${THEMEVERSION}-logo.png" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/gnome-background-properties/${FLDR}.xml" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/compiz/ue${THEMEVERSION}skydome.png" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/kstyle/themes/Ultimate Edition ${THEMEVERSION}.themerc" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/plasma/look-and-feel/org.kde.ultimate.edition.${THEMEVERSION}.desktop/Ultimate Edition ${THEMEVERSION}.desktop" "${FLDR}/theme/ultimate-edition-theme-${THEMEVERSION}_all/usr/share/aurorae/themes/Ultimate Edition ${THEMEVERSION}/Ultimate Edition ${THEMEVERSION}.desktop"  "${FLDR}/plymouth/ultimate-edition-plymouth-${THEMEVERSION}_all/usr/share/plymouth/themes/Ultimate_Edition_${THEMEVERSION}/Ultimate_Edition_${THEMEVERSION}.plymouth");
	ARRAY=0
	for FILE in "${SRCFILES[@]}"
	do
		if [[ -f "${FILE}" ]]; then
   			#echo "ARRAY:$ARRAY DESTFILES:${DESTFILES[$ARRAY]}"
   			REPLACE="${DESTFILES[$ARRAY]}"
   			if ! [[ "${SUPPRESS}" ]]; then
				Encapsulate "Renaming: ${FILE} to ${REPLACE}"
			fi
   			NOOUT=$(mv "${FILE}" "${REPLACE}")
		fi
		ARRAY=$((ARRAY + 1))
	done
}

SetupTheme () {
	THEMEVERSION="${2}"
	for EACH in "${THEMETYPES[@]}";
	do
		if ! [[ "${SUPPRESS}" ]]; then
			Encapsulate "Begining build of ${EACH}"
		fi
		SOURCEFOLDER="/usr/share/ultimate_edition/${EACH}"
		mkdir -p "Ultimate Edition ${THEMEVERSION}/${EACH}/ultimate-edition-${EACH}-${THEMEVERSION}_all/usr/share/"
		mkdir -p "Ultimate Edition ${THEMEVERSION}/${EACH}/ultimate-edition-${EACH}-${THEMEVERSION}_all/DEBIAN/"
		# cd "${SOURCEFOLDER}/" || exit 1;
		cp -T -R "${SOURCEFOLDER}/" "Ultimate Edition ${THEMEVERSION}/${EACH}/ultimate-edition-${EACH}-${THEMEVERSION}_all/"
		cp -T -R "${SOURCEFOLDER}/DEBIAN/" "Ultimate Edition ${THEMEVERSION}/${EACH}/ultimate-edition-${EACH}-${THEMEVERSION}_all/DEBIAN"
	done
	RenameFolders "$@"
	RenameFiles "$@"
	Wallpapers "$@"
	Previews "$@"
	#KDESPLASH "$@"
}

THEMETYPES=( "theme" "plymouth" "grub" ) ;
# Help system - I have re-wrote this section to work with code-cleanup.
Help() {
	CONTAINS=$(echo "$@" | grep -w '\-h\|\-\-help')
	if [[ "${DEBUG}" ]]; then
		echo "DEBUGGING HELP FUNCTION: 1:${1} 2:${2} 3:${3} CONTAINS:${CONTAINS}"
	fi
	if ! [[ "${3}" == "" && "${CONTAINS}" ]]; then
		if [[ "${2}" != "" ]]; then
			PRAM="${2}"
		else
			PRAM="ALL"
		fi
	fi
	if [[ "${CONTAINS}" ]]; then
		if [[ "${2}" != "" ]]; then
			PRAM="${2}"
			VersionDump
		else
			PRAM="ALL"
			VersionDump
		fi
	fi
	if ! [[ "${1}" ]]; then
		VersionDump
	fi
	case "${PRAM}" in
		ALL)
		Encapsulate "Usage: ${PROGNAME} -<-COMMAND> [OPTION]"
		FullBar
		Encapsulate "Mandatory arguments to long options are identical for short options."
		Encapsulate "  possible commands..."
		Encapsulate ""
		Encapsulate '  -s	   --setup		 sets up Ultimate_Edition_x.x theme files'
		Encapsulate '  -h	   --help		  this help message'
		Encapsulate '  -v	   --version	   dump version info and exit'
		Encapsulate ""
		FullBar
		Center "${PROGNAME} --help [COMMAND] for further information."
		FullBar;;
		ALL|s|setup)
		Encapsulate ""
		Encapsulate "Usage setup;"
		FullBar
		Center "${PROGNAME} -s <VERSION>"
		FullBar
		Encapsulate "Invokes ${PROGNAME} to build theme files for Ultimate_Edition_<VERSION> from scratch."
		Encapsulate "The format must comply as follows:"
		Encapsulate "<VERSION>"
		Encapsulate "Valid arguments are as follows and <REQUIRED>:"
		Encapsulate "Version: 2.3, 3.5 - 7.1"
		Encapsulate "[OPTIONAL PARMS]"
		Encapsulate " "
		Encapsulate "theme=, wallpaper="
		Encapsulate " "
		Encapsulate "Example: uetheme --setup 6.6 wallpaper=wallpaper.png theme=themefolder"
		FullBar;;
		ALL|v|version)
		echo -e "\\n${txtwht}Usage version;${txtrst}"
		echo -e "${txtgrn}${PROGNAME} -v${txtrst}"
		echo -e "Displays ${PROGNAME}'s version number and exits.";;
		ALL|h|help|\?)
		echo -e "\\n${txtwht}Useage Help [COMMAND];${txtrst}"
		echo -e "${txtgrn}${PROGNAME} -h [COMMAND]${txtrst}"
		echo "Displays this message. For futher information ${PROGNAME} help [COMMAND]"
		echo -e "or refer to the manpages.\\n"
		echo -e "man ${PROGNAME}"
		echo -e "${txtgrn}"
		echo -e "Example: ${PROGNAME} -h version"
		echo -e "${txtwht}Will display help about the command version${txtrst}"
	esac
	exit 0
}

All () {
	Center "${APPNAME} (${PROGNAME}) ${VERSION}, ${BUILDDATE}"
	Encapsulate "GNU ${PROGNAME} home page: <http://${WEBSITE}/>."
	Encapsulate "E-mail bug reports to: ${EMAIL}."
	Encapsulate "Be sure to include the word ${PROGNAME} somewhere in the Subject: field."
	FullBar
	BASECORES=0
	BASECORE=$(echo "$@" | grep -i "BASECORE")
	if [[ "${BASECORE}" ]]; then
		BASECORES=1
	fi
	# Snatch the number of CPU Core(s) / threads the end user has.
	CORES=$(grep -i "processor" "/proc/cpuinfo" | sed '/model/d' | wc -l)
	(( ACTUALCORES = ${CORES} -1 ))
	Encapsulate "Number of CPU Core(s) / threads(s) detected: ${CORES}"
	Encapsulate "Launching ${CORES} threads to accelerate the building process, please wait."
	#CORES=$(("${CORES}" +3))
	CONTAINS=$(echo "$@" | grep -i 'threads')
	if [[ "${CONTAINS}" ]]; then
		THREADS=$(echo "$@" | grep -i "threads=" | sed "s/^.*threads=//g" | cut -d" " -f1)
		Encapsulate "Threads Specified as: ${THREADS}"
		CALCULATE=$(( "${THREADS}" + 3 + "${BASECORES}" ))
		CORES="${CALCULATE}"
	fi
   	for VERSION in {2..7};
	do
		for DOT in {0..9};
		do
			# How many core do we have running currently?
	   	   	RUNNINGINSTANCES=$(ps -C uetheme | wc -l)
			# EXPELL all core, let's go to work, if we have maxed out, wait for one core to finish.
			while [[ "${RUNNINGINSTANCES}" -ge "${CORES}" ]] ;
			   	do
					RUNNINGINSTANCES=$(ps -C uetheme | wc -l)
   				done
			# Yee HAW we have an open core, let's put it to work.
	   	   	if [[ "${RUNNINGINSTANCES}" -lt "${CORES}" ]]; then
	   	   		INSTANCE=$(( "${RUNNINGINSTANCES}" + 1 -3 - "${BASECORES}"))
				if [[ -d "${VERSION}.${DOT}" ]]; then
					if [[ "${BASECORES}" ]] ; then
						if [[ -f "${VERSION}.${DOT}.png" ]]; then
					   	   	Encapsulate "Launching thread #${INSTANCE}: Populating Ultimate Edition Theme: ${VERSION}.${DOT} (RUNNING INSTANCES: ${RUNNINGINSTANCES} Benchmark cores: ${BASECORES} Internal processes: 2)"
						fi
					else
						if [[ -f "${VERSION}.${DOT}.png" ]]; then
							Encapsulate "Launching thread #${INSTANCE}: Populating Ultimate Edition Theme: ${VERSION}.${DOT} (RUNNING INSTANCES: ${RUNNINGINSTANCES} Internal processes: 2)"
						fi
					fi
					if ! [[ -d "Ultimate Edition ${VERSION}.${DOT}" ]] ; then
						if [[ -f "${VERSION}.${DOT}.png" ]]; then
				   	   		uetheme --setup ${VERSION}.${DOT} wallpaper=${VERSION}.${DOT}.png theme=${VERSION}.${DOT} --suppress & #& allows that to process and continue running through current software hence why the not equal 3 below
						fi
					fi
				fi
   			fi
	   	done
	done
	# Wait for the cores to finish, before continuing.
	CROAK=$(( 3 + "${BASECORES}" ))
	while [[ "${RUNNINGINSTANCES}" -ne "${CROAK}" ]] ;
   	do
		#Encapsulate "Waiting for threads to complete: ${RUNNINGINSTANCES}"
		RUNNINGINSTANCES=$(ps -C uetheme | wc -l)
   	done
	# I hate this command, but ensures all files are there before continuing.
	Encapsulate "Synconising data..."
	sync;
	# Import Goodies?
	for VERSION in {2..7};
	do
		for DOT in {0..9};
		do
			if [[ -d "${VERSION}.${DOT}" ]]; then
				Encapsulate "Build folder for ${VERSION}.${DOT} found: Importing to Ultimate Edition ${VERSION}.${DOT}/theme"
				cp -T -R "${VERSION}.${DOT}/" "Ultimate Edition ${VERSION}.${DOT}/theme/ultimate-edition-theme-${VERSION}.${DOT}_all/"
			fi
		done
	done
	Encapsulate "Synconising data..."
	sync;
	for VERSION in {2..7};
	do
		for DOT in {0..9};
		do
	   	   	RUNNINGINSTANCES=$(ps -C uetheme | wc -l)
				while [[ "${RUNNINGINSTANCES}" -ge "${CORES}" ]] ;
			   	do
					RUNNINGINSTANCES=$(ps -C uetheme | wc -l)
   				done
			# Yee HAW we have an open core, let's put it to work.
	   	   	if [[ "${RUNNINGINSTANCES}" -lt "${CORES}" ]]; then
	   	   		INSTANCE=$(( "${RUNNINGINSTANCES}" + 1 -3 - "${BASECORES}"))
				if [[ "${BASECORES}" ]] ; then
					if [[ -d "${VERSION}.${DOT}" ]]; then
				   	   	Encapsulate "Launching thread #${INSTANCE}: Building Ultimate Edition deb(s): ${VERSION}.${DOT} (RUNNING INSTANCES: ${RUNNINGINSTANCES} Benchmark cores: ${BASECORES} Internal processes: 2)"
					fi
				else
					if [[ -d "${VERSION}.${DOT}" ]]; then
						Encapsulate "Launching thread #${INSTANCE}: Building Ultimate Edition deb(s): ${VERSION}.${DOT} (RUNNING INSTANCES: ${RUNNINGINSTANCES} Internal processes: 2)"
					fi
				fi
				if [[ -d "${VERSION}.${DOT}" ]]; then
			   	   	uetheme --builddebs ${VERSION}.${DOT}  --suppress &
				fi
   			fi
	   	done
	done
	TCORES=$(( 3 + "${BASECORES}"))
	while [[ "${RUNNINGINSTANCES}" -ne "${TCORES}" ]] ;
   	do
		#Encapsulate "Waiting for threads to complete: ${RUNNINGINSTANCES}"
		RUNNINGINSTANCES=$(ps -C uetheme | wc -l)
		#echo "INSTANCES: ${RUNNINGINSTANCES} | TCORES: ${TCORES}"
   	done
	Encapsulate "Synconising data..."
	sync;
	TOTALDEBS=0
	for VERSION in {2..7};
	do
		for DOT in {0..9};
		do
			if [[ -d "Ultimate Edition ${VERSION}.${DOT}" ]]; then
				Encapsulate "Scanning Ultimate Edition ${VERSION}.${DOT} for successful builds."
				cd "Ultimate Edition ${VERSION}.${DOT}" || exit 1;
				find . -name "*.deb" -type f -exec mv {} . \;
				Encapsulate "Syncronising..."
				sync;
				DEBS=(./*.deb)
				COUNTDEBS="${#DEBS[@]}"
				(( TOTALDEBS += ${COUNTDEBS} ))
				Encapsulate "Found ${COUNTDEBS} perfect deb(s) in ${PWD}"
				cd "${INFOLDER}" || exit 1;
			fi
		done
	done
	Encapsulate "Moving deb(s) to ${PWD}"
	find . -name "*.deb" -type f -exec mv {} . \;
	Encapsulate "Sycronising ..."
	sync;
	DEBS=(./*.deb)
	COUNTDEBS="${#DEBS[@]}"
	Encapsulate "Perfect debs built this session: ${COUNTDEBS} in ${PWD}"
	Timer "Stop" "${APPNAME}"
	FullBar
}

CleanBuild () {
#	if [[ -f "*.deb" ]]; then
		Encapsulate "Cleaning Builds."
		FOLDERS=(Ultimate\ Edition\ *)
		NUMFOLDERS="${#FOLDERS[@]}"
		if [[ "${NUMFOLDERS}" -gt 1 ]]; then
			sudo rm -R Ultimate\ Edition\ *
		fi
		DEBS=(./*.deb)
		COUNTDEBS="${#DEBS[@]}"
		if [[ "${COUNTDEBS}" -gt 1 ]]; then
			sudo rm *.deb
		fi
		Encapsulate "Done."
		FullBar
#	fi
}

BenchMark () {
	if ! [[ -f "2.0.png" ]]; then
		Encapsulate "Downloading Themes source code (1/2 GB of data)"
		Download "themes-source.tar.gz"
		FullBar
		Encapsulate "Extracting archive."
		FullBar
		mkdir -p themes/
		mv themes-source.tar.gz themes/
		cd "themes/" || exit 1;
		tar xfv themes.tar.gz
		if [[ -f "2.0.png" ]]; then
			FullBar
			Encapsulate "Extraction complete."
		else
			Error "Extraction failure. Exiting."
			exit 1;
		fi
		#exit 0;
	fi
	# Snatch the number of CPU Core(s) / threads the end user has.
	Center "BENCHMARK MODE DETECTED"
	CORES=$(grep -i "processor" "/proc/cpuinfo" | sed '/model/d' | wc -l)
	(( ACTUALCORES = ${CORES} -1 ))
	Encapsulate "Number of CPU Core(s) / threads(s) detected: ${CORES}"
	PROCESSES=$(("${CORES}" +3))
	TOTALBUILDS={1..${CORES}}
	for EACH in $(seq 1 ${CORES})
	do
		Center "Initiating Build #${EACH} of ${CORES}"
		Encapsulate "Reseting Timer"
		Timer Start "Build #${EACH}"
		Encapsulate "Launching ${EACH} thread(s) of ${CORES}, please wait."
		uetheme --all threads="${EACH}" BASECORES
		Timer Stop "Build #${EACH}"
		Encapsulate "Syncronising..."
		sync;
		CleanBuild "$@"
		FullBar
	done

}

shopt -u nullglob
if ! [[ "${SUPPRESS}" ]]; then
	VersionDump "$@"
	Timer "Start" "${APPNAME}"
fi

case "${1}" in
	-a|--all) CheckRoot; All "$@"; exit 0;;
	-B|--benchmark) CheckRoot; BenchMark "$@"; exit 0;;
	-b|--builddebs) CheckRoot; BuildDebs "$@"; exit 0;;
	-c|--clean) CheckRoot; CleanBuild "$@"; exit 0;;
	-e|--extract) ExtractISO "$@"; exit 0;;
	-h|--help|-\?) Help "$@"; exit 0;; # TODO: Enhance this section.
	-s|--setup) CheckRoot; SetupTheme "$@"; exit 0;;
	-v|--version) exit 0;;
	*) Help; exit 0;;
esac

