# PCS makefile
#
# Author:
#   AJC: A J Chipperfield (STARLINK)
#
# Description:
#   This description file is used by the make utility to build the
#   PCS package from the distributed source files, to install
#   the resulting system for use, and to perform other housekeeping
#   tasks.
#   PCS is not quite like other Starlink Software Items in that its
#   sub-packages, dtask, subpar etc. may be built and installed etc.
#   separately. For this reason we attempt to 'make' the specified target
#   (except check) for each of the sub-packages before using the normal
#   criteria for deciding whether or not to 'make' the target for the 
#   top-level. At each stage if the criteria are not met the makefiles will
#   cause the normal messages to be displayed. Each sub-package and the 
#   top-level package has its own datestamp file installed. The fact that 
#   the top-level datestamp file is installed does not guarantee that all 
#   the sub-packages are also installed.
#   There are special targets, install_pcs and deinstall_pcs which handle
#   only the top-level package. These may be useful if only documentation
#   is to be updated.
#
# Pre-requisites:
#   It is assumed that all the sub-directories are created and contain
#   a tar file of source, and a makefile.
#   lex
#   string
#   sock
#   atimer
#   msp
#   messys
#   adam
#   misc
#   subpar
#   parsecon
#   hdspar
#   dtask
#   ams
#   task
#   adamnet
#
#   See mk file for environment variables used.
#
#  Invocation:
#     This makefile is not intended to be used by make when invoked
#     directly (although this is possible), but instead to be used via
#     the accompanying mk script.  This script sets up a number of
#     environment variables which are used as macros within the
#     makefile and which accommodate differences between machines and
#     operating systems (it invokes make with the -e option).  Please
#     consult the mk script prologue for full details.
#
# Dependencies:
#     The PCS package depends on the following other Starlink packages
#     which must previously have been installed into the appropriate
#     sub-directories of the $STARLINK directory (/star is used if the
#     environment variable STARLINK is not defined).
#        sae_par*
#           Global include files
#        chr*
#           Character handling routines
#        ems*
#           Error message service
#        hds*
#           Hierarchical data system
#        psx
#           Posix routines
#        hlp
#           Portable help library
#
# History:
#   07-JAN-1992 (AJC):
#     Original version
#   21-JUL-1992 (AJC):
#     Move MISC before PARSECON
#   24-MAY-1993 (AJC):
#     Overhaul and include icl etc
#    1-SEP-1993 (AJC):
#     Revise for new-style makefiles.
#   17-DEC-1993 (AJC):
#     Use subsidiary mk not makefiles.
#   11-FEB-1994 (AJC):
#     Version 2.0-1
#     Add DTASK_MAIN.TXT to BUILT_FILES
#     Remove icl
#     Remove PAR
#   11-JUL-1994 (AJC):
#     Version 3.0
#     Add sock, atimer and ams
#     Revised messys and msp
#     Remove olddtask, oldtask and oldsubpar
#    5-DEC-1994 (AJC):
#     Add facility error files to built system
#   16-FEB-1995 (AJC):
#     Use new-style .INSTALLED_$SYSTEM (pointing to $INSTALL).
#     Use new-style .BUILT file (containing $SYSTEM).
#     Add export_run target.
#     Comment out ant (except source) as it doesn't work.
#   24-APR-1994 (AJC):
#     Add tests of system built for etc.
#     Add else clause to catch failure of $(MAKE)s
#   29-SEP-1995 (AJC):
#     Renamed ADAMSYS to PCS Version 3.1-1
#   30-JAN-1996 (AJC):
#     Version 3.2
#   11-JUN-1996 (AJC):
#     Rename ant to adamnet
#     Add adamnet.news to source files.
#   14-OCT-1996 (AJC):
#     Version 3.2-1
#     Add MAKE_FILES to export_run
#   24-FEB-1997 (AJC):
#     Version 3.2-2
#   20-MAY-1997 (AJC):
#     Version 3.2-3
#   30-OCT-1997 (AJC):
#     Version 3.3
#     Print PACK_NAME not PKG_NAME in check:
#     Correct check for source files in check:
#    5-FEB-1998 (AJC):
#     Remove 'cd ..' from invocation of subsidiary makes so errors stop system
#     Add -f to rm of datestamp and .BUILT
#   20-MAY-1998 (AJC):
#     Version 3.4
#   23-JUN-1998 (AJC):
#     Add SUN/224 (HDSPAR)
#   20-JUL-1998 (AJC):
#     Install top level first so deinstall will work after a failure.
#     Improve some messages.
#    1-DEC-1998 (AJC):
#     Add -f to rm in test:
#   15-APR-1999 (AJC):
#     Revise to separate build and install phases
#     'make' targets of sub-packages regardless of top-level state.
#     Version 3.5
#   28-MAY-1999 (AJC):
#     Version 3.5-1 correct access to other PCS components for PARSECON,
#      MSP and AMS.
#    1-JUL-1999 (AJC):
#     Version 3.5-2 correct bug in subpar
#   30-SEP-1999 (AJC):
#     Version 3.5-3
#     Add dtask.act_err.h to list of BUILT_FILES
#    9-FEB-2000 (AJC):
#     Version 3.6
#     Add PCS_CONDITIONS
#    9-MAR-2000 (AJC):
#     Install LaTeX docs by copy and link
#   14-AUG-2000 (AJC):
#     Version 3.6-1
#   11-JAN-2001 (AJC):
#     Version 3.6-2
#   22-MAR-2001 (AJC):
#     Correct list of INSTALL directories
#   16-MAY-2001 (AJC):
#     Version 3.6-3 (SUBPAR mods)
#   16-AUG-2001 (AJC)
#     Version 4.0  (removed ADAM and MESSYS libraries)
#   26-FEB-2002 (AJC):
#     Version 4.0-1 (minor bug fixes in SUBPAR)
#     Moved documents SUN/115 and SUN134 into PCS from floating
#    6-JAN-2003 (AJC):
#     Version 4.1
#   16-APR-2003 (AJC):
#     Version 4.1-1
#   16-JUL-2003 (AJC):
#     Added subpar_err.h to BUILT_FILES
#    1-DEC-2003 (AJC):
#     Version 4.1-2
#     Remove PCS test dependency on PAR
#     Make install depend on INSTALL_DIRS
#    3-DEC-2003 (AJC):
#     Version 4.1-3
#     Added SSN/11
#
#
PKG_NAME = pcs
PACK_NAME = PCS
PKG_VERS = 4.1-2
EXPORT = .

