#!/bin/sh
##
#  Korinf project
#
#  Check built packages
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005-2009
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  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 Affero General Public License for more details.

#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

# check package existing by REBUILDLIST
check_built_package()
{
	local BUILDNAME=$1
	local NAME FILEDATE FILESIZE BASENAME FILENAME
	local SRCPKG

	if [ ! -d "$TARGETPATH" ] ; then
		echo "Path TARGETPATH:'$TARGETPATH' is missed"
		return 1
	fi

	SRCPKG=$(get_src_package "$SOURCEPATH" $BUILDNAME)
	[ -n "$QUIET" ] || echo "Source package: $SRCPKG"

	for i in $(get_distro_list $REBUILDLIST) ; do

		NAME=$i
		ST="   "
		test -L "$TARGETPATH/$i" && NAME="$NAME [L]"

		FILENAME=$(get_bin_package "$TARGETPATH/$i" $BUILDNAME check)

		if [ -r "$FILENAME" ] ; then
			FILEDATE=`stat -c"%y" $FILENAME | sed -e "s|:[0-9][0-9]\..*$||"`
			FILESIZE=`stat -c"%s" $FILENAME`
			BASENAME=`basename $FILENAME`
			[ "$SRCPKG" -nt "$FILENAME" ] && ST="OBS"
			[ -f "$TARGETPATH/$i/log/$BUILDNAME.autobuild.failed" ] && ST=" AF"
		else
			if [ -d "$TARGETPATH/$i/" ] ; then
				FILEDATE="MISSED"
			else
				FILEDATE="MISDIR"
			fi
			FILESIZE=""
			BASENAME="$BUILDNAME"
		fi

		printf "%22s: %17s  %8s %s %s" "$NAME" "$FILEDATE" "$FILESIZE" "$ST" "$BASENAME"
		test -L "$TARGETPATH/$i" && echo -n " (link to `readlink $TARGETPATH/$i`)"
		echo
	done
	echo "Legend: OBS - obsoleted build, AF - autobuild failed"
}

# args: BUILDNAME
# use external $BUILDSRPM $DESTDIR $MAINFILESLIST
do_need_build_package()
{
	local BUILDNAME=$1
	local FILENAME

	FILENAME=$(get_bin_package "$DESTDIR" $BUILDNAME check)
	# (extra dir already checked in get_bin_package)

	if [ -r "$FILENAME" ] ; then
		[ "$BUILDSRPM" -nt "$FILENAME" ] || return 1
	fi

	return 0
}
