#!/bin/bash

#
# Quick and easy install of my favorite programs
#

# set version here each time we update the most recent changelog
version=2016.1129.01

echo "*** 2016 development script ***"

debug_mode=false

# List packages only?
action_list_packages=false

# When listing packages, present in html format?
action_list_packages_in_html=false

# 
action_query_only=false

# Define here so they are global
install_packages_list=""
remove_packages_list=""

function debug_echo {
	if [[ $debug_mode == true ]]
	then
		echo "$1"
	fi
}

# Check for root user
function only_run_as_root {
	if [[ $UID != 0 ]]
	then
		echo "You must be root or use 'sudo' to run this program."
		exit 1
	fi
}

function exit_if_not_fedora_25 {
	if [[ ! -f /etc/fedora-release || \
			"$(cat /etc/fedora-release)" != "Fedora release 25 (Twenty Five)" ]]
	then
		echo "Error: This script only works with Fedora Release 25"
		exit 1
	fi
}

function install_rpm_fusion {

	if [[ ! -f /etc/yum.repos.d/rpmfusion-free.repo || ! -f /etc/yum.repos.d/rpmfusion-free.repo ]]
	then

		if [[ ! -f /etc/yum.repos.d/rpmfusion-free.repo ]]
		then
			wget -nc https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-25.noarch.rpm
		fi

		if [[ ! -f /etc/yum.repos.d/rpmfusion-nonfree.repo ]]
		then
			wget -nc https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-25.noarch.rpm
		fi

	  dnf -y install ./rpmfusion-free-release-25.noarch.rpm ./rpmfusion-nonfree-release-25.noarch.rpm
	fi
}

function check_installed {

	# check to see if package is installed,
	# if yes, it will print the full package name
	# if no, it will include the words 'not installed' in the output

	if [[ `rpm -q ${1} | grep "not installed" | wc -l` -gt 0 ]]
	then
		return 0
	else
		return 1
	fi
}

function populate_remove_list {

	# Set to empty
	install_packages_list=""
	remove_packages_list=""

	# packages to remove: the annoyingly slow command-not-found
	remove_packages_list="$remove_packages_list PackageKit-command-not-found"

	# fonts I don't use that are installed by default
	remove_packages_list="$remove_packages_list khmeros-fonts-common khmeros-base-fonts lohit-devangari sil-abyssinica-fonts sil-padauk-fonts lohit-kannada smc-meera-fonts paktype-naqsh lohit-assamese thai-scalable-fonts-common lklug-fonts lohit-telugu wqy-zenhei paratype-pt-sans thai-scalable-waree lohit-tamil cjkuni-uming lohit-punjabi jomolhari lohit-gujarati lohit-gujarati-fonts lohit-oriya-fonts lohit-punjabi-fonts lohit-assamese-fonts sil-nuosu-fonts cjkuni-uming-fonts nhn-nanum-gothic-fonts lohit-tamil-fonts wqy-zenhei-fonts lohit-devanagari-fonts lohit-telugu-fonts lohit-bengali-fonts paktype-naqsh-fonts lohit-kannada-fonts jomolhari-fonts paratype-pt-sans-fonts smc-fonts-common lohit-malayalam-fonts tabish-eeyek nhn-nanum sil-mingzat"
}