#-------------------------------------------------------------------------------

#  Help target.
#  ===========
#
#  This is the default target, so appears first.

#  Display information about the mk script and the make targets.

help:
	@ echo \
    '   The makefile provided is intended to be used by the make utility when';\
        echo \
    '   invoked via the associated mk script. This script defines environment';\
        echo \
    '   variables which are used by make to accommodate differing machine and';\
        echo \
    '   operating system characteristics. Please see the mk script prologue';\
        echo \
    '   for full details.';\
        echo;\
        echo \
    '   The following targets are provided:';\
        echo;\
        echo \
    '      help          - Print out this message';\
        echo \
    '      check         - Check source file presence and show current state';\
        echo \
    '      build         - Build the package from source';\
        echo \
    '      install       - Install the built package for use';\
        echo \
    '      install_pcs   - Install the built top-level package only';\
        echo \
    '      deinstall     - Deinstall the package';\
        echo \
    '      deinstall_pcs - Deinstall the top-level package only ';\
        echo \
    '      test          - Perform a quick test of the installation';\
        echo \
    '      export        - Make a tar file for exporting the built package';\
        echo \
    '      export_source - Make a tar file for exporting the package source';\
        echo \
    '      clean         - Tidy up after building the package';\
        echo \
    '      unbuild       - Remove all the built files';\
        echo;\
        echo \
    '   To build and install the $(PACK_NAME) package on a supported system, do:';\
        echo;\
        echo \
    '      mk build; mk install; mk test; mk clean';\
        echo

#------------------------------------------------------------------------------
# Make file
MAKE_FILES = makefile \
             mk \
             adam/makefile \
             adam/mk \
             ams/makefile \
             ams/mk \
             adamnet/makefile \
             adamnet/mk \
             atimer/makefile \
             atimer/mk \
             dtask/makefile \
             dtask/mk \
             hdspar/makefile \
             hdspar/mk \
             lex/makefile \
             lex/mk \
             messys/makefile \
             messys/mk \
             misc/makefile \
             misc/mk \
             msp/makefile \
             msp/mk \
             parsecon/makefile \
             parsecon/mk \
             sock/makefile \
             sock/mk \
             string/makefile \
             string/mk \
             subpar/makefile \
             subpar/mk \
             task/makefile \
             task/mk

# Source files
SOURCE_FILES = $(DOCUMENTATION) \
               pcs_source.tar \
               $(MAKE_FILES) \
               adam/adam_source.tar \
               ams/ams_source.tar \
               adamnet/adamnet_source.tar \
               adamnet/adamnet.news \
               atimer/atimer_source.tar \
               dtask/dtask_source.tar \
               hdspar/hdspar_source.tar \
               lex/lex_source.tar \
               messys/messys_source.tar \
               misc/misc_source.tar \
               msp/msp_source.tar \
               parsecon/parsecon_source.tar \
               sock/sock_source.tar \
               string/string_source.tar \
               subpar/subpar_source.tar \
               task/task_source.tar
               
BUILT_FLAGS =               adam/.BUILT \
              ams/.BUILT \
              atimer/.BUILT \
              dtask/.BUILT \
              hdspar/.BUILT \
              lex/.BUILT \
              messys/.BUILT \
              misc/.BUILT \
              msp/.BUILT \
              parsecon/.BUILT \
              sock/.BUILT \
              string/.BUILT \
              subpar/.BUILT \
              task/.BUILT

