#!/bin/bash

# is-this-pet-installed
#
# by Apollia - http://astroblahhh.com/
#
# 
# A console script for Puppy Linux.
#
# Completed June 24, 2017.
#
#
# Accepts 1 argument - the name or path of a pet file.
#
#
# This script uses a combination of awk and grep to search this list of
# installed .pet files:
#
#		/root/.packages/user-installed-packages
#
# 
# Exit code 0 = Found the pet name given in Arg 1.
#
# Exit code 1 = Searched the list, but the pet name wasn't there.
#
# Exit code 2 = Arg 1 didn't end in ".pet" (case insensitive).
#
# Exit code 3 = Arg 1 was empty.


if [ -z "$1" ]
then
	echo "Please provide a .pet file's name or path."
	exit 3
fi


filename=$(basename "$1")
extension="${filename##*.}"

lowercase_extension=$(echo "$extension" | tr '[:upper:]' '[:lower:]' )


if [ "$lowercase_extension" != "pet" ]
then
	echo "Arg #1 is not a .pet file:
	
	$1"
	
	exit 2
fi


Fillepath____user_installed_packages="/root/.packages/user-installed-packages"

awk 'BEGIN { FS = "|" } ; { print $8 }' "$Fillepath____user_installed_packages" | grep -x "$1" > /dev/null

result_code="$?"

if [ "$result_code" = 0 ]
then
	echo "Seems to be installed:
	
	$filename"
fi


if [ "$result_code" = 1 ]
then
	echo "Doesn't seem to be installed:

	$filename"
fi

exit "$result_code"