#!/bin/sh -ef
# Usage: Sisyphus-archive-import <package-name>

[ -n "$COMFORT_DIR" ] || COMFORT_DIR=/usr/share/comfort
. "$COMFORT_DIR/functions"

# ======================================================================

DESCRIPTION="Usage: $PROG <package-name> <command> <user>
Manage Sisyphus access control lists.
"
SHORT_OPTS='f,c'
LONG_OPTS='force,clear'

parse_option()
{
	case "$1" in
	-f|--force)
		FORCE=1
		;;
	-c|--clear)
		CLEAR_CMD=1
		;;
	*)
		show_usage "wrong option: $1"
		;;
	esac	
}

show_help()
{
	cat <<EOF
$DESCRIPTION
Commands:
  add <user>
  remove <user>
  nmu <user>
  leader <user>

Options:
  -f, --force                       force applying the ACL;
  -c, --clear                       clear all previous commands;
  -q, --quiet                       try to be more quiet;
  -v, --verbose                     print a message for each action;
  -V, --version                     print program version and exit;
  -h, --help                        show this text and exit.
    
Report bugs to http://bugs.altlinux.ru/
More info on http://wiki.sisyphus.ru/devel/Incoming/notes
EOF
	exit
}

# Parse command line
. "$COMFORT_DIR/getopt"

# ======================================================================

# List packages
step_1_help()
{
	echo 'Your packages:'
	echo $MY_PACKAGES
	echo
	echo 'Select one package from this list.'
}

# List commands
step_2_help()
{
	echo 'Command required. Available commands:'
	echo "Sisyphus-acl $PACKAGE add"
	echo "Sisyphus-acl $PACKAGE remove"
	echo "Sisyphus-acl $PACKAGE nmu"
	echo "Sisyphus-acl $PACKAGE leader"
}

# ======================================================================

SISYPHUS_DIR=/Sisyphus
MY_PACKAGES=`cat $SISYPHUS_DIR/files/list/list.src.classic | grep "	$ALT_USER" | cut -f1`

# Step 1 - need help - list packages
if [ "$#" -eq 0 ]; then
	if [ -n "$CLEAR_CMD" ]; then
		NOTEFILE=`mktemp -t`
		rsync --inplace $NOTEFILE incoming.alt:/incoming/notes/Sisyphus/$ALT_USER
		rm -f $NOTEFILE
		echo 'All previous ACL commands cleared.'
		exit 0
	else
		echo "$DESCRIPTION"
		step_1_help
		exit 1
	fi
fi

# Determine package; check if we're its' lead maintainer
PACKAGE=$1
shift
if [ "$FORCE" != 1 ]; then
	if ! grep -q "^$PACKAGE	" $SISYPHUS_DIR/files/list/list.src.classic; then
		echo "$DESCRIPTION"
		echo "Unable to find package '$PACKAGE'"
		step_1_help
		exit 2
	fi
	if ! grep -q "^$PACKAGE	$ALT_USER" $SISYPHUS_DIR/files/list/list.src.classic; then
		echo "$DESCRIPTION"
		echo "Selected package: $PACKAGE"
		echo "You have no rights to change permissions for this package"
		exit 2
	fi
fi

# Step 2 - need help - list commands
if [ "$#" -eq 0 ]; then
	echo "$DESCRIPTION"
	echo "Selected package: $PACKAGE"
	step_2_help
	exit 1
fi

# Determine command
CMD=$1
shift
[ "$CMD" == 'remove' ] && CMD=rem

# Step 3 - need help - list users
if [ "$#" -eq 0 ]; then
	echo "$DESCRIPTION"
	echo 'You have to specify maintainer(s).'
	exit 1
fi

# Execute the command
case "$CMD" in
add|rem|nmu|leader)
	NOTEFILE=`mktemp -t`
	ensure_ssh_key
	[ -n "$CLEAR_CMD" ] || rsync incoming.alt:/incoming/notes/Sisyphus/$ALT_USER $NOTEFILE
	echo "$PACKAGE $CMD $@" >>$NOTEFILE
	rsync --inplace $NOTEFILE incoming.alt:/incoming/notes/Sisyphus/$ALT_USER
	rm -f $NOTEFILE
	echo "ACL command executed. Robot's report will arrive by e-mail in 1 hour."
	;;
*)
	show_usage "invalid command: $CMD"
	;;
esac