BUILT_FILES = adam/adam_err \
              adam/adam_defns \
              adam/adam_defns.h \
              adam/adam_dev \
              adam/adam_datestamp \
              ams/ams_link_adam \
              ams/ams.h \
              ams/ams_sys.h \
              ams/ams_struc.h \
              ams/libams_adam.* \
              ams/ams_datestamp \
              atimer/atimer_link_adam \
              atimer/atimer_link \
              atimer/atimer.h \
              atimer/atimer_par.h \
              atimer/atimer_err.h \
              atimer/libatimer.* \
              atimer/fac_380_err \
              atimer/atimer_datestamp \
              dtask/dtask_link_adam \
              dtask/dtask_err \
              dtask/dtask_err.h \
              dtask/dtask_par \
              dtask/act_err \
              dtask/act_err.h \
              dtask/libdtask_adam.* \
              dtask/dtask_dev \
              dtask/dtask_main.o \
              dtask/dtask_main.txt \
              dtask/alink \
              dtask/ilink \
              dtask/fac_120_err \
              dtask/fac_320_err \
              dtask/dtask_datestamp \
              hdspar/hdspar_link_adam \
              hdspar/libhdspar_adam.* \
              hdspar/hdspar_dev \
              hdspar/hdspar_datestamp \
              lex/lex_link_adam \
              lex/lex_err \
              lex/lex_par \
              lex/liblex_adam.* \
              lex/lex_dev \
              lex/lex_datestamp \
              messys/messys_dev \
              messys/messys_par \
              messys/messys_err \
              messys/messys_len \
              messys/messys_struc \
              messys/messys_dd \
              messys/messys_par.h \
              messys/messys_err.h \
              messys/messys_len.h \
              messys/fac_110_err \
              messys/messys_datestamp \
              misc/misc_link_adam \
              misc/libmisc_adam.* \
              misc/misc_dev \
              misc/misc_datestamp \
              msp/msp_link \
              msp/msp_link_adam \
              msp/msp.h \
              msp/msp_par.h \
              msp/msp_err.h \
              msp/libmsp.* \
              msp/fac_1000_err \
              msp/msp_datestamp \
              parsecon/parsecon_link_adam \
              parsecon/parsecon_err \
              parsecon/parsecon_par \
              parsecon/compifl \
              parsecon/libparsecon_adam.* \
              parsecon/parsecon_dev \
              parsecon/parsecon_datestamp \
              sock/sock_link \
              sock/sock_link_adam \
              sock/sock.h \
              sock/sock_err.h \
              sock/libsock.* \
              sock/fac_390_err \
              sock/sock_datestamp \
              string/string_link_adam \
              string/libstring_adam.* \
              string/string_dev \
              string/string_datestamp \
              subpar/subpar_link_adam \
              subpar/subpar_err \
              subpar/subpar_err.h \
              subpar/subpar_par \
              subpar/libsubpar_adam.* \
              subpar/subpar_dev \
              subpar/fac_170_err \
              subpar/subpar_datestamp \
              task/task_link_adam \
              task/task_err \
              task/task_par \
              task/libtask_adam.* \
              task/task_dev \
              task/fac_364_err \
              task/task_datestamp \
              $(BUILT_FLAGS)
#              adamnet/adamnet \
#              adamnet/adamnet_datestamp \
#              adamnet/.BUILT \

#  Lists of Latex and hypertext documents.

LATEX_DOCS = ssn11.tex ssn29.tex ssn77.tex \
             sun115.tex sun134.tex sun144.tex sun224.tex sun241.tex
HYPERTEXT_DOCS = ssn11.htx ssn29.htx ssn77.htx \
             sun115.htx sun134.htx sun144.htx sun224.htx sun241.htx

#  List of documentation files.

DOCUMENTATION = $(LATEX_DOCS) $(HYPERTEXT_DOCS:.htx=.htx_tar) pcs.news \
                $(LICENCE)

#  Conditions of use file.

LICENCE = $(PACK_NAME)_CONDITIONS

################################################################################

#  Name of the date stamp file. This is used to record the time of the
#  most recent build for use in subsequent operations that require it.
#  There must be no rule for generating this file; it is only updated
#  as a side effect of building the package.

DATE_STAMP = $(PKG_NAME)_datestamp

#  Starlink directories
STARLINK = /star
STAR_INC = $(STARLINK)/include
STAR_BIN = $(STARLINK)/bin
STAR_HELP = $(STARLINK)/help

#  INSTALL directories
INSTALL = $(HOME)
INSTALL_BIN = $(INSTALL)/bin
INSTALL_DATES = $(INSTALL)/dates
INSTALL_INC = $(INSTALL)/include
INSTALL_LIB = $(INSTALL)/lib
INSTALL_SHARE = $(INSTALL)/share
INSTALL_ETC = $(INSTALL)/etc
INSTALL_DOCS = $(INSTALL)/docs
INSTALL_HELP = $(INSTALL)/help

