#!/bin/bash -ue

<< MIT-LICENSE
Copyright (c) 2023 Jianjun Liu<jianjunliu@126.com> https://www.pkglinux.top

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT-LICENSE

offer-usage()
{
	# Display Help
	echo
	echo "Create a simplest db for checking which PLIST file offers the command.";
	echo
	echo "Syntax: $0 [ -h|v|V|c ] [ --help|verbose|version|create ] [ creatingdir ] [ dbname ] [ cmd ]"
	echo "options:"
	echo "h     Print this help."
	echo "v     Verbose mode."
	echo "V     Print software version and exit."
	echo "c     creating mode."
	echo
}

offer-need-commands() {
	for arg in "$@"; do
		command -v $arg  > /dev/null || ( echo $0 needs "\"$arg\"" command;  exit 1 )
	done
}

for arg in "$@"; do
	shift
	case "$arg" in
		"--help")	set -- "$@" "-h" ;;
		"--version")	set -- "$@" "-V" ;;
		"--verbose")	set -- "$@" "-v" ;;
		"--create")	set -- "$@" "-c" ;;
		*)		set -- "$@" "$arg"
	esac
done

VERSION="2022Q2"
VERBOSE=""
INQUERY="yes"

while getopts ':hVvc' OPTION; do
	case "$OPTION" in
		h)
			offer-usage
			exit 0 ;;
		V)
			echo "$VERSION"
			exit 0 ;;
		v)
			VERBOSE="-v" ;;
		c)
			INQUERY="" ;;
		*)
			offer-usage
			exit 1 ;;
	esac
done

# Remove all options passed by getopts options
shift "$(($OPTIND -1))"

[ "$#" -gt 0 ] || ( offer-usage; exit 1 )

OFFER_DB="$(basename $0).$VERSION"


if [ -z "$INQUERY" ]; then

offer-need-commands mkdir rm tar gzip dirname basename nproc xargs

offer-register() {
(
	cd $1
	grep $4 "^@" $2 | while read f; do
		local f="${f//$/$_}"
		local r="${f// /__}"
		mkdir $4 -p "$(dirname $3/$r)"
		[ -d "$3/$r" ] && continue
		[ -z "$4" ] || echo "$2 >> $3/$r"
		echo "$2" >> "$3/$r"
	done
)
}

export -f offer-register

	[ -d "$1" ] || ( echo $1 is not a directory; exit 1 )
	d="/sda3/tmp/$OFFER_DB"
	rm -rf $VERBOSE "$d"
	mkdir $VERBOSE -p "$d"
	(
		cd $1
		ls */*/PLIST | while read p; do
	       		echo "$1 $p $d $VERBOSE" | xargs -n 4 -P $(nproc) sh -c 'offer-register "$@"' _
		done
		tar $VERBOSE -czf $OFFER_DB.tar.gz -C $d *
		rm -rf $VERBOSE $d
	)
else

	offer-need-commands tar gzip

	[ -f "$OFFER_DB.tar.gz" ] || ( echo "$OFFER_DB.tar.gz doesn't exists"; exit 1 )
	tar -tzof $1.tar.gz "*/$1" | while read f; do
		tar -xzof $OFFER_DB.tar.gz $f | cat
	done
fi
