#!/bin/sh
#
# FreeBSD Port Tools 1.11
#
# Copyright (c) 2003-2009, Sergei Kolobov
# Copyright (c) 2013-2014, Mathieu Arnold
# Copyright (c) 2014, Johannes Meixner
# Copyright (c) 2014-2015, Steven Kreuzer
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# cmd_create
# Module for port(1)
# SUMMARY: create a new port from a template

# Check if this script is run via port(1)
if [ "${PORTTOOLS}" = "" ]; then
	echo "This script should be run via port(1) front-end"
	exit 1
fi

usage ()
{
cat << EOF
FreeBSD Port Tools 1.11
Usage: port create [-h] [-a <author>] [-e <email>] <portname>
	-h	- Display this usage summary
	-a	- Override author name set in ~/.porttools
	-e	- Override email set in ~/.porttools
EOF
}

ARGS=`/usr/bin/getopt h:a:e: $*`
if [ $? != 0 ]
then
	echo "Error: invalid arguments"
	usage
	exit 2
fi

set -- $ARGS
while [ x"$1" != x"--" -a x"$1" != x"" ]
do
	i=$1
	case "$i" in
	# help
	-h)
		usage
		exit 0
		;;
	-a)
		FULLNAME=$2
		shift
		;;
	-e)
		EMAIL=$2
		shift
		;;
	esac
	shift
done

TEMPLATE_PREFIX=default
ARG_PORTNAME=$2
# Check if port name was supplied
if [ "${ARG_PORTNAME}" = "" ]; then
	echo "Error: port name not specified"
	exit 1
fi

# Check if specified dir already exist
if [ -e "${ARG_PORTNAME}" ]; then
	echo "Error: ${ARG_PORTNAME} already exists"
	exit 1
else
	mkdir -p ${ARG_PORTNAME}
fi

CATEGORIES=$(basename ${PWD})
PORTBASE=$(basename ${ARG_PORTNAME})
PORTPREFIX=$(echo ${PORTBASE} | cut -s -d- -f1)

# Work around the fact that R modules are
# prefixed with R-cran
if [ "${PORTPREFIX}" = 'R' ]
then
	SECONDPORTPREFIX=$(echo ${PORTBASE} | cut -d- -f2)
	if [ "${SECONDPORTPREFIX}" = 'cran' ]
	then
		PORTPREFIX="R-cran"
	fi
fi

PORTNAME=${PORTBASE}

if [ ! -z ${PORTPREFIX} ]
then
	# Check to make sure that a template for this prefix exists
	# before changing the value of TEMPLATE_PREFIX from default
	# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=204001
	if [ -d ${TEMPLATE_DIR}/${PORTPREFIX} ]
	then
		# Change the location of the port template files to
		# use from default to the port prefix
		TEMPLATE_PREFIX=${PORTPREFIX}

		# Strip the port prefix from PORTNAME
		PORTNAME=$(echo ${PORTBASE} | sed "s|${PORTPREFIX}-||")
	fi
fi

case ${PORTPREFIX} in
	p5)
		CATEGORIES="${CATEGORIES} perl5"
		MASTER_SITES=CPAN
		PKGNAMEPREFIX=p5-
		WWW=https://metacpan.org/dist/${PORTNAME}/
		WANT_PKG_PLIST=Y
		;;
	R-cran)
		WWW=https://cran.r-project.org/web/packages/${PORTNAME}/
		;;
	pear)
		CATEGORIES="${CATEGORIES} pear"
		WWW=https://pear.php.net/package/${PORTNAME}/
		;;
	py)
		CATEGORIES="${CATEGORIES} python"
		WWW=https://pypi.python.org/pypi/${PORTNAME}
		;;
	rubygem)
		CATEGORIES="${CATEGORIES} rubygems"
		WWW=https://rubygems.org/gems/${PORTNAME}
		;;
	*)
		WWW=https://example.com
		WANT_PKG_PLIST=Y
		;;
esac

if [ ! -z ${WANT_PKG_PLIST} ]
then
	cp ${TEMPLATE_DIR}/common/pkg-plist.tmpl ${ARG_PORTNAME}/pkg-plist
fi

# Create pkg-descr
cp ${TEMPLATE_DIR}/common/pkg-descr.tmpl ${ARG_PORTNAME}/pkg-descr

# Create Makefile
sed "s|%%PORTNAME%%|${PORTNAME}|g;
	s|%%CATEGORIES%%|${CATEGORIES}|g;
	s|%%MASTER_SITES%%|${MASTER_SITES}|g;
	s|%%FULLNAME%%|${FULLNAME}|g;
	s|%%EMAIL%%|${EMAIL}|g;
	s|%%WWW%%|${WWW}|g;" < ${TEMPLATE_DIR}/${TEMPLATE_PREFIX}/Makefile.tmpl > ${ARG_PORTNAME}/Makefile
