#!/bin/sh -eu

arches=
move_opt=
destdir=
addon_flavour=

PROG=`basename $0`
show_help()
{
	cat <<EOF
$PROG - create repofork add-on repo from autorepo TASK.GOOD build results

Usage: $PROG [options] addrepo1 ... addrepoN

Options:
  --addon-component-name=<name>  addon repository component name (default is $addon_flavour)
  --arch="i586 x86_64"           binary architectures to add
  --destdir=/path                where to create the add-on repository (default is $destdir)
  --cp                           copy files (default)
  --mv                           move files
  --ln                           hard link files
  -h, --help                     show this text and exit.

Report bugs to http://bugs.altlinux.ru/

EOF
}

show_usage()
{
	show_help
	exit
}

parse_common_option()
{
	echo "ERROR: unsupported option $1"
	show_usage
}

TEMP=`getopt -n $PROG -o h -l addon-component-name:,arch:,cp,destdir:,help,ln,mv -- "$@"` ||
	show_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		--arch) shift; arches="$1";
			;;
		--destdir) shift; destdir="$1";
			;;
		--addon-component-name) shift; $addon_flavour="$1";
			;;
		--cp) move_opt="--cp";
			;;
		--ln) move_opt="--ln";
			;;
		--mv) move_opt="--mv";
			;;
		--help|-h) show_help; exit;
			;;
		--) shift; break
			;;
		*) parse_common_option "$1"
			;;
	esac
	shift
done

GOOD=$1
for i in `ls $GOOD | sed -e 's,^\(.\).*,\1,'| sort -u` ; do
    echo "creating $GOOD char $i ..."
    repofork-create-addon-repo-from-autorepo-taskgood-subset ${arch:+--arch "$arches"} ${destdir:+--destdir "$destdir"} ${addon_flavour:+--addon-component-name "$addon_flavour"} ${move_opt:+$move_opt} $GOOD/${i}*
done
