#!/bin/bash
# 2010 Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain

which xdg-open 2>/dev/null >/dev/null && BROWSER=xdg-open

# TODO: use epm assure
# (command from perl-libwww)
which GET &>/dev/null && GET=GET || GET=false

which rsync &>/dev/null && RSYNC=rsync || GET=false

# will DOWNLOADEDNAME with real name
download_url()
{
	local URL="$1"

	# if has_space
	if [ "${URL/ //}" != "$URL" ] ; then
		URL="$($EPMCMD tool eget --list --latest $URL)"
		[ -n "$URL" ] || fatal "Can't find $1 tarball."
	fi

	local NAME="$(basename "$URL")"

	# TODO
	# do not download if already exists...
	#[ -r "$NAME" ] && { echo "skipping $1, already exists..."; return; }
	rm -f "$NAME"

	# NOTE: disable cert checking
	# use wget with try=1 and timeout = 30 sec
	docmd $EPMCMD tool eget -O "$NAME" "$URL" && test -r "$NAME" && DOWNLOADEDNAME="$NAME" && DOWNLOADEDURL="$URL" && return

	rm -f "$NAME"
	return 1
}

check_url()
{
	$GET -d "$1"
}

check_rsync()
{
	[ -n "$1" ] || return 1
	$RSYNC -n "$1" >/dev/null 2>&1
}
