#!/bin/bash
# apw, a simple packager wrapper for Arch, Debian, RedHat/Fedora, CentOS 6, Suse, and all his derivates)
# It simplifies the process of remembering pacman,apt,dnf, etc , all in one called apw
# Author: JKA Network - contacto@jkanetwork.com

# $1 is the command
# $2,$3.. will be $packages

# gettext initialization
export TEXTDOMAIN='apw'
export TEXTDOMAINDIR='/usr/share/locale'
#Version variable
version="1.7.1"

#This option is for make if case insensitive
shopt -s nocasematch

#Basic info echo
case "$1" in
	*h)
		printf -- "$(gettext "apw - A Packager Wrapper, a Idea from JKA Network, version %s")" "$version"
		echo
		echo "$(gettext "	Usage: apw <option> [packages]")"
		echo "$(gettext "	{-h | h} 	Show this help")"
		echo "$(gettext "	{-U | U}	Update system. If errors, you can do -Uf to force it")"
		echo "$(gettext "	{-I | I}	Install packages (Updates system also for preventing problems)")"
		echo "$(gettext "	{-Ir | Ir}	Reinstall packages (Updates system also for preventing problems)")"
		echo "$(gettext "	{-R | R}	Removes a package")"
		echo "$(gettext "	{-Rd | Rd}	Removes a package along with dependencies that are not more in use (Only for Arch)")"
		echo "$(gettext "	{-C | C}	Clean temp files")"
		echo "$(gettext "	{-Cd | Cd}	Clean system from dependencies that are not in use (ADVANCED, BE CAREFUL!)")"
		echo "$(gettext "	{-S | S}	Search in package database")"
		echo "$(gettext "	{-F | F}	Install file (distro family dependent, e.g. .deb , .pkg.tar.xz,...)")"
		echo "$(gettext "	Append y to -U or -I (-Uy -Iy -Iry) for autoconfirm the operations")"
		exit
		;;
	"")
		echo "$(gettext "apw - A Packager Wrapper")"
		echo "$(gettext "Use apw -h for help")"
		exit
		;;
esac

#Force sudo
uid=$(/usr/bin/id -u)
if [[ $uid != "0" ]];then
echo "$(gettext "apw has to run as root, please run as root/with sudo")"
exit
fi

if [[ ! -f /etc/apw.conf ]];then
	echo "$(gettext "It's the first run of the program, we need a thing before continue")"
	echo "$(gettext "What, or in what is based your Linux?")"
	echo "$(gettext "(The answer will be recorded in /etc/apw.conf, if you want to reset apw, delete this file)")"
	options=("Debian/Ubuntu (apt)" "Fedora/Redhat (dnf)" "CentOS 6 (yum)" "Archlinux (pacman)" "Suse (zypper)")
	select opt in "${options[@]}"
	do
		case $opt in
			"Debian/Ubuntu (apt)")
			echo "apt" > /etc/apw.conf
			break
			;;
			"Fedora/Redhat (dnf)")
			echo "dnf" > /etc/apw.conf
			break
			;;
			"CentOS 6 (yum)")
			echo "yum" > /etc/apw.conf
			break
			;;
			"Archlinux (pacman)")
			echo "pacman" > /etc/apw.conf
			break
			;;
			"Suse (zypper)")
			echo "zypper" > /etc/apw.conf
			break
			;;
		esac
	done
fi

SO=$(cat /etc/apw.conf)

#Reading packages to install/remove.. (The rest of $) , it has to be done before the option echo, logically
packages=$2
#Control the incompleted arguments
if [[ -z $packages && ( $1 != *u* && $1 != *c* ) ]];then
	echo "$(gettext "No package specified, aborting")"
	exit
