#!/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.
# port(1)
# Front-end interface to Port Tools functions

# Port Tools configuration file
PORTTOOLS="${HOME}/.porttools"

# Check to see the config file exists; generate one if not
if [ ! -f ${PORTTOOLS} ]
then
	echo "===> Generating ${PORTTOOLS} configuration file"
	cat > ${PORTTOOLS} <<- EOF
	# FreeBSD Port Tools configuration file - see porttools(5)
	# vim: ft=sh
	EMAIL="${USER}@`hostname`"
	FULLNAME="`pw usershow -n ${USER} | cut -d: -f8`"
	ORGANIZATION=""
	BCC=""
	CC=""
	BUILDROOT="/tmp"
	ARCHIVE_DIR=""
	DIFF_MODE="GIT"
	DIFF_VIEWER="more"
	PORTLINT_FLAGS="abct"
	PORTSDIR="/usr/ports"
	EOF
fi

if [ -z "$PORTSDIR" ]; then
	PORTSDIR="/usr/ports"
else
	PORTSDIR_OVERRIDE="$PORTSDIR"
fi

# Reading configuration file
. ${PORTTOOLS}

[ ! -z "$PORTSDIR_OVERRIDE" ] && export PORTSDIR="$PORTSDIR_OVERRIDE"

# Port Tools scripts location
SCRIPT_DIR="/usr/local/share/porttools"
TEMPLATE_DIR="${SCRIPT_DIR}/tmpl"

# Run specified command
CMD="$1"
shift
[ -n "${CMD}" -a -r ${SCRIPT_DIR}/cmd_${CMD} ] || CMD="help"
. ${SCRIPT_DIR}/cmd_${CMD}
