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

opt="all"

if [ $# -lt 1 ]; then
    opt="all"
else

    # Process cmdline flags.
    while [ -n "$1" ]; do
      case "$1" in
      "-all")                          # clean all package .s
	  opt="all"
	  ;;
      "-src")                          # update only . code
	  opt="src"
	  ;;
      "-list")                         # list files needing updating
	  opt="list"

	  echo ""
	  
	  if util/check_update; then
	      echo "IRAF is up to date"
	  else
	      echo "An IRAF update is available"
	  fi
	  echo
	  (cd util && "${iraf}/util/pkgupdate" -list)
	  exit 0
	  ;;
      "-core")                         # update only core system
	  opt="core"
	  ;;
      "-noao")                         # update only NOAO package
	  opt="noao"
	  ;;
      "-vo")                           # update only VO package
	  opt="vo"
	  ;;
      *)
          echo "Error: Unknown option '$1', quitting."
          exit 1
      esac

      shift
   done
fi


# Initialize the $iraf and environment.
if [ -z "$iraf" ]; then
  if [ -e "$HOME/.iraf/setup.sh" ]; then
    . "$HOME/.iraf/setup.sh"
  else
    . unix/hlib/setup.sh
  fi
else
    . "$iraf/unix/hlib/setup.sh"
fi


# Called from Makefile, iraf=true root.
. "$iraf/unix/hlib/irafuser.sh"

#  Init the build scripts as the first update.
#echo "Updating build scripts ...."
#${iraf}/util/self_update


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

if [ "$status" = 2 ]; then
   echo "Update Status:    No updates necessary"
elif [ "$status" = 0 ]; then
   echo "Update Status:    Successful"
else
   echo "Update Status:    Fails"
fi

exit 0
