#!/bin/bash

# Install common packages if not already installed

#Program install list - Your personal list of programs you want to install. Not all of them are going to be installed
#   but, this script will make an attempt to do so. You can add or subtract or change these packages at your pleasure.
#   plist - Program list
#   flist - Flatpak list -- You can get the full name of the desired program by running "flatpak search <short name>"
#		at a terminal prompt. Ex.: flatpak search studio | grep -i obs -- will bring up the long file name to install OBS Studio.

plist="gftp nano neofetch htop flatpak gscan2pdf kitty"

flist="com.obsproject.Studio org.strawberrymusicplayer.strawberry io.github.celluloid_player.Celluloid org.kde.krita org.inkscape.Inkscape org.libreoffice.LibreOffice org.shotcut.Shotcut"

# First, we must determine what package manager we are using . . .by process of elmination

# This is the package manager list. If you add any pk mgrs to it, don't forget to add
#	them to the function list below as well. NOTE: the last match will be used, so order wisely
#	when there is the potential for more than one pkg mgr in a distro.

pkmg="apt-get pacman yay yum dnf eopkg zypper"

# This is the trigger that, if we get through the above list and no pk mgr is found, it will
#	trigger the appropriate warning message and exit the script.

errmsg=1
for I in $pkmg; do
	which $I
	if [ $? -eq 0 ]; then
		distro=$I
		errmsg=0
	fi
done
if [ $errmsg -gt 0 ]; then
	echo "I'm sorry, but your distro's package manager is not in my list."
	echo ""
	echo "You'll either need to add it into the list by editing this script,"
	echo "or you'll need to enter it manually in a terminal, or use your"
	echo "desktop envionment's GUI software center to install this software."
	exit 0
fi
		## Next, we will setup the functions: "uplist" for updating the system and "packlist" for installing
		##	the programs based upon the various pkg mgrs.

		## Setting some variables that we will use.

		green="\033[0;32m"		# Turn text green
		red="\033[0;31m"		# Turn text red
		nc="\033[0m"			# Turn off text colors
		wdinfo="$HOME/WD-deleteme.txt"	# The file that will be used to display a summary of actions at the end.
		echo "" > $wdinfo
		echo -e "${green}****** SUMMARY OF INSTALLATION ******${nc}" >> $wdinfo
		echo "" >> $wdinfo

			case "$distro" in
				"apt-get")
					function uplist () {
						sudo apt-get update && sudo apt-get upgrade -y
					}
					function packlist () {
						green="\033[0;32m"
						red="\033[0;31m"
						nc="\033[0m"
						sudo apt-get -y install $1
						if [ $? -eq 0 ]; then
							echo -e "${green}Installation of ${1} was successful!${nc}" | tee -a $wdinfo
						else
							echo -e "${red}Program ${1} was not installed!${nc}" | tee -a $wdinfo
						fi
					}
					fontdir='/usr/share/fonts/truetype'
					;;
				"pacman")
					function uplist () {
						sudo pacman -Syu --noconfirm
					}
					function packlist () {
						green="\033[0;32m"
						red="\033[0;31m"
						nc="\033[0m"
						sudo pacman -S --noconfirm $1
						if [ $? -eq 0 ]; then
							echo -e "${green}Installation of ${1} was successful!${nc}" | tee -a $wdinfo
						else
							echo -e "${red}Program ${1} was not installed!${nc}" | tee -a $wdinfo
						fi
					}
					fontdir='/usr/share/fonts/TTF'
					;;
				"yay")
					function uplist () {
						sudo yay -Syu --noconfirm
					}
					function packlist () {
						green="\033[0;32m"
						red="\033[0;31m"
						nc="\033[0m"
						sudo yay -S --noconfirm $1
						if [ $? -eq 0 ]; then
							echo -e "${green}Installation of ${1} was successful!${nc}" | tee -a $wdinfo
						else
							echo -e "${red}Program ${1} was not installed!${nc}" | tee -a $wdinfo
						fi
					}
					fontdir='/usr/share/fonts/TTF'
					;;
				"yum")
					function uplist () {
						sudo yum -y upgrade
					}
					function packlist () {
						green="\033[0;32m"
						red="\033[0;31m"
						nc="\033[0m"
						sudo yum -y install $1
						if [ $? -eq 0 ]; then
							echo -e "${green}Installation of ${1} was successful!${nc}" | tee -a $wdinfo
						else
							echo -e "${red}Program ${1} was not installed!${nc}" | tee -a $wdinfo
						fi
					}
					fontdir='/usr/share/fonts/truetype'
					;;
				"dnf")
					# Installing rpmfusion repos before doing installs
					dnf repolist | grep rpmfusion
					if [ $? -gt 0 ]; then
						sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
						[[ $? -gt 0 ]] || errorcode=1
						sudo dnf install https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
						[[ $? -gt 0 ]] || errorcode=1
					fi
					if [ $errorcode -gt 0 ]; then
						echo -e "${red}Rpmfusion repositories were not added. Some software may not be available to install.${nc}" | tee -a $wdinfo
					fi
					function uplist () {
						sudo dnf -y upgrade
					}
					function packlist () {
						green="\033[0;32m"
						red="\033[0;31m"
						nc="\033[0m"
						sudo dnf -y install $1
						if [ $? -eq 0 ]; then
							echo -e "${green}Installation of ${1} was successful!${nc}" | tee -a $wdinfo
						else
							echo -e "${red}Program ${1} was not installed!${nc}" | tee -a $wdinfo
						fi
					}
					fontdir='/usr/share/fonts/truetype'
					;;
				"eopkg")
					function uplist () {
						sudo eopkg -y upgrade
					}
					function packlist () {
						green="\033[0;32m"
						red="\033[0;31m"
						nc="\033[0m"
						sudo eopkg -y install $1
						if [ $? -eq 0 ]; then
							echo -e "${green}Installation of ${1} was successful!${nc}" | tee -a $wdinfo
						else
							echo -e "${red}Program ${1} was not installed!${nc}" | tee -a $wdinfo
						fi
					}
					fontdir='/usr/share/fonts/truetype'
					;;
				"zypper")
					function uplist () {
						sudo zypper --non-interactive up
					}
					function packlist () {
						green="\033[0;32m"
						red="\033[0;31m"
						nc="\033[0m"
						sudo zypper --non-interactive install $1
						if [ $? -eq 0 ]; then
							echo -e "${green}Installation of ${1} was successful!${nc}" | tee -a $wdinfo
						else
							echo -e "${red}Program ${1} was not installed!${nc}" | tee -a $wdinfo
						fi
					}
					fontdir='/usr/share/fonts/truetype'
					;;
			esac