#  List of directories actually used for installation (selected from
#  those above) and rules to create them.

INSTALL_DIRS = $(INSTALL_BIN) $(INSTALL_DATES) $(INSTALL_INC) $(INSTALL_LIB) \
               $(INSTALL_SHARE) $(INSTALL_ETC) $(INSTALL_DOCS) $(INSTALL_HELP)

$(INSTALL_DIRS):
	mkdir -p $@

#-------------------------------------------------------------------------------

#  Primary targets.
#  ===============
#
#  These are the targets intended for normal external use (apart from
#  help, which appears at the start of the file).

#  check: Check source file presence and show current state.
#  --------------------------------------------------------

#  The check target simply depends on all the source files being
#  present.

check:
	@ echo
	@ echo \
   '*** This is $(PACK_NAME) version V$(PKG_VERS) on system $(SYSTEM)'
	@ echo
	@ nosource='';\
          for f in $(SOURCE_FILES); do \
             if test ! -f $$f; then \
                nosource='1';\
                break;\
             else :; fi;\
          done;\
          if test -n "$$nosource"; then \
             echo '    Source files are NOT present';\
          else \
             echo '    All essential source files are present';\
          fi
	@ echo
#
#  Display the current state.
	@ echo '*** The following only applies to the top-level PCS - '
	@ echo '*** use target checkall to check on all sub-packages.'
	@ echo
	@ if test -f .BUILT;\
          then echo '    The package is currently:  built for system'\
             `cat .BUILT`;\
          else echo '    The package is currently:  not built';fi
	@ if test -f .INSTALLED_$(SYSTEM);\
          then echo '                               installed in'\
             `cat .INSTALLED_$(SYSTEM)`;\
          else echo '                               not installed';fi
	@ if test -f .TESTED_$(SYSTEM);\
          then echo '                               tested';\
          else echo '                               not tested';fi
	@ echo
	@ if test -f .BUILT;\
          then if test "$(SYSTEM)" != "`cat .BUILT`";\
             then echo '***  WARNING  ***';\
                echo \
'    The package is built for a system other than the current one';\
                echo ;\
             else :;fi;\
          else :;fi

#  checkall: Check each sub-package.
#  ---------------------------------
checkall:
	$(MAKE) check
	cd lex; ./mk check
	cd string; ./mk check
	cd sock; ./mk check
	cd atimer; ./mk check
	cd msp; ./mk check
	cd messys; ./mk check
	cd adam; ./mk check
	cd misc; ./mk check
	cd subpar; ./mk check
	cd parsecon; ./mk check
	cd hdspar; ./mk check
	cd dtask; ./mk check
	cd ams; ./mk check
	cd task; ./mk check
#	cd adamnet; ./mk check

#  build: Build the system.
#  -----------------------
#
#  Compile the source and build the required files in the source
#  directory.

#  N.B. Each subsidiary library in turn is built then installed.

#  The build target first checks that the package is not installed. If
#  not, it then causes the .BUILT target to be made which ensures that
#  the package has been built.

build: $(SOURCE_FILES)
	cd lex; ./mk build
	cd string; ./mk build
	cd sock; ./mk build
	cd atimer; ./mk build
	cd msp; ./mk build
	cd messys; ./mk build
	cd adam; ./mk build
	cd misc; ./mk build
	cd subpar; ./mk build
	cd parsecon; ./mk build
	cd hdspar; ./mk build
	cd dtask; ./mk build
	cd ams; ./mk build
	cd task; ./mk build
#	cd adamnet; ./mk build
	@ if test -f .INSTALLED_$(SYSTEM); then \
           echo;\
           echo \
   '*** The $(PACK_NAME) package is currently installed -- please use the';\
           echo '    "deinstall" target before re-building it';\
           echo;\
        else \
           if $(MAKE) .BUILT; then \
              echo;\
              echo '*** The $(PACK_NAME) package has been built';\
           else \
              echo;\
              echo '*** "make" failed building the $(PACK_NAME) package';\
              echo;\
              exit 1;\
           fi;\
        fi

#  The .BUILT target records the time of the most recent build which
#  modified any of the built files. It depends on all the built files
#  being up to date (which causes them to be built).

