#! /bin/ksh -x
# $OpenBSD: fetch_cmd.template,v 1.2 2021/04/30 20:20:02 naddy Exp $
# Copyright (c) 2021 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


# this is a template for parsing the common ftp options a FETCH_CMD
# requires, so that you can use curl or whatever weird shit you need
# later

DISTMIRROR="http://192.168.1.20/pub/OpenBSD/distfiles"

continued=false
if test -t
then
	verbose=true
	progressmeter=true
else
	verbose=false
	progressmeter=false
fi
sessionfile=
outputfile=
pipeout=false

set -e
while getopts CmVvo:S: name
do
	case $name in
	C)	continued=true
		;;
	m)	progressmeter=true
		;;
	V)	verbose=false
		;;
	v)	verbose=true
		;;
	o)	outputfile=$OPTARG
		case $outputfile in
		-)	pipeout=true ;;
		esac
		;;
	S)	ssl=$OPTARG
		case $ssl in
		session=*)
			sessionfile=${sessionfile#session=} ;;
		esac
		;;
	?)	echo "Usage: $0 [ftp options] [files ...]" >&2
		exit 2
		;;
	esac
done
shift $((OPTIND - 1))

# for instance, a skeleton using curl
args="-L -s"
[[ -n $outputfile ]] && args="$args -L -f -o $outputfile"
$continued && args="$args -C -"
/usr/local/bin/curl $args "$DISTMIRROR/$(basename "${outputfile%%.part}")" \
    || /usr/local/bin/curl $args "$@"