# Install the list of programs if not already installed.

for I in $plist
do

	which $I
	if [ $? -gt 0 ]; then
		[[ "$I" == "obs" ]] && I="obs-studio"
		echo -e "${green}Installing $I . . .${nc}" | tee -a $wdinfo
		packlist $I
	else
       	echo -e "${red}Program $I is already installed.${nc}" | tee -a $wdinfo 
	fi

done


# Install programs from $flist via Flatpak if not installed already.
# Enable flatpakrepo on flatpak hub.

sudo flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
if [ $? -eq 0 ]; then
	echo -e "${green}Set up of flatpak repo was successful!${nc}" | tee -a $wdinfo
else
	echo -e "${red}There was an error in setting up the flatpak repos!--flatpak list of programs are not installed.${nc}" | tee -a $wdinfo
	exit 0
fi

for I in $flist; do

# Extracting the short name of the program from the bigger file name to install . . .
[[ $(echo ${I} | awk -F'.' '{print $1}') != '' ]] && J=$(echo ${I} | awk -F'.' '{print $1}')
[[ $(echo ${I} | awk -F'.' '{print $2}') != '' ]] && J=$(echo ${I} | awk -F'.' '{print $2}')
[[ $(echo ${I} | awk -F'.' '{print $3}') != '' ]] && J=$(echo ${I} | awk -F'.' '{print $3}')
[[ $(echo ${I} | awk -F'.' '{print $4}') != '' ]] && J=$(echo ${I} | awk -F'.' '{print $4}')
[[ $(echo ${I} | awk -F'.' '{print $5}') != '' ]] && J=$(echo ${I} | awk -F'.' '{print $5}')
if [[ $J == "" ]]; then
	echo -e "#{red}Could not get a proper name from file name for the program: ${I}${nc}"
else

# Now that we have a short name, we'll check to make sure the program is not already installed from the repos or by Flatpaks.
which ${J,,}
if [ $? -gt 0 ]; then
    flatpak list | grep -i ${J}
    if [ $? -gt 0 ]; then
    	echo -e "${green}Installing ${J} via Flatpaks . . .${nc}" | tee -a $wdinfo
    	flatpak --noninteractive install ${I}
	if [ $? -eq 0 ]; then
		echo -e "${green}Installation of ${J} was successful!${nc}" | tee -a $wdinfo
	else
		echo -e "${red}${J} was not installed!${nc}" | tee -a $wdinfo
	fi
    else
        echo -e "${red}Program ${J} is already installed via Flatpak.${nc}" | tee -a $wdinfo
    fi
else
    echo -e "${red}Program ${J} is already installed.${nc}" | tee -a $wdinfo
fi

fi

done

clear
cat $wdinfo
sleep 5
rm $wdinfo
exit