.BUILT: $(BUILT_FLAGS)
#  Enter information about the current machine and build environment
#  into the date stamp file.
	@ echo 'Package : $(PACK_NAME)'         >$(DATE_STAMP)
	@ echo 'Version : V$(PKG_VERS)'       >>$(DATE_STAMP)
	@ echo 'Library : V$(LIB_VERS)'       >>$(DATE_STAMP)
	@ echo ''                             >>$(DATE_STAMP)
	@ echo "Built by: $(USER) on node `uname -n`" \
                                              >>$(DATE_STAMP)
	@ echo "On      : `date`"             >>$(DATE_STAMP)
	@ echo ''                             >>$(DATE_STAMP)
	@ echo \
  "Machine : `uname -m` running `uname -s` `uname -v` (release `uname -r`)" \
                                              >>$(DATE_STAMP)
	@ echo ''                             >>$(DATE_STAMP)
	@ echo 'make macros:'                 >>$(DATE_STAMP)
	@ echo ''                             >>$(DATE_STAMP)
	@ echo '   SYSTEM  : $(SYSTEM)'       >>$(DATE_STAMP)
	@ echo ''                             >>$(DATE_STAMP)
	@ echo '   EXPORT  : $(EXPORT)'       >>$(DATE_STAMP)
	@ echo '   INSTALL : $(INSTALL)'      >>$(DATE_STAMP)
	@ echo '   STARLINK: $(STARLINK)'     >>$(DATE_STAMP)
	@ echo ''                             >>$(DATE_STAMP)
	@ echo '   AR_IN   : $(AR_IN)'        >>$(DATE_STAMP)
	@ echo '   BLD_SHR : $(BLD_SHR)'      >>$(DATE_STAMP)
	@ echo '   CC      : $(CC)'           >>$(DATE_STAMP)
	@ echo '   CFLAGS  : $(CFLAGS)'       >>$(DATE_STAMP)
	@ echo '   FC      : $(FC)'           >>$(DATE_STAMP)
	@ echo '   FFLAGS  : $(FFLAGS)'       >>$(DATE_STAMP)
	@ echo '   LINK    : $(LINK)'         >>$(DATE_STAMP)
	@ echo '   RANLIB  : $(RANLIB)'       >>$(DATE_STAMP)
	@ echo '   SHARE   : $(SHARE)'        >>$(DATE_STAMP)
	@ echo '   TAR_IN  : $(TAR_IN)'       >>$(DATE_STAMP)
	@ echo '   TAR_OUT : $(TAR_OUT)'      >>$(DATE_STAMP)
	@ echo ''                             >>$(DATE_STAMP)

#  Record completion of the build.
	@ echo '$(SYSTEM)' > .BUILT

#  install: Install the package for use.
#  ------------------------------------
#
#  Copy the built files to their installation directories, from where
#  they may be accessed.

#  The install target first checks if the package is already installed
#  If not, it causes the .INSTALLED_$(SYSTEM) target to be made which 
#  performs the installation.

install: $(INSTALL_DIRS)
	cd lex; ./mk install
	cd string; ./mk install
	cd sock; ./mk install
	cd atimer; ./mk install
	cd msp; ./mk install
	cd messys; ./mk install
	cd adam; ./mk install
	cd misc; ./mk install
	cd subpar; ./mk install
	cd parsecon; ./mk install
	cd hdspar; ./mk install
	cd dtask; ./mk install
	cd ams; ./mk install
	cd task; ./mk install
#	cd adamnet; ./mk install
	@ if test -f .INSTALLED_$(SYSTEM); then \
           echo;\
           echo \
   '*** The $(PACK_NAME) package has already been installed -- please use the';\
           echo \
   '    "deinstall" target first if you wish to reinstall it';\
           echo;\
        elif test -f .BUILT; then \
           if test "`cat .BUILT`" = "$(SYSTEM)"; then \
              if $(MAKE) .INSTALLED_$(SYSTEM); then \
                 echo;\
                 echo \
   '*** The $(PACK_NAME) package has been installed in directory $(INSTALL)';\
                 echo;\
              else \
                 echo;\
                 echo \
   '*** "make" failed installing the $(PACK_NAME) package in directory $(INSTALL)';\
                 echo;\
                 exit 1;\
              fi; \
           else \
              echo;\
              echo \
   "*** The $(PACK_NAME) package is built for system `cat .BUILT` -"\
   'so cannot be installed on system $(SYSTEM)';\
              exit 1;\
              echo;\
           fi; \
        else \
           echo;\
           echo \
   '*** The $(PACK_NAME) package is not built, so cannot be installed';\
           echo;\
           exit 1;\
        fi

#  The .INSTALLED_$(SYSTEM) target copies each file from the source
#  directory using "cp -p" to preserve its date, and replaces each
#  original file by a link to the installed copy.

.INSTALLED_$(SYSTEM): $(INSTALL_DIRS)
	$(MAKE) install_pcs

# Installation of the top level stuff is separated for clarity and so that
# documentaion can be updated without updating all the sub-packages.
install_pcs:
#  Install the Latex documentation, giving it world read permission,
#  leaving the source copy in place.
	for f in $(LATEX_DOCS) ""; do \
           if test -n "$$f"; then \
              cp -p $$f $(INSTALL_DOCS);\
              chmod 644 $(INSTALL_DOCS)/$$f;\
              rm -f $$f;\
              $(LINK) $(INSTALL_DOCS)/$$f $$f;\
           else :; fi;\
        done
