#!/bin/sh

set -e

GMAKE=gmake
PREFIX=/usr/local/soft

BIN="$PREFIX/bin"
SHARE="$PREFIX"/share
FONTS="$SHARE"/fonts
LICENSES="$PREFIX"/licenses
MAN="$PREFIX"/man
SRC="$PREFIX/src/$NAME"
mkdir -p "$BIN" "$SHARE" "$FONTS" "$LICENSES" "$MAN" "$SRC"

soft_advise(){
	printf "\
%s: %s: It is advised to run the following, or the system equivalent:
	%s\n" soft "$NAME" "$*" >&2
}

case "$UPSTREAM_TYPE" in
custom)	;;
*)	get_upstream(){
		case "$UPSTREAM_TYPE" in
		custom)	;;
		http*)	mkdir -p "$SRC"
			curl -LO "$UPSTREAM" --output-dir "$SRC" ;;
		git)	git clone "$UPSTREAM" "$SRC" || git -C "$SRC" pull;;
		*)	case "$UPSTREAM" in
			git:*)	UPSTREAM_TYPE=git; get_upstream ;;
			*.git)	UPSTREAM_TYPE=git; get_upstream ;;
			*)	! printf '%s: %s: %s: Unknown upstream type\n'\
					soft "$NAME" "$UPSTREAM_TYPE" >&2 ;;
		esac
	esac
	} ;;
esac; get_upstream

if test "$LICENSE" = custom || ! test -e "$LICENSES/$LICENSE"
then	case "$LICENSE" in
	agpl30)	# GNU Affero General Public License 3.0
		curl 'https://www.gnu.org/licenses/agpl-3.0.txt' ;;
	fdl11)	# GNU Free Documentation License 1.1
		curl 'https://www.gnu.org/licenses/old-licenses/fdl-1.1.txt' ;;
	fdl12)	# GNU Free Documentation License 1.2
		curl 'https://www.gnu.org/licenses/old-licenses/fdl-1.2.txt' ;;
	fdl13)	# GNU Free Documentation License 1.3
		curl 'https://www.gnu.org/licenses/fdl-1.3.txt' ;;
	gpl10)	# GNU General Public License 1.0
		curl 'https://www.gnu.org/licenses/old-licenses/gpl-1.0.txt' ;;
	gpl20)	# GNU General Public License 2.0
		curl 'https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt' ;;
	gpl30)	# GNU General Public License 3.0
		curl 'https://www.gnu.org/licenses/gpl-3.0.txt' ;;
	lgpl20)	# GNU Library General Public License 2.0
		curl 'https://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt'
		;;
	lgpl21)	# GNU Lesser General Public License 2.1
		curl 'https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt'
		;;
	lgpl30)	# GNU Lesser General Public License 3.0
		curl 'https://www.gnu.org/licenses/lgpl-3.0.txt' ;;
	*)	stream_license ;;
	esac >"$LICENSES/$LICENSE"
fi
