#!/bin/csh -f
#
#  CONFIGURE -- Bootstrap the dynamic external package system by downloading
#  the repository manifest and creating a workable Makefile to be used for
#  install packages and updates.  This script only needs to be run once
#  after the system is installed, thereafter the 'make' commands are used.
#  See the README file for details.


set  irafdir    = $cwd/../util

echo "Initializing repository data ...."
$irafdir/pkginit				# init repository information


# Create the template Makefile.
echo "Creating system makefile ...."
cat << MAKE_TEMP_END		> Makefile
#
#  Makefile for IRAF external package installation/maintenance.
#
# ---------------------------------------------------------------------------

# Compiler Flags.

RELEASE		= v2.15
        
all:: update

# Update recent changes from the repository.
update::
	@./configure
	@../util/pkgupdate -all

# List packages available on the repository.
list::
	@cat .repo_pkgs

# Clean the IRAF tree of binaries for the currently configured arch.
init::
	@../util/pkgclean -init

# Remove all package code but leave the structure in place.
clean::
	@../util/pkgclean -all

# Restore the dynamic package directory to its distribution state.
distclean::
	@../util/pkgclean -init

# Check to see which installed packages could be updated.
check::
	@../util/pkgupdate -list

# Update recent changes from the repository.
self_update::
	@../util/pkgupdate -self
	@./configure


MAKE_TEMP_END

echo "Setup Complete."



# For each package we have, append a makefile entry.
foreach p (`cat .repo_pkgs`)

   # Create template makefile entries for each package
   echo "${p}::"						>> Makefile
   echo "	@../util/pkginst $p"			>> Makefile
   echo "clean_${p}::"					>> Makefile
   echo "	@../util/pkgclean $p"			>> Makefile
   echo "update_${p}::"					>> Makefile
   echo "	@../util/pkgupdate $p"			>> Makefile
   echo ""						>> Makefile

   # Create the directory
   if (-e $p ) then
      /bin/rm -rf $p
   endif
   mkdir $p
end

echo ""
echo ""
echo "    To install packages, use 'ls' to list the currently available"
echo "    packages from the IRAF repository.  For each package you wish"
echo "    to install, use the command:"
echo ""
echo "      make <pkg>"
echo ""
echo "    The package will be loaded dynamically the next time you start"
echo "    the CL session."
echo ""
echo "    Use the commmands:"
echo ""
echo "      make update	# to update pkgs to the latest repository version"
echo "      make check	# to list available updates"
echo "      make clean	# to delete installed all packages"
echo "      make init    	# restore to pre-configure state"
echo "      make <pkg>	# to force a re-install of named <pkg>"
echo ""
echo ""



exit 0