#
#  Install any hypertext documents, giving world read access to all the files
#  they contain and linking with other documents.
	if test -n "$(HYPERTEXT_DOCS)"; then \
           pwd=`pwd`;\
           (cd $(INSTALL_DOCS);\
           for f in $(HYPERTEXT_DOCS) ""; do \
              if test -n "$$f"; then\
                 $(TAR_OUT) $$pwd/$${f}_tar;\
                 chmod 755 `find $$f -type d -print`;\
                 chmod 644 `find $$f ! -type d -print`;\
                 touch $$f;\
              else :; fi;\
           done);\
           HTX_PATH='$(STAR_DOCS):$(STAR_HELP)';\
           export HTX_PATH;\
           $(STAR_BIN)/hlink $(INSTALL_DOCS) $(INSTALL_HELP);\
        fi;
#
#  Install the conditions of use file and make it read-only to prevent its
#  date being changed.
	cp -p $(LICENCE) $(INSTALL_DATES)
	chmod 444 $(INSTALL_DATES)/$(LICENCE)
#
#  Install the date stamp file and make it read-only to prevent its
#  date being changed.
	cp -p $(DATE_STAMP) $(INSTALL_DATES)
	chmod 444 $(INSTALL_DATES)/$(DATE_STAMP)
	chmod 644 $(DATE_STAMP)
	rm $(DATE_STAMP)
	$(LINK) $(INSTALL_DATES)/$(DATE_STAMP) $(DATE_STAMP)
#
#  Create .INSTALLED_$(SYSTEM) (pointing to $INSTALL) to record that the
#  package is installed.
	@ echo $(INSTALL) > .INSTALLED_$(SYSTEM)

#
#  deinstall: Deinstall the package.
#  --------------------------------
#
#  Reverse the action of the install target, removing the installed
#  files and returning them to the source directory.

#  The deinstall target checks that the package is installed in the INSTALL
#  directory.  If so, it causes the do_deinstall target to be made which 
#  performs the deinstallation.


deinstall:
	cd lex; ./mk deinstall
	cd string; ./mk deinstall
	cd sock; ./mk deinstall
	cd atimer; ./mk deinstall
	cd msp; ./mk deinstall
	cd messys; ./mk deinstall
	cd adam; ./mk deinstall
	cd misc; ./mk deinstall
	cd subpar; ./mk deinstall
	cd parsecon; ./mk deinstall
	cd hdspar; ./mk deinstall
	cd dtask; ./mk deinstall
	cd ams; ./mk deinstall
	cd task; ./mk deinstall
#	cd adamnet; ./mk deinstall
	@ if test ! -f .INSTALLED_$(SYSTEM); then \
           echo;\
           echo '*** The $(PACK_NAME) package is not currently installed';\
           echo;\
        else \
           if test "`cat .INSTALLED_$(SYSTEM)`" = "$(INSTALL)"; then \
              if $(MAKE) do_deinstall; then \
                 echo;\
                 echo \
'*** The $(PACK_NAME) package has been deinstalled from directory $(INSTALL)';\
                 echo;\
              else \
                 echo;\
                 echo \
'*** "make" failed deinstalling the $(PACK_NAME) package from directory $(INSTALL)';\
                 echo;\
                 exit 1;\
              fi;\
           else \
              echo;\
              echo \
"*** The $(PACK_NAME) package is installed in `cat .INSTALLED_$(SYSTEM)`";\
              echo \
"*** and not in your INSTALL directory ($(INSTALL))";\
              echo '*** Not deinstalled';\
              exit 1;\
           fi; \
        fi

#  The do_deinstall target (which should never exist) checks that an
#  installed version of each file exists (in case an install failed
#  part of the way through) and returns it to the source directory,
#  using "cp -p" to preserve file dates. Links are removed from the
#  source directory before copying.

do_deinstall:
#
#  Note the package will need to be tested again.
	@- if test -f .TESTED_$(SYSTEM); then rm .TESTED_$(SYSTEM); else :; fi
#  Deinstall the top-level stuff
	$(MAKE) deinstall_pcs

#  Deinstall the top-level stuff
#  This is separated so that documentation etc may be handled without
#  having to do all the sub-packages
deinstall_pcs:
#  Deinstall the Latex documentation, if installed versions exist.
	- for f in $(LATEX_DOCS) ""; do \
           if test -n "$$f" -a -f $(INSTALL_DOCS)/$$f; then \
              chmod 644 $$f; rm -f $$f;\
              chmod 644 $(INSTALL_DOCS)/$$f;\
              cp -p $(INSTALL_DOCS)/$$f .;\
              rm -f $(INSTALL_DOCS)/$$f;\
           else :; fi;\
        done