function populate_install_list {

	# MULTIMEDIA
	install_packages_list="$install_packages_list lame grip rhythmbox gstreamer-ffmpeg gstreamer-plugins-ugly gstreamer-plugins-bad gstreamer-plugins-bad-nonfree amarok mplayer mplayer-gui mencoder xine-ui gxine xine-lib-extras vlc easytag k3b k3b-extras-freeworld banshee smplayer mp3splt libmp3splt"

	# GSTREAMER backends
	install_packages_list="$install_packages_list gstreamer-plugins-good-extras gstreamer-plugins-good gstreamer-plugins-base-tools gstreamer-plugins-base gstreamer-plugins-bad-free gstreamer gstreamer-plugins-ugly gstreamer1-plugins-bad-freeworld gstreamer1-plugins-good-extras gstreamer1-plugins-good gstreamer1-libav gstreamer1-plugins-base-tools gstreamer1-plugins-base gstreamer1-plugins-bad-free gstreamer1 gstreamer1-plugins-ugly"

	# APACHE HTTPD
	install_packages_list="$install_packages_list httpd httpd-tools"

	# REMOTE ACCESS
	install_packages_list="$install_packages_list tigervnc-server remmina remmina-plugins-rdp remmina-plugins-nx remmina-plugins-vnc"

	# MISC CLI TOOLS 
	install_packages_list="$install_packages_list expect pv"

	# PASSWORD MANAGEMENT
	install_packages_list="$install_packages_list pass keepassx"

	# SSH CLIENTS
	install_packages_list="$install_packages_list openssh-server openssh-clients"

	# TEXT FILE EDITING
	install_packages_list="$install_packages_list meld vim-X11 vim-enhanced dos2unix"

	# PERFORMANCE
	install_packages_list="$install_packages_list atop jnettop iptraf-ng strace ntop htop fio"

	# KVM VIRTUALIZATION
	install_packages_list="$install_packages_list qemu-kvm virt-manager virt-top libvirt-daemon-qemu virt-viewer libguestfs-tools virt-what"

	# KERNEL SOURCE (required by VIRTUALBOX)
	install_packages_list="$install_packages_list kernel-headers kernel-devel gcc dkms"

	# HARDWARE
	install_packages_list="$install_packages_list lm_sensors bonnie++ lsscsi sysstat powertop screenfetch"

	# CHAT
	install_packages_list="$install_packages_list pidgin pidgin-sipe pidgin-logviewer pidgin-libnotify pidgin-otr"

	# OFFICE
	install_packages_list="$install_packages_list libreoffice-writer libreoffice-calc libreoffice-base libreoffice-impress libreoffice-draw"

	# GRAPHICS
	install_packages_list="$install_packages_list gcolor2 gimp gimp-data-extras ufraw-gimp"

	# NETWORKING
	install_packages_list="$install_packages_list nmap mtr mtr-gtk wget openvpn NetworkManager-openvpn gftp tsocks proxychains-ng proxytunnel telnet firewall-config"

	# FILE ARCHIVING
	install_packages_list="$install_packages_list unzip unrar"

	# SERIAL COMMUNICATIONS
	install_packages_list="$install_packages_list conserver-client minicom"

	# STORAGE
	install_packages_list="$install_packages_list gparted"

	# GUI ENVIRONMENT
	install_packages_list="$install_packages_list gnome-tweak-tool alacarte dconf-editor wmctrl xbacklight"

	# GUI Terminals
	install_packages_list="$install_packages_list terminator"

	# GUI Terminal Terminix
	#install_packages_list="$install_packages_list terminix"

	# SHELL PROMPTS
	install_packages_list="$install_packages_list powerline powerline-fonts vim-powerline"

	# VERSION CONTROL
	install_packages_list="$install_packages_list git gitg"

	# GO LANGUAGE
	install_packages_list="$install_packages_list golang vim-go"

	# SAMBA CIFS SERVER
	install_packages_list="$install_packages_list samba-client samba-common samba"

	# NEMO FILE MANAGER
	install_packages_list="$install_packages_list nemo nemo-extensions"

	# MISC GNOME APPLICATIONS
	install_packages_list="$install_packages_list gnome-music gnome-photos gnome-maps gnome-weather bijiben"

	# PANORAMIC AND HDR PHOTOGRAPHY
	install_packages_list="$install_packages_list hugin darktable luminance-hdr"

	# DOCKER
	install_packages_list="$install_packages_list docker docker-client docker-vim docker-logrotate"

	# VAGRANT
	install_packages_list="$install_packages_list vagrant"

	# LDAP CLIENTS
	install_packages_list="$install_packages_list openldap-clients"
}

