#!/bin/csh -f
#
#  IRAF_UPDATE - Update the core IRAF system with a cumulative patch.

unalias 	grep ls

set opt		= "all"
set all		= yes
set src_only	= no
set list_only	= no
set core_only	= no
set noao_only	= no
set vo_only	= no

if ($#argv < 1) then
    set opt	= "all"
    set all	= yes
else

    # Process cmdline flags.
    while ("$1" != "")
      switch ("$1")
      case -all:                          # clean all package sources
        set all = yes
	set opt	 = "all"
        breaksw
      case -src:                          # update only source code
        set src_only = yes
	set opt	 = "src"
        breaksw
      case -list:                         # list files needing updating
        set list_only = yes
	set opt	 = "list"

	echo ""
	util/check_update
	if ($status == 0) then
	    echo "IRAF is up to date"
	else
	    echo "An IRAF update is available"
	endif
	echo ""
	(chdir util ; ${iraf}/util/pkgupdate -list)
	exit 0

      case -core:                         # update only core system
        set core_only = yes
	set opt	 = "core"
        breaksw
      case -noao:                         # update only NOAO package
        set noao_only = yes
	set opt	 = "noao"
        breaksw
      case -vo:                           # update only VO package
        set vo_only = yes
	set opt	 = "vo"
        breaksw
      default:
        echo "Error: Unknown option '"$1"', quitting."
        exit 1
      endsw

      shift
   end
endif


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


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


#  Init the build scripts as the first update.
#echo "Updating build scripts ...."
util/self_update


#  Execute the update.
echo "Updating IRAF system  ...."
util/iraf_latest $opt

if ($status == 2) then
   echo -n "Update Status:    No updates necessary"
else if ($status == 0) then
   echo -n "Update Status:    Successful	"
else
   echo -n "Update Status:    Fails	"
endif
echo ""

exit 0