#
#  Deinstall any hypertext documents,  and relink the hypertext if required.
	- if test -n "$(HYPERTEXT_DOCS)"; then \
           for f in $(HYPERTEXT_DOCS) ""; do \
              if test -n "$$f" -a -d $(INSTALL_DOCS)/$$f; then \
                 rm -f -r $(INSTALL_DOCS)/$$f;\
              else :; fi;\
           done;\
           HTX_PATH='$(STAR_DOCS):$(STAR_HELP)';\
           export HTX_PATH;\
           $(STAR_BIN)/hlink $(INSTALL_DOCS) $(INSTALL_HELP);\
        fi
#
#  Deinstall the conditions of use file after setting its protection so it may
#  be removed.
	- if test -f $(INSTALL_DATES)/$(LICENCE); then \
           chmod 644 $(INSTALL_DATES)/$(LICENCE);\
           rm -f $(INSTALL_DATES)/$(LICENCE);\
        else :; fi
#
#  Deinstall the date stamp file after setting its protection so it may
#  be removed.
	- if test -f $(INSTALL_DATES)/$(DATE_STAMP); then \
           chmod 644 $(DATE_STAMP); rm $(DATE_STAMP);\
           chmod 644 $(INSTALL_DATES)/$(DATE_STAMP);\
           cp -p $(INSTALL_DATES)/$(DATE_STAMP) ./;\
           rm $(INSTALL_DATES)/$(DATE_STAMP);\
        else :; fi
#
#  Note the system is no longer installed. Touch .BUILT, since we have
#  returned the built files to the source directory.
	@- rm .INSTALLED_$(SYSTEM) 1>/dev/null 2>/dev/null
	@ touch .BUILT

#  test: Perform an installation test.
#  ----------------------------------
#
#  Check that installed files are in their correct places and that a
#  simple test program will run correctly.

#  The test target checks that the package is currently installed. If
#  so, it causes the do_test target to be made, which performs the
#  installation test.

test:
	@ if test ! -f .INSTALLED_$(SYSTEM); then \
           echo;\
           echo '*** The $(PACK_NAME) package is not currently installed';\
           echo;\
        elif $(MAKE) do_test; then\
           echo;\
           echo \
           '*** Installation test for the $(PACK_NAME) package has been run';\
           echo;\
        else \
           echo;\
           echo \
           '*** Installation test for the $(PACK_NAME) package failed';\
           echo;\
           exit 1;\
        fi


#  The do_test target (which should never exist) performs the
#  installation test.

do_test: $(EXTERNAL_INCLUDES)
#
#  Note the test has not yet succeeded.
	@- if test -f .TESTED_$(SYSTEM); then rm .TESTED_$(SYSTEM); else :; fi
#
#  Extract the test program from the archive and set up new links for
#  the include files which point at the installed versions. Remove any
#  pre-existing links first if necessary.
	$(TAR_OUT) $(PKG_NAME)_source.tar \
                   $(PKG_NAME)_test.f $(PKG_NAME)_test.ifl
	-rm -f SAE_PAR 
	$(STAR_BIN)/star_dev
#
#  Build the test program, ensuring that the installed version of the library
#  and link files are used. We need to modify alink so it doesn't need the PAR
#  library.
	sed -e s/-lpar_adam// $(INSTALL_BIN)/alink > tlink
	chmod 755 tlink
	./tlink $(PKG_NAME)_test.f 
#  Compile the interface file, ensuring that the installed compifl is used.
	$(INSTALL_BIN)/compifl $(PKG_NAME)_test
#
#  Remove the test program source and the include file links used to
#  build it and the interface file source file
	-rm $(PKG_NAME)_test.f $(PKG_NAME)_test.ifl tlink
	if test -f $(PKG_NAME)_test.o; then rm $(PKG_NAME)_test.o; fi
	$(STAR_BIN)/star_dev remove
#
#  Execute the test program and remove the binary file and compiled interface
#  file when done. Note that any external mechanism for locating shareable 
#  libraries (e.g. a search path) must previously have been set up.
	./$(PKG_NAME)_test 5
	rm $(PKG_NAME)_test $(PKG_NAME)_test.ifc
#
#  Note the test has been run.
	@ touch .TESTED_$(SYSTEM)

#  export: Export the installed system.
#  -----------------------------------
#
#  Export the source plus all the built files to a new user.

#  The export target depends on the resulting compressed tar file being
#  up to date.

export: $(EXPORT)/$(PKG_NAME)_$(SYSTEM).tar.Z
	@ echo
	@ echo \
'*** Export copy of the built $(PACK_NAME) package is in the compressed'
	@ echo \
'    tar file $(EXPORT)/$(PKG_NAME)_$(SYSTEM).tar.Z'
	@ echo

#  The compressed tar file is up to date if it exists and is more
#  recent than all the source files and the date stamp file (which
#  records the time of the last build which modified any files).

