#!/bin/sh -ef

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

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

AVAIL_TEMPLATES=`ls -1 $COMFORT_DIR/spec/ | sed 's/\.spec$//'`
AVAIL_TEMPLATES=`echo $AVAIL_TEMPLATES`
DESCRIPTION="Usage: $PROG [options] <package-name> [version]
Creates a new empty repository on local machine with a RPM spec. RPM spec
can be generated using templates for several popular package types.

Following spec templates are available:
$AVAIL_TEMPLATES"
SHORT_OPTS='t:'
LONG_OPTS='template:'

parse_option()
{
	case "$1" in
	-t|--template)
		shift
		SPEC_TEMPLATE=$1
		[ -r $COMFORT_DIR/spec/${SPEC_TEMPLATE}.spec ] || Fatal "No \"$SPEC_TEMPLATE\" template found. Available templates: $AVAIL_TEMPLATES"
		return 1
		;;
	*)
		show_usage "wrong option: $1"
		;;
	esac	
}

show_help()
{
	cat <<EOF
$DESCRIPTION

Options:
  -t, --template <type>             select spec template; "application" is used by default.
  -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/
EOF
	exit
}

# Parse command line
SPEC_TEMPLATE=application
. "$COMFORT_DIR/getopt"
PACKAGE=$1
[ -n "$2" ] && SPEC_VERSION=$2 || SPEC_VERSION=1.0

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

[ -d "$GIT_HOME_DIR/$PACKAGE/.git" ] && Fatal "GIT repository for $PACKAGE already exists. Won't overwrite."
[ -e "$GIT_HOME_DIR/$PACKAGE" ] && Fatal "$GIT_HOME_DIR/$PACKAGE already exists and it isn't a GIT repository! Won't overwrite."

mkdir -p "$GIT_HOME_DIR/$PACKAGE"
pushd "$GIT_HOME_DIR/$PACKAGE" >/dev/null
git-init-db
sed -e \
	"s/##NAME##/$PACKAGE/g;
	s/##PACKAGER_EMAIL##/$ALT_USER_EMAIL/g;
	s/##PACKAGER_NAME##/$ALT_USER_NAME/g;" < $COMFORT_DIR/spec/${SPEC_TEMPLATE}.spec >$PACKAGE.spec
git-update-index --add $verbose $PACKAGE.spec
git-commit -m "Initial commit; started spec from template \"$SPEC_TEMPLATE\"" $verbose
popd >/dev/null
