#============================================================================
# Rules for installation
# Copyright (C)2003 by Matze Braun <matzebraun@users.sourceforge.net>
# Copyright (C)2004 by Eric Sunshine <sunshine@sunshineco.com>
#
#    This library is free software; you can redistribute it and/or modify it
#    under the terms of the GNU Library General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or (at your
#    option) any later version.
#
#    This library is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
#    License for more details.
#
#    You should have received a copy of the GNU Library General Public License
#    along with this library; if not, write to the Free Software Foundation,
#    Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#============================================================================

INSTALL ?= "install" ;
INSTALL_PROGRAM ?= $(INSTALL) ;
INSTALL_DATA ?= "$(INSTALL) -m 644" ;

# set some paths
appdatadir ?= [ ConcatDirs $(datadir) $(PACKAGE_NAME) ] ;
appdocdir ?= [ ConcatDirs $(datadir) doc $(PACKAGE_NAME)-$(PACKAGE_VERSION) ] ;
appconfdir ?= [ ConcatDirs $(sysconfdir) $(PACKAGE_NAME) ] ;
appincdir ?= [ ConcatDirs $(includedir) $(PACKAGE_NAME) ] ;
plugindir ?= [ ConcatDirs $(libdir) $(PACKAGE_NAME) ] ;

rule GristInstall
{
  local i, files ;
  for i in $(<)
  {
    if $(i:G) { files += $(i) ; }
    else { files += $(i:G=$(SUBDIR)) ; }
  }
  return $(files) ;
}

##  InstallHeader headername [ : subdir ]
##    DoInstall a header file into the includedir directory. A subdirectory
##    relative to the includedir can be specified.
rule InstallHeader
{
    local files = [ GristInstall $(<) ] ;
    SEARCH on $(files) = $(SUBDIR) ;
    Depends install_include : [ DoInstall $(files) : $(appincdir) $(2) ] ;
}

##  InstallData files [ : subdir ]
##    Installs data files
rule InstallData
{
    local files = [ GristInstall $(<) ] ;
    SEARCH on $(files) = $(SUBDIR) ;
    Depends install_data : [ DoInstall $(files) : $(appdatadir) $(2) ] ;
}

##  InstallConfig files [ : subdir ]
##    Installs configuration files
rule InstallConfig
{
    local files = [ GristInstall $(<) ] ;
    SEARCH on $(files) = $(SUBDIR) ;
    Depends install_config : [ DoInstall $(files) : $(appconfdir) $(2) ] ;
}

##  InstallDoc files [ : subdir ]
##    Installs documentation files
rule InstallDoc
{
    local files = [ GristInstall $(<) ] ;
    SEARCH on $(files) = $(SUBDIR) ;
    Depends install_doc : [ DoInstall $(files) : $(appdocdir) $(2) ] ;
}

##  InstallMan files
##    Installs Unix manual files
rule InstallMan
{
    local files = [ GristInstall $(<) ] ;
    SEARCH on $(files) = $(SUBDIR) ;

    local i ;
    for i in $(files)
    {
        local dir = $(mandir) ;
        switch $(i:S)
        {
            case .1 : dir += man1 ;
            case .2 : dir += man2 ;
            case .3 : dir += man3 ;
            case .4 : dir += man4 ;
            case .5 : dir += man5 ;
            case .6 : dir += man6 ;
            case .7 : dir += man7 ;
            case .8 : dir += man8 ;
            case .9 : dir += man9 ;
            case * :
                echo "WARNING: manfile has no *.[0-9] ending." ;
        }
        Depends install_man : [ DoInstall $(i) : $(dir) ] ;
    }
}

