#!/bin/sh
#
#  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.


clean=1
irafdir=$(pwd)/../util


# Process cmdline flags.
while [ -n "$1" ]; do
   case "$1" in
   "-noclean")                      # clean all package sources
      clean=0
      ;;
   *)
      break
      ;;
   esac

   shift
done



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.16
        
all:: update

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

# Install all available packages for this platform.
install_all::
	@../util/pkgall

# 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 -noclean

# Update recent changes from the repository.
config_update::
	@../util/pkgupdate -config

MAKE_TEMP_END

echo "Setup Complete."



# For each package we have, append a makefile entry.
files=$(cat .repo_pkgs)
for p in ${files}; do

   # Create template makefile entries for each package
   cat >> Makefile <<EOF

${p}::
	@../util/pkginst $p
clean_${p}::
	@../util/pkgclean $p
update_${p}::
	@../util/pkgupdate $p
EOF

   # Create the directory
   if [ -e "$p" ]; then
	if [ "$clean" = 1 ]; then
	    /bin/rm -rf "$p"
	fi
   else
       mkdir "$p"
   fi
done

if [ "$clean" = 0 ]; then
    exit 0
fi


cat <<EOF

    To install packages, use 'ls' to list the currently available
    packages from the IRAF repository.  For each package you wish
    to install, use the command:

      make <pkg>

    The package will be loaded dynamically the next time you start
    the CL session.

    Use the commmands:

      make update	# to update pkgs to the latest repository version
      make check	# to list available updates
      make clean	# to delete installed all packages
      make init    	# restore to pre-configure state
      make <pkg>	# to force a re-install of named <pkg>

EOF

exit 0
