#!/bin/csh -f
#
#  PKGGET -- Download the specified URL to the current directory.  We use 
#  a command specific to the system we're on.  We assume the URL has been 
#  properly escaped in the argument list.
#
#  Usage:     pkgget [-h] [-n] [-v] url
#
#  Where	-n	no-op flag
#		-v 	verbose output
#		-h 	this message
#
#  Example:
#	% pkgget -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)"

# Utility aliases.
alias PUT      "mv -f \!*; chown $user \!$ "                   # [MACHDEP]
alias BOLD_ON  "(tput bold)"
alias BOLD_OFF "(tput sgr0)"
alias SO_ON    "(tput smso)"
alias SO_OFF   "(tput sgr0)"

alias DO_OK    "(echo -n '[ '; BOLD_ON; echo -n ' OK '; BOLD_OFF; echo ' ]')"
alias DO_WARN  "(echo -n '[ '; BOLD_ON; echo -n 'WARN'; BOLD_OFF; echo ' ]')"
alias DO_FAIL  "(echo -n '[ ';   SO_ON; echo -n 'FAIL';   SO_OFF; echo ' ]')"

alias ERRMSG   "(echo -n '   ';BOLD_ON;echo -n 'ERROR: '  ;BOLD_OFF; echo \!*)"
alias WARNING  "(echo -n '   ';BOLD_ON;echo -n 'WARNING: ';BOLD_OFF; echo \!*)"
alias NEWLINE  "(echo '')"



# set echo



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

# MACHDEP definitions which may be reset below.
set VERSION		= `cat ../.version`

# Utility aliases.


#----------------------------------
# Determine platform architecture.
#----------------------------------

set UNAME=""
if (-e /usr/bin/uname) then
    set uname_cmd = /usr/bin/uname
    set UNAME=`/usr/bin/uname | tr '[A-Z]' '[a-z]'`
else if (-e /bin/uname) then
    set uname_cmd = /bin/uname
    set UNAME=`/bin/uname | tr '[A-Z]' '[a-z]'`
else
    WARNING  "No 'uname' command found to determine architecture."
    exit 1
endif

switch ($UNAME) 
    case linux:
	set dlcmd		= "/usr/bin/wget"
        breaksw
    case darwin: 					    # Mac OSX/iOS
    case macosx:
    case macintel:
    case ipad:
        #set dlcmd 		= "/usr/bin/ftp -A"
        set dlcmd 		= "/usr/bin/ftp"
        breaksw

    # Other architectures to be added here

    default:      
	ERRMSG  "Unable to determine platform architecture."
	exit 1
endsw

# If we don't have a download command installed, use our own .....
if (! -e $dlcmd) then
    set dlcmd = `dirname $0`/fget
endif

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

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

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


# Process cmdline flags.
while ("$1" != "")
    switch ("$1")
    case -n:                            # no execute
        set exec  = no
        breaksw
    case -v:                            # be chatty
        set verb  = yes
        set quiet = no
        breaksw
    case -h:                            # print help summary
        goto Usage
    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


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

   if ("$verb" == "no") then
      $dlcmd $url 		>& /dev/null
   else
      $dlcmd $url
   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 '"$url:t"'"
   endif
   exit 1

else
   if ($#argv > 1) then
      mv $url:t $2
   endif
endif

#  Normal exit.
exit 0



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

Usage:
    echo "Usage: pkgget [-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