function list_packages {
	if [[ $action_list_packages ]]
	then
		for each_package in $install_packages_list
		do
			if [[ $action_list_packages_in_html ]]
			then
				echo "<li><strong>$each_package</strong> - </li>"
			else
				echo $each_package
			fi
		done
	fi
}

function apply_changes {

	package_available_for_install=""
	packages_available_to_remove=""

	for each_package in $install_packages_list
	do
		check_installed "$each_package"
		retval=$?
		if [[ $retval == 1 ]]
		then
			debug_echo "$each_package is already installed."
		else
			debug_echo "$each_package needs to be installed."
			packages_available_for_install="$packages_available_for_install $each_package"
		fi
	done

	for each_package in $remove_packages_list
	do
		check_installed "$each_package"
		retval=$?
		if [[ $retval == 1 ]]
		then
			debug_echo "$each_package needs to be removed."
			packages_available_to_remove="$packages_available_to_remove $each_package"
		fi
	done

	# default both to no, let user proceed by answering Y or y below
	install_confirm="n"
	remove_confirm="n"

	if [[ X$packages_available_for_install == "X" ]]
	then
		echo
		echo "There are no packages to install."
		echo
	else
		# query user now and apply changes below in one pass
		echo
		echo "PACKAGES TO BE INSTALLED: $packages_available_for_install"

		if [[ $action_query_only == false ]]
		then
			echo -n "Proceed? [y/n]"
			read install_confirm
		fi
	fi

	if [[ $packages_available_to_remove == "" ]]
	then
		echo
		echo "There are no packages to remove."
		echo
	else
		# query user now and apply changes below in one pass
		echo
		echo "PACKAGES TO BE REMOVED: $packages_available_to_remove"

		if [[ $action_query_only == false ]]
		then
			echo -n "Proceed? [y/n]"
			read remove_confirm
		fi
	fi

	if [[ X$install_confirm == X"Y" ]] || [[ X$install_confirm = X"y" ]]
	then
		dnf install $packages_available_for_install
	fi

	if [[ X$remove_confirm == X"Y" ]] || [[ X$remove_confirm = X"y" ]]
	then
		# do not provide -y (automatic confirmation yes) as dnf erase may remove
		# dependencies that we wish to keep?
		dnf erase $packages_available_to_remove
	fi
}

function usage {
	echo "packages $version"
	echo
	echo "usage: packages <options>"
	echo
	echo "-d --debug     Debug mode on (mostly for development use)"
	echo
	echo "-q --query     Query packages for it's changelist but do not do anything"
	echo
	echo "-l --list      List all packages checked for inclusion in ASCII format"
	echo "               Useful for external documentation by the author"
	echo
	echo "   --htmllist  List all packages checked for inclusion in HTML format"
	echo "               Useful for external web based documentation by the author"
	echo
	echo "-h --help      Show this syntax information"
	echo

	# If they're using fedora, this should always be there, but..
	if [[ -f /etc/fedora-release ]]
	then
		echo -n "You are using:"
		cat /etc/fedora-release
	fi

	exit
}

only_run_as_root

exit_if_not_fedora_25

populate_remove_list
populate_install_list

# Process command line arguments
while [ "$1" != "" ]
do
	case $1 in

		-q | --query )
			shift
			echo "Querying only, no actions will be taken."
			action_query_only=true;;

		-d | --debug )
			shift
			echo "Debugging enabled."
			debug_mode=true ;;

		-l | --list )
			shift
			action_list_packages=true;;

		--htmllist )
			shift
			action_list_packages=true
			action_list_packages_in_html=true;;

		-h | --help | --syntax )
			usage
			exit 1;;

		*)
			usage
			exit 0 ;;
	esac
done

if [[ $action_list_packages == true ]]
then
	list_packages
	exit 0
fi

install_rpm_fusion

apply_changes
