File kalpa-firstboot of Package plasma-branding-Kalpa

#!/bin/bash

# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Copyright 2024 SUSE LLC
# SPDX-FileCopyrightText: Copyright 2024 Richard Brown
# SPDX-FileCopyrightText: Copyright 2024 Raymond Yip

installFlatpakRepo(){
	if ! /usr/bin/flatpak remote-add --user --if-not-exists flathub /usr/share/kalpa/flathub.flatpakrepo
	then
		kdialog --error "Adding Flathub Repo Failed"
		exit 1
	fi
}

installFlatpakPackage(){
	if ! /usr/bin/flatpak install --user --noninteractive flathub $1
	then
		kdialog --error "Installing $1 Failed"
		exit 1
	fi
}

waitforNet(){
	until /usr/bin/curl -s --max-time 5 https://flathub.org > /dev/null; do sleep 1; done
}

defaultOptions() {
	pkgs=("org.mozilla.firefox" "org.kde.kcalc" "org.kde.ark")
}

customizeOptions() {
	#User has decided to customize their installed flatpaks
	selection=$(kdialog --geometry 600x400+100+100 --title "Choose software to install" \
		--checklist "Select which software you would like to install\nMore software can be found in the Discover Software Center" \
			org.mozilla.Firefox "Mozilla Firefox" on \
			org.kde.kcalc "KCalc" on \
			org.kde.ark "Ark" on \
			org.kde.kwrite "KWrite" off \
			org.kde.gwenview "Gwenview" off \
			org.kde.okular "Okular" off \
	)
	# The above command returns a list of the flathub apps the user chose, in a single line.
	if [[ -z "${selection}" ]]; then
		#if cancel is pressed, $custom variable is empty so we go back to use the default options
		defaultOptions
	else
		IFS=\" read -a pkgs <<< "$selection"
	fi
}

firstMSG() {
    #Insert Welcome message here
	kdialog --title "Welcome to openSUSE Kalpa" --geometry 600x400+100+100 --icon start-here-kde\
		--yesno "Almost done! There is only one last step\n\nWe need to download some applications to get you started\n\nYou can add more software later using the Discover Software Center" \
		--yes-label "OK" \
		--no-label "Customise"
    option=$?
    if [ "$option" != 0 ]; then
        #User wants to customize software
        customizeOptions
    else
        #OK Button selected, running with defaults
        defaultOptions
    fi
}

lastMSG() {
    #Insert Welcome message here
	kdialog --geometry 600x400+100+100 --icon start-here-kde --title "Welcome to openSUSE Kalpa" \
		--msgbox "Congratulations!\n\nYour system is ready to use\n\nWe hope you'll enjoy openSUSE Kalpa"
    rm ~/.config/autostart/kalpa-firstboot.desktop
}

detectLocale() {
    if [[ ! "${LANG}" =~ ^en_ ]]; then
    need_locale=${LANG}
    fi
}

show_progress() {
	sleep 1
	num_pkgs="${#pkgs[@]}"
	total="$(( 4 + $num_pkgs ))"
	dbusRef=$(kdialog --title "Final Setup" --geometry 600x400+100+100 --progressbar "Final Setup" $total)
	qdbus6 "$dbusRef" showCancelButton false
	qdbus6 "$dbusRef" Set "" value 1
	qdbus6 "$dbusRef" setLabelText "Waiting for Internet connection"
	sleep 1
	waitforNet
	qdbus6 "$dbusRef" Set "" value 2
	if [[ -n "${need_locale}" ]]; then
		qdbus6 "$dbusRef" setLabelText "Installing Locales"
		install_locale
	fi
	qdbus6 "$dbusRef" Set "" value 3
	qdbus6 "$dbusRef" setLabelText "Installing Packages"
	installFlatpakRepo
	step=4
	for pkg in "${pkgs[@]}"; do
		installFlatpakPackage $pkg
		((step++))
		qdbus6 "$dbusRef" Set "" value $step
	done 
	qdbus6 "$dbusRef" close
}

install_locale() {
	if ! pkexec sh -c "/usr/sbin/transactional-update -n run zypper -n aloc ${need_locale} && /usr/sbin/transactional-update -n apply"
	then
		kdialog --error "Installing Locales Failed"
		exit 1
	fi
	for i in "$HOME/Desktop" "$HOME/Downloads" "$HOME/Templates" "$HOME/Public" "$HOME/Documents" "$HOME/Music" "$HOME/Pictures" "$HOME/Videos"
		do
			rmdir "$i"
		done
	rm "$HOME"/.config/user-dirs.dirs
	rm "$HOME"/.config/user-dirs.locale
	/usr/bin/xdg-user-dirs-update --force
}

detectLocale
firstMSG
show_progress
lastMSG
openSUSE Build Service is sponsored by