:downloading utility
:module
:misc.m help.m


eval addhelp download&download $(c4)[$(c3)url/ftp$(c4)]&download the specified file.
alias download {
	if ([$0]) {
		if (match(*http:* $0)) {
			@site = before(/ $after(2 / $0))
			@file = [/$after(3 / $0)]
		}
		{
			@ site = before(/ $0)
			@ file = [/$after(/ $0)]
		}
		if (fexist($twiddle(~/$(after($numwords(/ $file) / $file))))) {
			@ rename($twiddle(~/$(after($numwords(/ $file) / $file))) $twiddle(~/$(after($numwords(/ $file) / $file)))~)
		}
                ^exec -9 %dl
                ^exec -name dl wget --directory-prefix=~/ $(site)$(file) 1>/dev/null 2>/dev/null
		@ stime = time()
		xecho -b connecting to $(c4)[$(c3)$(site)$(c4)]
		xecho -b requesting $(file) from $(c4)[$(c3)$(site)$(c4)]
	}
	^on ^exec_errors "dl *" { 
        	xecho -b [$0 ERR] $1- 
        }
	^on ^exec_exit "dl *" { 
	        if ( [$2] ) { 
			xecho -b error: unable to download $(file) from $(c4)[$(c3)$(site)$(c4)]
        	}
		{
			@ ftime = time()
			@ ttime = [${ftime - stime}]
			@ tsize = fsize($twiddle(~/$(after($numwords(/ $file) / $file))))
	                ^stack push set floating_point_math_var
	                ^set floating_point_math_var on
	                @ kbps  = [$fix.size($trunc(1 ${tsize / ttime}))/s]
	                ^stack pop set floating_point_math_var
			xecho -b successfully downloaded $file $(c4)[$(c3)$fix.size($tsize)$(c4)]$(c3) from $(c4)[$(c3)$(site)$(c4)]$(c3) in $ttime seconds $(c4)[$(c3)$(kbps)$(c4)]
			if (file == [/daedo/core.mtml])	{
				xecho -b checking core version.
				checkver
			}
            	} 
        }
	{
		chelp download
	}
	^restub download download.m
}
