#! /bin/bash
#
# is_installed - a script to find if a package
# is currently installed on Slackware and derivates
#
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# var
SHOWHELP=69
E_NOPKGFOUND=70
E_NOARGS=71
PKGS_PATH="/var/log/packages"

PROG="${0##*/}"


# colors (disabled when stdout is not a terminal, e.g. piped)
if [ -t 1 ]; then
	RED='\033[1;31m'
	ORANGE='\033[1;33m'
	GREEN='\033[1;32m'
	MAGENTA='\033[1;35m'
	NC='\033[0m'
else
	RED='' ORANGE='' GREEN='' MAGENTA='' NC=''
fi

function search_file () {
	SEARCH=$1
	if [ -z "$SEARCH" ]; then
		#we don't have a string to search
		echo -e "Usage: ${MAGENTA}$PROG ${NC}[file-search] ${GREEN}< filename >"
		echo -e "${RED}exiting${NC}"
		exit $E_NOARGS
	fi
	finding=$(grep -Hi "$SEARCH" "${PKGS_PATH}"/*)
	if [[ -n $finding ]]; then
		old_pkgname=""
		while IFS= read -r line; do
			pkgname="${line%%:*}"
			match="${line#*:}"
			if [[ $pkgname != $old_pkgname ]]; then
				echo -e "${ORANGE}${pkgname##*/}${NC}"
			fi
			old_pkgname=$pkgname
			echo -en "\t${GREEN}$match${NC}\n"
		done <<< "$finding"
	else
		echo -e "${ORANGE}no match for pattern ${GREEN}'$SEARCH' ${ORANGE}found"
		echo -e "${RED}exiting${NC}"
		exit $E_NOPKGFOUND
	fi
}

function search_package () {
	SEARCH=$1
	if [ -z "$SEARCH" ]; then
		#we don't have a string to search
		echo -e "Usage: ${MAGENTA}$PROG ${GREEN}<filename>"
		echo -e "${RED}exiting${NC}"
		exit $E_NOARGS
	fi
	finding=$(ls -1 "$PKGS_PATH" | grep -i "$SEARCH")
	if [[ -n $finding ]]; then
		echo -en "${GREEN}$finding\n"
		echo -e "${NC}"
	else
		echo -e "${ORANGE}no package matching ${GREEN}'$SEARCH' ${ORANGE}found"
		echo -e "${RED}exiting${NC}"
		exit $E_NOPKGFOUND
	fi
}

function info () {
	SEARCH=$1
	if [ -z "$SEARCH" ]; then
		#we don't have a string to search
		echo -e "Usage: ${MAGENTA}$PROG ${ORANGE}info ${GREEN}<filename>"
		echo -e "${RED}exiting${NC}"
		exit $E_NOARGS
	fi
	mapfile -t matches < <(ls -1 "$PKGS_PATH" | grep -i "$SEARCH")
	if [[ ${#matches[@]} -eq 1 ]]; then
		less "$PKGS_PATH/${matches[0]}"
	elif [[ ${#matches[@]} -gt 1 ]]; then
		# multiple matches: list them, do not feed garbage to less
		echo -e "${ORANGE}multiple packages match ${GREEN}'$SEARCH'${ORANGE}, be more specific:${NC}"
		printf "${GREEN}\t%s\n${NC}" "${matches[@]}"
		exit $E_NOPKGFOUND
	else
		echo -e "${ORANGE}no package matching ${GREEN}'$SEARCH' ${ORANGE}found"
		echo -e "${RED}exiting${NC}"
		exit $E_NOPKGFOUND
	fi
}

function help () {
	echo
	echo -e "${MAGENTA}$PROG${NC} -\tSearch for installed packages in the system"
	echo -e "\t\tand show which package contains a specific file."
	echo -e "${GREEN}USAGE:"
	echo -e "\t${MAGENTA}$PROG ${ORANGE}[ file-search ] [ help ] ${GREEN}< item >"
	echo -e "\t${MAGENTA}$PROG ${GREEN}<item>${NC} - Search between all installed packages for ${GREEN}item${NC}."
	echo -e "\t${MAGENTA}$PROG ${ORANGE}file-search ${GREEN}<item>${NC} - Search for ${GREEN}item${NC} inside every package installed."
	echo -e "\t${MAGENTA}$PROG ${ORANGE}help${NC} - Show this help and exit."
	echo -e "${NC}"
}
# controlla se vengono passati parametri
if [ -z "$1" ]; then
	help
	exit $E_NOARGS
else
	case $1 in
		"file-search" )
			search_file "$2"
			;;
		"info" )
			info "$2"
			;;
		"help" )
			help
			exit $SHOWHELP
			;;
		* )
			search_package "$1"
			;;
	esac
fi


exit
