# Sis file creation
GENERATE_SIS_TARGETS = false

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

equals(GENERATE_SIS_TARGETS, true) {

    baseTarget = $$symbianRemoveSpecialCharacters($$basename(TARGET))
    template_pkg_target.target = $${baseTarget}_template.pkg
    template_pkg_target.depends += $$_PRO_FILE_
    template_pkg_target.commands = $(MAKE) -f $(MAKEFILE) qmake
    installer_pkg_target.target = $${baseTarget}_installer.pkg
    installer_pkg_target.depends += $$_PRO_FILE_
    installer_pkg_target.commands = $(MAKE) -f $(MAKEFILE) qmake
    stub_pkg_target.target = $${baseTarget}_stub.pkg
    stub_pkg_target.depends += $$_PRO_FILE_
    stub_pkg_target.commands = $(MAKE) -f $(MAKEFILE) qmake
    QMAKE_EXTRA_TARGETS += template_pkg_target installer_pkg_target stub_pkg_target

    qtPrepareTool(QMAKE_CREATEPACKAGE, createpackage)

    symbian-abld|symbian-sbsv2 {
        symbian-sbsv2 {
            CONVERT_GCCE_PARAM = -g
        } else {
            CONVERT_GCCE_PARAM =
        }
        
        make_cache_name = .make.cache
        sis_target.target = sis
        sis_target.commands = $(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 \
                                  ) \
                              )

        sis_target.depends += $${baseTarget}_template.pkg

        ok_sis_target.target = ok_sis
        ok_sis_target.commands = $$QMAKE_CREATEPACKAGE $$CONVERT_GCCE_PARAM $(QT_SIS_OPTIONS) $${baseTarget}_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 $$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 \
                                          ) \
                                      )

        unsigned_sis_target.depends += $${baseTarget}_template.pkg

        ok_unsigned_sis_target.target = ok_unsigned_sis
        ok_unsigned_sis_target.commands = $$QMAKE_CREATEPACKAGE $$CONVERT_GCCE_PARAM $(QT_SIS_OPTIONS) -o $${baseTarget}_template.pkg $(QT_SIS_TARGET)

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

        # The installer_sis target has dependency to sis target, so it will regenerate sis package.
        # To create smart installer wrapper for for an existing sis package, use ok_installer_sis target directly.
        installer_sis_target.target = installer_sis
        installer_sis_target.commands = $(MAKE) -f $(MAKEFILE) ok_installer_sis
        installer_sis_target.depends = $${baseTarget}_installer.pkg sis

        ok_installer_sis_target.target = ok_installer_sis
        ok_installer_sis_target.commands = $$QMAKE_CREATEPACKAGE $(QT_SIS_OPTIONS) $${baseTarget}_installer.pkg - \
                                               $(QT_SIS_CERTIFICATE) $(QT_SIS_KEY) $(QT_SIS_PASSPHRASE)
        ok_installer_sis_target.depends = $${baseTarget}_installer.pkg

        unsigned_installer_sis_target.target = unsigned_installer_sis
        unsigned_installer_sis_target.commands = $(MAKE) -f $(MAKEFILE) ok_unsigned_installer_sis
        unsigned_installer_sis_target.depends = $${baseTarget}_installer.pkg unsigned_sis

        ok_unsigned_installer_sis_target.target = ok_unsigned_installer_sis
        ok_unsigned_installer_sis_target.commands = $$QMAKE_CREATEPACKAGE $(QT_SIS_OPTIONS) -o $${baseTarget}_installer.pkg
        ok_unsigned_installer_sis_target.depends = $${baseTarget}_installer.pkg

        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 $$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 \
                                       ) \
                                   )

        stub_sis_target.depends += $${baseTarget}_stub.pkg

        ok_stub_sis_target.target = ok_stub_sis
        ok_stub_sis_target.commands = $$QMAKE_CREATEPACKAGE -s $(QT_SIS_OPTIONS) $${baseTarget}_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 \
                               unsigned_installer_sis_target \
                               ok_unsigned_installer_sis_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 QT_SIS_TARGET ?= $(QT_SIS_TARGET) >> $$make_cache_name

            QMAKE_EXTRA_TARGETS += store_build_target
        }
    } else {
        sis_destdir = $$DESTDIR
        isEmpty(sis_destdir):sis_destdir = .
        !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 $${baseTarget}_template.pkg

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

        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 = $(MAKE) -f $(MAKEFILE) ok_installer_sis
        installer_sis_target.depends = $${baseTarget}_installer.pkg sis

        ok_installer_sis_target.target = ok_installer_sis
        ok_installer_sis_target.commands = $$QMAKE_CREATEPACKAGE $(QT_SIS_OPTIONS) $${baseTarget}_installer.pkg - \
                                            $(QT_SIS_CERTIFICATE) $(QT_SIS_KEY) $(QT_SIS_PASSPHRASE)
        ok_installer_sis_target.depends = $${baseTarget}_installer.pkg

        unsigned_installer_sis_target.target = unsigned_installer_sis
        unsigned_installer_sis_target.commands = $$QMAKE_CREATEPACKAGE $(QT_SIS_OPTIONS) -o $${baseTarget}_installer.pkg
        unsigned_installer_sis_target.depends = $${baseTarget}_installer.pkg unsigned_sis

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

        QMAKE_EXTRA_TARGETS += sis_target \
                               unsigned_sis_target \
                               target_sis_target \
                               installer_sis_target \
                               ok_installer_sis_target \
                               unsigned_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
    }
}