fi
for (( i=3; i<=$#; i++ ))
do
	valtmp=$(echo $* | cut -f$i -d" ")
	packages="$packages $valtmp"
done

#Check if is dnf in RedHat, or apt in Debian, new tools
if [[ $SO = "dnf" && ! -f /usr/bin/dnf ]];then
	yum install dnf
fi
if [[ $SO = "apt" && ! -f /usr/bin/apt ]];then
	apt-get update && apt-get install apt
fi

#Command wrapper
updating="$(gettext "Updating system")"
case "$1" in
	*U)
		echo $updating
		case "$SO" in
			apt)
				apt update && apt upgrade;;	
			pacman)
				pacman -Syu $packages;;
			yum | dnf | zypper)
				$SO update;;
		esac;;
	*Uf)
		echo $(gettext "Force updating system")
		case "$SO" in
			apt)
				apt update && apt upgrade -f;;	
			pacman)
				pacman -Syu $packages --force;;
			yum | dnf | zypper)
				echo "$(gettext "Not supported on your distribution. Doing -U instead")"
				$SO update;;
		esac;;
	*Uy)
		echo $updating
		case "$SO" in
			apt)
				apt update && apt -y upgrade;;	
			pacman)
				pacman -Syu $packages --noconfirm;;
			yum | dnf | zypper)
				echo "$(gettext "Not supported on your distribution. Doing -U instead")"
				$SO update;;
		esac;;
	*I)
		printf -- "$(gettext "You are going to install %s and dependencies")" "$packages"
		echo
		case "$SO" in
			apt)
				apt update && apt install $packages;;
			pacman)
				pacman -Syu $packages --needed;;
			yum | dnf | zypper)
				$SO install $packages;;
		esac;;
	*Iy)
		printf -- "$(gettext "You are going to install %s and dependencies")" "$packages"
		echo
		case "$SO" in
			apt)
				apt update && apt -y install $packages;;
			pacman)
				pacman -Syu $packages --noconfirm --needed;;
			yum | dnf | zypper)
				echo "$(gettext "Not supported on your distribution. Doing -I instead")"
				$SO install $packages;;
		esac;;
	*Ir)
		printf -- "$(gettext "You are going to reinstall %s")" "$packages"
		echo
		case "$SO" in
			apt)
				apt update && apt install --reinstall $packages;;
			pacman)
				pacman -Syu $packages;;
			yum | dnf | zypper)
				$SO reinstall $packages;;
		esac;;
	*Iry)
		printf -- "$(gettext "You are going to reinstall %s")" "$packages"
		case "$SO" in
			apt)
				apt update && apt -y install --reinstall $packages;;
			pacman)
				pacman -Syu $packages --noconfirm;;
			yum | dnf | zypper)
				echo "$(gettext "Not supported on your distribution. Doing -Ir instead")"
				$SO reinstall $packages;;
		esac;;
	*R)
		printf -- "$(gettext "You are going to remove %s")" "$packages"
		case "$SO" in
			apt)
				apt remove $packages;;
			pacman)
				pacman -R $packages;;
			yum | dnf | zypper)
				$SO remove $packages;;
		esac;;
	*Ry)
		printf -- "$(gettext "You are going to remove %s")" "$packages"
		case "$SO" in
			apt)
				apt -y remove $packages;;
			pacman)
				pacman -R $packages --noconfirm;;
			yum | dnf | zypper)
				echo "$(gettext "Not supported on your distribution. Doing -R instead")"
				$SO remove $packages;;
		esac;;
	*Rd)
		printf -- "$(gettext "Removing %s along with his dependencies that are not more in use")" "$packages"
		echo
		case "$SO" in
			pacman)
				pacman -Rs $packages;;
			*)
			echo "$(gettext "Not supported on your distribution")";;
		esac;;
	*Rdy)
		printf -- "$(gettext "Removing %s along with his dependencies that are not more in use")" "$packages"
		echo
		case "$SO" in
			pacman)
				pacman -Rs $packages --noconfirm;;
			*)
			echo "$(gettext "Not supported on your distribution")";;
		esac;;
	*C)
		echo "$(gettext "Cleaning package manager")"
		case "$SO" in
			apt)
				apt clean
				apt autoclean
				#apt autoremove
				;;
			pacman)
				pacman -Scc;;
			yum)
				yum clean all;;
			dnf)
				dnf clean all --enablerepo=\*;;
			zypper)
				echo "$(gettext "Not supported on your distribution")";;
		esac;;
	*Cd)
		echo "$(gettext "Cleaning system from dependencies that are not in use (BE CAREFUL, ONLY ADVANCED)")"
		case "$SO" in
			apt)
				apt autoremove --purge;;
			pacman)
				pacman -Rs $(pacman -Qdtq);;
			*)
				echo "$(gettext "Not supported on your distribution")";;
		esac;;
	*S)
		printf -- "$(gettext "Searching %s")"  "$packages"
		echo
		case "$SO" in
			pacman)
				pacman -Ss $packages;;
			apr | yum | dnf | zypper)
				$SO search $packages;;
		esac;;
	*F)
		printf -- "$(gettext "Installing files: %s")" "$packages"
		echo
		case "$SO" in
			apt)
				dpkg -I $packages;;
			pacman)
				pacman -U $packages;;
			yum | dnf | zypper)
				rpm -Uvh $packages;;
		esac;;
	*)
		echo "$(gettext "Invalid option especified")"
		echo "$(gettext "Use apw -h for help")"
		exit;;
esac

if [[ $SO = "pacman" ]];then
	case "$1" in
		*Rd)
			pacman -Rs $packages;;
	esac
fi
