#!/bin/sh
#

set -e

# 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

. $iraf/unix/hlib/irafuser.sh

# Reconfigure the system for the proper architecture if needed.
if [ -n "$IRAFARCH" ]; then		# use environment value
    make "$IRAFARCH"
else					# use actual value
    arch=$("$iraf/unix/hlib/irafarch.sh" -actual)
    cfgarch=$(mkpkg arch | sed -e "s/system is configured for //g")
    if [ "$cfgarch" != "$arch" ]; then
	#make $arch
	echo "Warning: current arch=$cfgarch but system arch=$arch"
	export IRAFARCH=$arch
    fi
fi

cd "$iraf/"				# build core system
mkpkg
mkpkg update

cd "$iraf/noao"				# build NOAO package
export noao=$(pwd)/
mkpkg -p noao

cd "$iraf/vo"				# build VO package
export vo=$(pwd)/
cd votools				# build VOTOOLS package
mkpkg -p vo update
