#!/bin/sh
#
# cook-new - SliTaz cook: create a new package directory in the wok
# with a fresh receipt skeleton. Optional interactive mode prompts
# for the standard fields.
#
# Usage:
#   cook-new <pkg>            # create $WOK/<pkg>/ with template receipt
#   cook-new <pkg> -x         # interactive prompts to fill the receipt
#   cook-new <pkg> --interactive
#
# Copyright (C) SliTaz GNU/Linux - GNU GPL v3
#

. /usr/lib/slitaz/libcook.sh


usage() {
	cat <<EOT

$(boldify "$(_ 'Usage:')") cook-new <pkg> [--interactive|-x]

$(boldify "$(_ 'Commands:')")
  usage|help|--help|-h $(_ 'Display this short usage.')
  <pkg>                $(_ 'Create $WOK/<pkg>/ from the receipt template.')

$(boldify "$(_ 'Options:')")
  --interactive | -x   $(_ 'Prompt for version, category, maintainer, etc.')

$(boldify "$(_ 'Examples:')")
  cook-new foo                 $(_ 'create a minimal foo receipt')
  cook-new foo -x              $(_ 'interactive: fill the standard fields')

EOT
	exit 0
}


pkg="$1"

case "$pkg" in
	usage|help|--help|-h|'') usage ;;
esac

newline
[ -d "$WOK/$pkg" ] && die 'Package "%s" already exists.' "$pkg"

action 'Creating folder "%s"' "$WOK/$pkg"
mkdir $WOK/$pkg; cd $WOK/$pkg; status

action 'Preparing the package receipt...'
cp $DATA/receipt .
sed -i "s|^PACKAGE=.*|PACKAGE=\"$pkg\"|" receipt
status; newline


# Interactive mode

case "$2" in
	--interactive|-x)
		_ 'Entering interactive mode...'
		separator
		_  'Package       : %s' "$pkg"

		_n 'Version       : ' ; read answer
		sed -i "s|^VERSION=.*|VERSION=\"$answer\"|" receipt

		_n 'Category      : ' ; read answer
		sed -i "s|^CATEGORY=.*|CATEGORY=\"$answer\"|" receipt

		# L10n: Short description
		_n 'Short desc    : ' ; read answer
		sed -i "s|^SHORT_DESC=.*|SHORT_DESC=\"$answer\"|" receipt

		_n 'Maintainer    : ' ; read answer
		sed -i "s|^MAINTAINER=.*|MAINTAINER=\"$answer\"|" receipt

		_n 'License       : ' ; read answer
		sed -i "s|^LICENSE=.*|LICENSE=\"$answer\"|" receipt

		_n 'Web site      : ' ; read answer
		sed -i "s|^WEB_SITE=.*|WEB_SITE=\"$answer\"|" receipt
		newline

		# Wget URL.
		_ 'Wget URL to download source tarball.'
		_n 'Example  : ' ; echo '$GNU_MIRROR/$PACKAGE/$TARBALL'
		_n 'Wget url : ' ; read answer
		sed -i "s|^WGET_URL=.*|WGET_URL=\"$answer\"|" receipt

		# Ask for a stuff dir.
		confirm "$(_n 'Do you need a stuff directory? (y/N)')"
		if [ "$?" -eq 0 ]; then
			action 'Creating the stuff directory...'
			mkdir $WOK/$pkg/stuff; status
		fi

		# Ask for a description file.
		confirm "$(_n 'Are you going to write a description? (y/N)')"
		if [ "$?" -eq 0 ]; then
			action 'Creating the "%s" file...' 'description.txt'
			touch $WOK/$pkg/description.txt; status
		fi

		footer "$(_ 'Receipt is ready to use.')" ;;
esac
