#!/bin/bash
# installer feature kickstarter from a template:
# will take $TEMPLATE and turn it into new module skeleton
# to start working on without the need to manually do all
# the needed substitutions (or create from scratch)

# 2008, Michael Shigorin <mike@altlinux.org>
# use freely

# initial setup
error() { echo -e "$0: $*" >&2; exit 1; }
usage() { error "Usage:\n$0 feature-name [feature description]"; }

STUB="@name@"
DESC="@description@"
TEMPLATE="/usr/share/installer-sdk/template"

# otmazki
[ -d "$TEMPLATE" ] || error "need $TEMPLATE"

[ "$#" = 0 -o "$1" = "-h" -o "$1" = "--help" ] && usage

echo "Setting feature name to \"$1\""

FEATURE="$1"; shift
NAME="installer-feature-$FEATURE"
DESCRIPTION="installer feature: $FEATURE"

[ -d "$NAME" ] && error "$NAME directory already exists"

# well, we're busy
[ "$#" = 0 ] && {
	echo "No description given, leaving default:"
} || {
	echo "Setting package description to:"
	DESCRIPTION="$*"
}
echo "   $DESCRIPTION"
echo

# now let's make it happen
cp -a "$TEMPLATE" "$NAME"

find "$NAME" \
	-depth \
	-name "*$STUB*" \
	-execdir sh -c "orig={}; mv \$orig \${orig/$STUB/$FEATURE}" \;

find "$NAME" \
	-type f \
	-execdir sed -i "s/$STUB/$FEATURE/g; s|$DESC|$DESCRIPTION|g" "{}" \;

# final touch
PACKAGER="`rpm --eval %packager`"
SPEC="$NAME/$NAME.spec"
[ "$PACKAGER" = "%packager" ] && {
	echo "You'll probably need to set the Packager tag properly"
	echo "before using add_changelog"
} || {
	sed -i "s/^Packager:.*$/Packager: $PACKAGER/; /^%changelog/ q" "$SPEC" &&
	add_changelog -e '- init with installer-sdk' "$SPEC"
}

# ...and look what happened
cd "$NAME"
[ -x /usr/bin/git-init-db ] && git-init-db && echo
[ -x /usr/bin/tree ] && tree "$NAME" || { pwd; ls -l --color=auto . "$NAME"; }

# farewell!
echo
echo "Welcome to your brand new installer feature!"
echo "You might want to just remove unneeded *install.d/ parts"
echo
echo "See also:"
echo "* http://wiki.sisyphus.ru/devel/installer/features"
echo "* https://lists.altlinux.org/mailman/listinfo/devel-conf"
echo "* http://sisyphus.ru/find.shtml?request=installer-feature"
echo "Good luck!"
echo
