#!/bin/sh
#
# $NetBSD: distfetch,v 1.1.1.1 2002/09/30 08:37:14 martti Exp $
#
# Copyright (c) 2002 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Martti Kuparinen.
#
# 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.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by the NetBSD
#        Foundation, Inc. and its contributors.
# 4. Neither the name of The NetBSD Foundation nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
#
#
# This script helps you to fetch distfiles for packages.
#
# See also:
#
# - distfetch(1)
# - __PREFIX__/share/examples/distfetch/distfetch.conf
#
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"

# Default settings
CONFIG="__PREFIX__/etc/distfetch.conf"
PKGSRCDIR="${PKGSRCDIR:=/usr/pkgsrc}"
DISTDIR="`cd ${PKGSRCDIR} && make -V DISTDIR`"
TEMPFILE="/tmp/`basename $0`.$$" 
VERBOSE=

usage() {
	cat << EOF

Usage: `basename $0` [options]

Options:

  -c filename   Location of the configuration file
  -d directory  Location of the distribution files
  -s directory  Location of pkgsrc

  -h            This help text
  -v            Be more verbose

Default settings:

  -c ${CONFIG}
  -d ${DISTDIR}
  -s ${PKGSRCDIR}

Example:

  # `basename $0` -c ~/.distfetchrc

EOF
	exit 1
}

#
# main()
#

# Read command line arguments
ARGV=`getopt c:d:hs:v $*`
[ $? != 0 ] && usage
set -- ${ARGV}
for i; do
	case "${i}" in
	-c)
		CONFIG="${2}"
		shift 2
		;;
	-d)
		DISTDIR="${2}"
		shift 2
		;;
	-h)
		usage
		;;
	-s)
		PKGSRCDIR="${2}"
		shift 2
		;;
	-v)
		VERBOSE=YES
		shift
		;;
	--)
		shift
		break
		;;
	esac
done

# Check if DISTDIR could be set
DIRECTORY="`cd ${PKGSRCDIR} && make -V DISTDIR DISTDIR=${DISTDIR}`"
if [ "x${DIRECTORY}" != "x${DISTDIR}" ]; then
	echo "Error: you already have DISTDIR=${DIRECTORY} in /etc/mk.conf"
	exit 1
fi
DISTDIR="${DIRECTORY}"
export DISTDIR

# Read the configuration file
if [ ! -r "${CONFIG}" ]; then
	echo "`basename $0`: ${CONFIG} not found"
	exit 1
fi
echo "Reading package list from ${CONFIG}"
PKGS=
for i in `sed -e 's+#.*++' -e '/^$/d' < ${CONFIG} | tr '\n' ' '`
do
	if [ ! -d "${PKGSRCDIR}/${i}" ]; then
		echo "Warning: unknown package ${i}"
		continue
	fi
	echo "===> ${i}"
	PKGS="${PKGS} ${i}"
done

# Generate a list of missing files
echo "Processing packages"
(cd ${PKGSRCDIR} && make SITE_SPECIFIC_PKGS="${PKGS}" SPECIFIC_PKGS=1 \
	fetch-list) > "${TEMPFILE}"

# Fetch distfiles into ${DISTDIR}
echo "Fetching missing distfiles into ${DISTDIR}"
if [ -z "${VERBOSE}" ]; then
	sh "${TEMPFILE}" || exit 1
else
	echo "Running \"sh -x ${TEMPFILE}\""
	sh -x "${TEMPFILE}" || exit 1
fi

rm -f "${TEMPFILE}"
echo "All done"