##  InstallProgram files [ : directories ]
##    Installs program files (executable or shell script). This is a
##    convenience wrapper for DoInstall when the resources to be installed is
##    of an invocable nature. It sets SEARCH on `files', and uses
##    $(INSTALL_PROGRAM) to perform the actual installation. Unlike
##    SystemInstallApplication, it does not assume that all such targets should
##    be installed into $(bindir); instead, you can provide `directories' to
##    specify the installation location. If `directories' is not given, then
##    $(bindir) is assumed. Also, unlike SystemInstallApplication, this rule
##    does not have any platform-specific knowledge (such as how to install a
##    Cocoa application wrapper on MacOS/X). Always use
##    SystemInstallApplication for installation of full-fledged applications. A
##    typical use for InstallProgram would be to install an already-existing
##    shell script. This differs from the ShellScript rule which both creates a
##    shell script from scratch at build time, and arranges for it to be
##    installed. Like DoInstall, this rule returns the names of the installed
##    targets, so it is convenient to use the results as the input for another
##    rule, such as Depends.
rule InstallProgram
{
    local files = [ GristInstall $(1) ] ;
    local dir = $(2) ;
    if ! $(dir) { dir = $(bindir) ; }
    SEARCH on $(files) = $(SUBDIR) ;
    return [ DoInstall $(files) : $(dir) : $(INSTALL_PROGRAM) ] ;
}

##  DoInstall sourcenames : directories [ : installapp : [ postinstallrules ]]
##    Creates a new install target for the given sources named by
##    `sourcenames'.  `directories' is a list of directory components
##    indicating the installation directory for `sourcename'.  `installapp' is
##    the actual program to run to install the sources.  If not specified, then
##    $(INSTALL_DATA) is used.  If the optional `postinstallrules' is provided,
##    it is a list of Jam rules to invoke on the installed target after it is
##    installed.  Each rule is invoked with the installed target as the first
##    argument, and the source target as the second. An obvious instance where
##    `postinstallrules' proves useful is when the Ranlib rule should be
##    invoked on just-installed static library (.a) files.  The DoInstall rule
##    returns the names of the installed targets, so it is convenient to use
##    the results as the input for another rule.  For example:
##        Depends install : [ DoInstall $(docfiles) : $(installdocdir) ] ;
##    (Implementation Note: We did not name this rule Install, because Jambase
##    already defines an action named Install :-/)
rule DoInstall
{
    local targets target i dir gdir ;
    dir = [ ConcatDirs $(DESTDIR) $(2) ] ;

    gdir = $(dir:G=dir) ;
    MkDir $(gdir) ;
  
    for i in $(1)
    {
        target = $(i:BSR=$(dir):G=install) ;
        targets += $(target) ;
        Depends $(target) : $(gdir) $(i) ;
        Install1 $(target) : $(i) ;

        if "$(3)"
	{
            INSTALL_APP on $(target) = $(3) ;
        }
	else
	{
            INSTALL_APP on $(target) = $(INSTALL_DATA) ;
        }

	if "$(4)"
	{
	    local postrule ;
	    for postrule in $(4)
	    {
		$(postrule) $(target) : $(i) ;
	    }
	}
    }

    Always $(targets) ;
    return $(targets) ;
}

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

INSTALLTARGETS = install_bin install_plugin install_lib install_include
		 install_data install_config install_doc ;

Always install $(INSTALLTARGETS) ;
NotFile install $(INSTALLTARGETS) ;
Depends install : $(INSTALLTARGETS) ;

if ! $(PACKAGE_INSTALL_NAME) { PACKAGE_INSTALL_NAME = $(PACKAGE_LONGNAME) ; }
if ! $(PACKAGE_INSTALL_NAME) { PACKAGE_INSTALL_NAME = $(PACKAGE_NAME) ; }
if ! $(PACKAGE_INSTALL_NAME) { PACKAGE_INSTALL_NAME = "the project" ; }
Help install : "Install $(PACKAGE_INSTALL_NAME)" ;

actions Install1
{
  $(INSTALL_APP) $(INSTALLFLAGS) $(>) $(<)
}

actions CopyDirs
{
  $(DEEPCOPY) $(>) $(<)
}
