#!/bin/csh -f
#
#  IRAF_LATEST - Update the system with the latest distribution files.

unalias 	grep ls

set opt		= "all"


if ($#argv < 1) then
    echo "Usage:  iraf_latest <opt>"
    exit 0
else
    set opt = $1
endif


# Called from Makefile, set iraf root.
set  iraf       = $cwd/			
source $iraf/unix/hlib/irafuser.csh


set REPO	= `${iraf}/util/pkgrepo`
if ($?IRAFARCH) then
    set arch    = $IRAFARCH
else
    set arch    = `${iraf}/unix/hlib/irafarch.csh -actual`
endif


echo "iraf_latest: cwd = " $cwd

# Figure out which binaries are required.
set files = "patch-src.tar.gz"			# always need the source ....
set bins = ""
foreach b (linux linux64 macosx macintel)
    if (-e "bin.$b/x_images.e") then
	set bins = `echo $bins " " $b`
	switch ($b)
	case linux:
	    set files = `echo "$files patch.lnux.x86.tar.gz"`
	    breaksw
	case linux64:
	    set files = `echo "$files patch.lnux.x86_64.tar.gz"`
	    breaksw
	case macosx:
	    set files = `echo "$files patch.macx.uni.tar.gz"`
	    breaksw
	case macintel:
	    set files = `echo "$files patch.macx.x86_64.tar.gz"`
	    breaksw
	endsw
    endif
end
echo "Updating binaries:  " $bins


# Download the needed files to /tmp

set FGET        = "${iraf}/util/fget"
set REPO	= `util/pkgrepo`
foreach f ($files)
    echo -n "Downloading: $f"
    ${FGET} -q -d /tmp/ $REPO/$f
    if (! (-e /tmp/$f)) then
	echo ""
	echo "Error downloading $REPO/$f, quitting"
	exit
    endif

    echo -n " Unpacking ..."
    tar zxf /tmp/$f
    /bin/rm -f /tmp/$f

    echo    " Done."
end


#  For the initial release, the update procedures haven't yet been defined.
#  This script will be replaced by the working version at the first release.


if ($opt == "all") then				# Update everything
  if (-e extern/.installed) then
    echo "Update all external packages ..."
    (chdir extern ; make update)
  endif
endif

echo ""
exit 0
