#!/bin/csh -f
#
#  FGET -- Download a URL.
#
#  Usage:     fget [-h] [-n] [-q | -v] url
#
#  Where	-n	no-op flag
#		-q 	suppress output
#		-v 	verbose output
#		-d 	set download directory
#		-o 	set output filename
#		-h 	this message
#
#  Example:
#	% fget -q ftp://iraf.noao.edu/iraf/extern/foo-linux.tar.gz
#
# ----------------------------------------------------------------------------


unset	noclobber
onintr	cleanup_
unalias cd cp cmp echo ln mv rm sed set grep ls chmod chown pwd touch sort which
unalias ftp wget

setenv	path  "(../util /sbin /usr/sbin /bin /usr/bin /usr/5bin /usr/ucb /etc /usr/etc $path /usr/local/bin /opt/local/bin /local/bin /home/local/bin)"

# set echo


##############################################################################
# START OF MACHDEP DEFINITIONS.
##############################################################################

# MACHDEP definitions which may be reset below.


# Find the iraf root directory.
if (! $?iraf) then
  set iraf = ""
  foreach f ( ~/.iraf.h ~/.iraf/iraf.h /usr/include/iraf.h)
     # $iraf is defined, use a well-known path for the system
     if (-e ${f}) then
       set i = `egrep IRAF ${f} | egrep \#define | sed -e 's/"//g'`
       set iraf = $i[3]
echo $i
       break
     endif
  end
endif
if ("$iraf" == "") then
    if (-e /iraf/iraf) then
	set iraf = /iraf/iraf/
    else
	set iraf = `dirname $0`/../
    endif
endif

# Determine platform architecture.
set arch = `$iraf/unix/hlib/irafarch.csh`


##############################################################################
# END OF MACHDEP DEFINITIONS.
##############################################################################

#=============================================================================
# Declarations and initializations.
#=============================================================================

set exec	= yes
set verb	= no
set url		= ""
set fname	= ""
set ddir	= ""


# Process cmdline flags.
while ("$1" != "")
    switch ("$1")
    case -n:                            # no execute
        set exec  = no
        breaksw
    case -q:                            # be quiet
        set verb  = no
        set quiet = yes
        breaksw
    case -v:                            # be chatty
        set verb  = yes
        set quiet = no
        breaksw
    case -h:                            # print help summary
        goto Usage
    case -d:                            # set download directory
        set ddir = $2
	shift
        breaksw
    case -o:                            # set output file name
        set fname = $2
	shift
        breaksw
    default:
        set url = $1
        break
    endsw

    if ("$2" == "") then
        break
    else
        shift
    endif
end


#  Error checks.
if ("$url" == "") then
   if ("$verb" == "yes") then
      echo "ERROR: URL not specified"
   endif
   exit 1
endif

# Get the download filename.  Delete an existing copy of the file
if ($fname == "") then
    set fname = $url:t
endif
if (-e "$fname") then
    /bin/rm -f $fname
endif

# Ensure URL is an HTTP protocol.
set prot =  `echo $url | cut -c1-3`
if ("$prot" == "ftp") then
  set u = `echo $url | sed -e 's;ftp://iraf.noao.edu/iraf;http://iraf.noao.edu/ftp;'`
  set url = $u
endif

#  Do it.
if ("$exec" == "yes") then
   if ("$verb" == "yes") then
      echo "Downloading "$url" ...."
   endif

   set args = "url='$url' fname='${ddir}${fname}' cache='/tmp' verbose=no extn='' use_cache=no"
   if ("$verb" == "no") then
      $iraf/bin.$arch/x_system.e urlget ${args} \$nargs=2	>& /dev/null
   else
      $iraf/bin.$arch/x_system.e urlget ${args} \$nargs=2
   endif

   if ("$verb" == "yes") then
      echo "done"
   endif
endif


#  Verify we have the file.
if (! -e $url:t) then
   if ("$verb" == "yes") then
      echo "Error downloading file '"$fname"'"
   endif
   exit 1
else
   if ($#argv > 1) then
      mv $url:t $2
   endif
endif

#  Normal exit.
exit 0



#=============================================================================
# Usage
#=============================================================================

Usage:
    echo "Usage: fget [-h] [-n] [-q | -v] url"
    echo ""
    echo "    where -n          # no execute"
    echo "          -q          # suppress output"
    echo "          -v          # verbose output"
    echo "          -h          # this message"

exit 0
