# Sis file creation
GENERATE_SIS_TARGETS = false

contains(TEMPLATE, app): GENERATE_SIS_TARGETS = true
else:!equals(DEPLOYMENT, default_deployment) {
    for(dep_item, $$list($$DEPLOYMENT)) {
        eval(dep_item_sources = $${dep_item}.sources)
        !isEmpty(dep_item_sources): GENERATE_SIS_TARGETS = true
    }
}

equals(GENERATE_SIS_TARGETS, true) {
    symbian-abld|symbian-sbsv2 {
        sis_destdir =
        make_cache_name = .make.cache
        sis_target.target = sis
        sis_target.commands = $(if $(wildcard $$basename(TARGET)_template.pkg), \
                                  $(if $(wildcard $$make_cache_name), \
                                      $(MAKE) -f $(MAKEFILE) ok_sis MAKEFILES=$$make_cache_name \
                                  , \
                                      $(if $(QT_SIS_TARGET), \
                                          $(MAKE) -f $(MAKEFILE) ok_sis \
                                      , \
                                          $(MAKE) -f $(MAKEFILE) fail_sis_nocache \
                                      ) \
                                  ) \
                              , \
                                  $(MAKE) -f $(MAKEFILE) fail_sis_nopkg \
                              )

        ok_sis_target.target = ok_sis
        ok_sis_target.commands = createpackage.bat $(QT_SIS_OPTIONS) $$basename(TARGET)_template.pkg \
                                     $(QT_SIS_TARGET) $(QT_SIS_CERTIFICATE) $(QT_SIS_KEY) $(QT_SIS_PASSPHRASE)

        unsigned_sis_target.target = unsigned_sis
        unsigned_sis_target.commands = $(if $(wildcard $$basename(TARGET)_template.pkg), \
                                  $(if $(wildcard $$make_cache_name), \
                                      $(MAKE) -f $(MAKEFILE) ok_unsigned_sis MAKEFILES=$$make_cache_name \
                                  , \
                                      $(if $(QT_SIS_TARGET), \
                                          $(MAKE) -f $(MAKEFILE) ok_unsigned_sis \
                                      , \
                                          $(MAKE) -f $(MAKEFILE) fail_sis_nocache \
                                      ) \
                                  ) \
                              , \
                                  $(MAKE) -f $(MAKEFILE) fail_sis_nopkg \
                              )

        ok_unsigned_sis_target.target = ok_unsigned_sis
        ok_unsigned_sis_target.commands = createpackage.bat $(QT_SIS_OPTIONS) -o $$basename(TARGET)_template.pkg $(QT_SIS_TARGET)

        target_sis_target.target = $${sis_destdir}$${TARGET}.sis
        target_sis_target.commands = $(MAKE) -f $(MAKEFILE) sis

        installer_sis_target.target = installer_sis
        installer_sis_target.commands = $(if $(wildcard $$basename(TARGET)_installer.pkg), \
                                            $(MAKE) -f $(MAKEFILE) ok_installer_sis \
                                        , \
                                            $(MAKE) -f $(MAKEFILE) fail_sis_nopkg \
                                        )
        installer_sis_target.depends = $${sis_destdir}$${TARGET}.sis

        ok_installer_sis_target.target = ok_installer_sis
        ok_installer_sis_target.commands = createpackage.bat $(QT_SIS_OPTIONS) $$basename(TARGET)_installer.pkg - \
                                               $(QT_SIS_CERTIFICATE) $(QT_SIS_KEY) $(QT_SIS_PASSPHRASE)

        fail_sis_nopkg_target.target = fail_sis_nopkg
        fail_sis_nopkg_target.commands = "$(error PKG file does not exist, 'sis' and 'installer_sis' target are only supported for executables or projects with DEPLOYMENT statement)"

        fail_sis_nocache_target.target = fail_sis_nocache
        fail_sis_nocache_target.commands = "$(error Project has to be built or QT_SIS_TARGET environment variable has to be set before calling 'SIS' target)"

        stub_sis_target.target = stub_sis
        stub_sis_target.commands = $(if $(wildcard $$basename(TARGET)_template.pkg), \
                                       $(if $(wildcard $$make_cache_name), \
                                           $(MAKE) -f $(MAKEFILE) ok_stub_sis MAKEFILES=$$make_cache_name \
                                       , \
                                           $(if $(QT_SIS_TARGET), \
                                               $(MAKE) -f $(MAKEFILE) ok_stub_sis \
                                           , \
                                               $(MAKE) -f $(MAKEFILE) fail_sis_nocache \
                                           ) \
                                       ) \
                                   , \
                                       $(MAKE) -f $(MAKEFILE) fail_sis_nopkg \
                                   )

        ok_stub_sis_target.target = ok_stub_sis
        ok_stub_sis_target.commands = createpackage.bat -s $(QT_SIS_OPTIONS) $$basename(TARGET)_stub.pkg \
                                          $(QT_SIS_TARGET) $(QT_SIS_CERTIFICATE) $(QT_SIS_KEY) $(QT_SIS_PASSPHRASE)

        QMAKE_EXTRA_TARGETS += sis_target \
                               ok_sis_target \
                               unsigned_sis_target \
                               ok_unsigned_sis_target \
                               target_sis_target \
                               installer_sis_target \
                               ok_installer_sis_target \
                               fail_sis_nopkg_target \
                               fail_sis_nocache_target \
                               stub_sis_target \
                               ok_stub_sis_target
        # Sbsv2 has its own store_build target which is using flms.
        !symbian-sbsv2 {
            contains(QMAKE_HOST.os, "Windows") {
                shellFixedHash = $${LITERAL_HASH}
            } else {
                shellFixedHash = \\$${LITERAL_HASH}
            }
            store_build_target.target = store_build
            store_build_target.commands = \
                  @echo $${shellFixedHash} ============================================================================== > $$make_cache_name \
                && echo $${shellFixedHash}   This file is generated by make and should not be modified by the user >> $$make_cache_name \
                && echo $${shellFixedHash}  Name        : $$make_cache_name >> $$make_cache_name \
                && echo $${shellFixedHash}  Part of     : lineedits >> $$make_cache_name \
                && echo $${shellFixedHash}  Description : This file is used to cache last build target for >> $$make_cache_name \
                && echo $${shellFixedHash}                make sis target. >> $$make_cache_name \
                && echo $${shellFixedHash}  Version     :  >> $$make_cache_name \
                && echo $${shellFixedHash} >> $$make_cache_name \
                && echo $${shellFixedHash} ============================================================================== >> $$make_cache_name \
                && echo. >> $$make_cache_name \
                && echo QT_SIS_TARGET ?= $(QT_SIS_TARGET) >> $$make_cache_name

            QMAKE_EXTRA_TARGETS += store_build_target
        }
    } else {
        qtPrepareTool(QMAKE_CREATEPACKAGE, createpackage)

        sis_destdir = $$DESTDIR
        isEmpty(sis_destdir):sis_destdir = .
        baseTarget = $$basename(TARGET)
        !equals(TARGET, "$$baseTarget"):sis_destdir = $$sis_destdir/$$dirname(TARGET)

        sis_target.target = sis
        sis_target.commands = $$QMAKE_CREATEPACKAGE $(QT_SIS_OPTIONS) $${baseTarget}_template.pkg \
                                  - $(QT_SIS_CERTIFICATE) $(QT_SIS_KEY) $(QT_SIS_PASSPHRASE)
        sis_target.depends = first

        unsigned_sis_target.target = unsigned_sis
        unsigned_sis_target.commands = $$QMAKE_CREATEPACKAGE $(QT_SIS_OPTIONS) -o $${baseTarget}_template.pkg
        unsigned_sis_target.depends = first

        target_sis_target.target = $${sis_destdir}/$${baseTarget}.sis
        target_sis_target.commands = $(MAKE) -f $(MAKEFILE) sis

        installer_sis_target.target = installer_sis
        installer_sis_target.commands = $$QMAKE_CREATEPACKAGE $(QT_SIS_OPTIONS) $${baseTarget}_installer.pkg - \
                                            $(QT_SIS_CERTIFICATE) $(QT_SIS_KEY) $(QT_SIS_PASSPHRASE)
        installer_sis_target.depends = $${sis_destdir}/$${baseTarget}.sis

        !isEmpty(sis_destdir):!equals(sis_destdir, "."):!equals(sis_destdir, "./") {
            sis_target.commands += && $$QMAKE_MOVE $${baseTarget}.sis $$sis_destdir
            installer_sis_target.commands += && $$QMAKE_MOVE $${baseTarget}.sis $$sis_destdir
        }

        QMAKE_EXTRA_TARGETS += sis_target \
                               unsigned_sis_target \
                               target_sis_target \
                               installer_sis_target

        QMAKE_DISTCLEAN += $${sis_destdir}/$${baseTarget}.sis
    }

    deploy_target.target = deploy
    contains(QMAKE_HOST.os, "Windows") {
        deploy_target.depends = sis
        deploy_target.commands = call $$target_sis_target.target
    } else {
        deploy_target.commands = @echo Deployment not supported in this environment
    }
    QMAKE_EXTRA_TARGETS += deploy_target

} else {
    contains(TEMPLATE, subdirs) {
        # Enable recursive sis target.
        sis_target.CONFIG = recursive
        sis_target.recurse = $$SUBDIRS
    } else {
        # Make sure we build everything, since other sis targets in a recursive invocation
        # may depend on them, even if this one is empty.
        # In abld/sbsv2, we assume that subdir has been built already, as all builds are recursive.
        !symbian-abld:!symbian-sbsv2: sis_target.depends = first
    }
    sis_target.commands =
    sis_target.target = sis
    QMAKE_EXTRA_TARGETS += sis_target

    symbian-abld {
        # Create dummy store_build target to avoid errors from .mk callbacks during build
        store_build_target.target = store_build
        QMAKE_EXTRA_TARGETS += store_build_target
    }
}