#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: bin/sde-reschedule-dependers
# Copyright (C) 2008 The OpenSDE Project
#
# More information can be found in the files COPYING and README.
#
# 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. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

#Description: Reschedule packages which depend on those given

[ -n "$SDEROOT" ] ||
	export SDEROOT=$( cd "${0%/*}/.."; pwd -P )

. "$SDEROOT/lib/libsde.in"

reschedule_usage() {
	local progname=${0##*/}
	cat <<EOT
Usage:	${progname//-/ } [<options>] <package_1> ...

Supported Options:
	--cfg|-c <config>	process a given config (multiple supported)
	--any|-A		process any available config

	--dry-run|-n	don't really reschedule
	--soft|-s	don't remove the packages when rescheduling

	-t		use theoric dependers (from .cache at \$confdir)
EOT
}

shortopts='c:Asnt'
longopts='help,cfg:,any,soft,dry-run'
options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
if [ $? -ne 0 ]; then
	reschedule_usage
	exit -1
fi

# load new arguments list
eval set -- "$options"

reschedule_any=
reschedule_soft=
reschedule_dry=
reschedule_theoric=

reschedule_configs=

while [ $# -gt 0 ]; do
	case "$1" in
		--help)
			reschedule_usage
			exit 0 ;;

		--any|-A)	reschedule_any=yes ;;
		--soft|-s)	reschedule_soft=yes ;;
		--dry-run|-n)	reschedule_dry=yes ;;

		-t)		reschedule_theoric=yes ;;

		--cfg|-c)	reschedule_configs="$reschedule_configs $2"
				shift ;;

		--)	shift; break ;;
		*)	echo_abort 1 "$1: Unknown argument, aborting."
	esac
	shift
done

. "$SDEROOT/lib/sde-config.in"
. "$SDEROOT/lib/sde-package.in"

reschedule_dependers()
{
	local config="$1" pattern= sandbox=
	local x= y=
	shift

	if [ $# -eq 0 ]; then
		return
	elif [ $# -eq 1 ]; then
		pattern="$1"
	else
		pattern="$*"
		pattern="\(${pattern// /\|}\)"
	fi

	sandbox="$SDEROOT/build/$( config_id "$config" )"
	pattern="^\[DEP\].* $pattern\( .*\)\?$"

	if config_iscross "$config" || [ "$reschedule_theoric" = "yes" ]; then
		ls -1 "$sandbox/var/adm/packages"/* 2> /dev/null | sed -e 's|.*/||' |
		while read x; do
			y=$( package_confdir "$x" )
			if [ -s "$y/$x.cache" ]; then
				if grep -q "$pattern" "$y/$x.cache"; then
					echo $x
				fi
			fi
		done
	else
		grep -l "$pattern" "$sandbox/var/adm/cache"/* 2> /dev/null |
			sed -e 's|.*/||'
	fi
}

# which configs?
#
if [ "$reschedule_any" = "yes" ]; then
	reschedule_configs=$( config_list )

	[ -n "$reschedule_configs" ] || echo_abort 1 "No config found."
elif [ -z "$reschedule_configs" ]; then
	# try default

	y="$( config_id default )"

	if [ -z "$y" ]; then
		echo_abort 1 "No config found."
	elif [ -d "$SDEROOT/build/$y/" ]; then
		reschedule_configs=default
	fi
fi

[ -n "$reschedule_configs" ] || exit 0

y=
for x; do
	if package_exists "$x"; then
		y="$y $x"
	else
		echo_warning "$x: Invalid package"
	fi
done

set -- $y

[ $# -gt 0 ] || exit 0

for x in $reschedule_configs; do
	y=$( reschedule_dependers "$x" "$@" )
	if [ -n "$y" ]; then
		"$SDEROOT/bin/sde-reschedule-package" \
			${reschedule_dry:+--dry-run} \
			${reschedule_soft:+--soft} \
			-c "$x" $y
	fi
done