#============================================================================
# Rules for installation
#============================================================================

COPYDIRS ?= "cp -R" ;

INSTALL ?= "install" ;
INSTALL_PROGRAM ?= $(INSTALL) ;
INSTALL_SCRIPT ?= $(INSTALL) ;
INSTALL_DATA ?= "$(INSTALL) -m644" ;

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

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

##  InstallShellScript scriptname [ : subdir ]
##    Installs a shell script into the bindir directory. A subdirectory
##    relative to the bindir can be specified.
rule InstallShellScript
{
    SEARCH on $(<:G=installscript) = $(SUBDIR) ;
    Depends install_shellscript 
        : [ DoInstall $(<:G=installscript) : $(bindir) $(2) : $(INSTALL_SCRIPT) ]
    ;
}

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

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

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

##  InstallMan files
##    DoInstall man files
rule InstallMan
{
    local dir i ;
    
    SEARCH on $(<:G=installman) = $(SUBDIR) ;
    for i in $(<:G=installman) {
        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) ] ;
    }
}

##  InstallPixmap files [ : subdir ]
##    Install a pixmap
rule InstallPixmap
{
    LOCATE on $(<:G=installpixmap) = $(SUBDIR) ;
    Depends install_data
        : [ DoInstall $(<:G=installpixmap) : $(pixmapsdir) $(2) ] ;
}

##  InstallDesktop files [ : subdir ]
##    Install a .desktop file (menu entries for kde/gnome and others)
rule InstallDesktop
{
    LOCATE on $(<:G=installdesktop) = $(SUBDIR) ;
    Depends install_bin
        : [ DoInstall $(<:G=installdesktop) : $(applicationsdir) $(2) ] ;
}

##  DoInstall sourcename : directories [ : installapp ] [ : options ]
##    Creates a new installtarget for the given sources. The target(s) are
##    returned as function result.
rule DoInstall
{
    local targets target i dir gdir ;
    dir = [ ConcatDirs $(DESTDIR) $(2) ] ;

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

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

    # We want to package all files we install
    if ! [ IsElem nopackage : $(4) ] {
        Package $(<) ;
    }

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

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

INSTALLTARGETS = install_bin install_plugin install_lib install_header
		 install_shellscript install_data install_config
                 install_doc ;

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

Help install : "Install $(PACKAGE_NAME)" ;

actions Install1
{
  $(INSTALL) "$(>)" "$(<:D)" $(INSTALLIBS) ;
}

actions CopyDirs
{
  $(COPYDIRS) "$(>)" "$(<)"
}