$(EXPORT)/$(PKG_NAME)_$(SYSTEM).tar.Z: $(SOURCE_FILES) $(DATE_STAMP)
#
#  Issue a warning if the package has not been tested.
	@ if test ! -f .TESTED_$(SYSTEM); then \
           echo;\
           echo '*** Warning: the $(PACK_NAME) package has not been tested';\
           echo;\
        else :; fi
#
#  Remove any pre-existing tar files before creating new ones.
	if test -f $(EXPORT)/$(PKG_NAME)_$(SYSTEM).tar.Z; then \
           rm -f $(EXPORT)/$(PKG_NAME)_$(SYSTEM).tar.Z; else :; fi
	$(TAR_IN) - $(SOURCE_FILES) $(BUILT_FILES) $(DATE_STAMP) .BUILT \
           | compress -v > $(EXPORT)/$(PKG_NAME)_$(SYSTEM).tar.Z

#  export_run: Export the built system (without source).
#  -----------------------------------------------------
#
#  Export all the built files to a new user.

#  The export_run target depends on the resulting compressed tar file being
#  up to date.

export_run: $(EXPORT)/$(PKG_NAME)_$(SYSTEM)_run.tar.Z
	@ echo
	@ echo \
'*** Export copy of the "runtime" $(PACK_NAME) package is in the compressed'
	@ echo \
'    tar file $(EXPORT)/$(PKG_NAME)_$(SYSTEM)_run.tar.Z'
	@ echo

#  The compressed tar file is up to date if it exists and is more
#  recent than all the source files and the date stamp file (which
#  records the time of the last build which modified any files).

$(EXPORT)/$(PKG_NAME)_$(SYSTEM)_run.tar.Z: $(BUILT_FILES) $(MAKE_FILES) \
                                           $(DATE_STAMP)
#
#  Issue a warning if the package has not been tested.
	@ if test ! -f .TESTED_$(SYSTEM); then \
           echo;\
           echo '*** Warning: the $(PACK_NAME) package has not been tested';\
           echo;\
        else :; fi
#
#  Remove any pre-existing tar files before creating new ones.
	if test -f $(EXPORT)/$(PKG_NAME)_$(SYSTEM)_run.tar.Z; then \
           rm -f $(EXPORT)/$(PKG_NAME)_$(SYSTEM)_run.tar.Z; else :; fi
	$(TAR_IN) - $(MAKE_FILES) $(DOCUMENTATION) $(BUILT_FILES) \
           $(DATE_STAMP) .BUILT \
           | compress -v > $(EXPORT)/$(PKG_NAME)_$(SYSTEM)_run.tar.Z

#  export_source: Export the source.
#  --------------------------------
#
#  Export the source files only to a new user.

#  This target depends on the resulting compressed tar file being up to
#  date.

export_source: $(EXPORT)/$(PKG_NAME).tar.Z
	@ echo
	@ echo \
'*** Export copy of the $(PACK_NAME) package source is in the compressed'
	@ echo \
'    tar file $(EXPORT)/$(PKG_NAME).tar.Z'
	@ echo

#  The compressed tar file is up to date if it exists and is more
#  recent than all the source files.

$(EXPORT)/$(PKG_NAME).tar.Z: $(SOURCE_FILES)
#
#  Remove any pre-existing tar files before creating new ones.
	if test -f $(EXPORT)/$(PKG_NAME).tar.Z; then \
           rm -f $(EXPORT)/$(PKG_NAME).tar.Z; else :; fi
	$(TAR_IN) - $(SOURCE_FILES) \
           | compress -v > $(EXPORT)/$(PKG_NAME).tar.Z


#  Clean - remove intermediate files
# ----------------------------------
clean:
	cd lex; ./mk clean
	cd string; ./mk clean
	cd sock; ./mk clean
	cd atimer; ./mk clean
	cd msp; ./mk clean
	cd messys; ./mk clean
	cd adam; ./mk clean
	cd misc; ./mk clean
	cd subpar; ./mk clean
	cd parsecon; ./mk clean
	cd hdspar; ./mk clean
	cd dtask; ./mk clean
	cd ams; ./mk clean
	cd task; ./mk clean
#	cd adamnet; ./mk clean


# unbuild - remove all but the original files
# -------------------------------------------
unbuild:
	cd lex; ./mk unbuild
	cd string; ./mk unbuild
	cd sock; ./mk unbuild
	cd atimer; ./mk unbuild
	cd msp; ./mk unbuild
	cd messys; ./mk unbuild
	cd adam; ./mk unbuild
	cd misc; ./mk unbuild
	cd subpar; ./mk unbuild
	cd parsecon; ./mk unbuild
	cd hdspar; ./mk unbuild
	cd dtask; ./mk unbuild
	cd ams; ./mk unbuild
	cd task; ./mk unbuild
#	cd adamnet; ./mk unbuild
	@- rm -f $(DATE_STAMP) .BUILT 1>/dev/null 2>/dev/null
	@ echo '*** Built files removed'
	@ echo
