if(WIN32)
  set(sak_endl "\r\n")
else()
  set(sak_endl "\n")
endif()

# config.xml
set(config_file ${ROOT_DIR}/config/config.xml)
set(target_dir "@HomeDir@/AppData/Roaming/Qsaker")
file(WRITE ${config_file} "<?xml version=\"1.0\" encoding=\"UTF-8\"?>${sak_endl}")
file(APPEND ${config_file} "<Installer>${sak_endl}")
file(APPEND ${config_file} "    <Name>${TARGET}</Name>${sak_endl}")
file(APPEND ${config_file} "    <Version>${VERSION}</Version>${sak_endl}")
file(APPEND ${config_file} "    <Title>${TARGET}</Title>${sak_endl}")
file(APPEND ${config_file} "    <Publisher>Qsaker</Publisher>${sak_endl}")
file(APPEND ${config_file} "    <StartMenuDir>Qsaker/${TARGET}</StartMenuDir>${sak_endl}")
file(APPEND ${config_file} "    <TargetDir>${target_dir}/${TARGET}</TargetDir>${sak_endl}")
file(APPEND ${config_file} "    <RemoveTargetDir>false</RemoveTargetDir>${sak_endl}")
file(APPEND ${config_file} "    <WizardDefaultHeight>420</WizardDefaultHeight>${sak_endl}")
file(APPEND ${config_file} "</Installer>")

# package.xml
set(package_file ${ROOT_DIR}/packages/${TARGET}/meta/package.xml)
string(TIMESTAMP PACKING_DATE %Y-%m-%d)
file(WRITE ${package_file} "<?xml version=\"1.0\" encoding=\"UTF-8\"?>${sak_endl}")
file(APPEND ${package_file} "<Package>${sak_endl}")
file(APPEND ${package_file} "    <DisplayName>EasyDebug</DisplayName>${sak_endl}")
file(APPEND ${package_file} "    <Description>Installer ${TARGET}</Description>${sak_endl}")
file(APPEND ${package_file} "    <Version>${VERSION}</Version>${sak_endl}")
file(APPEND ${package_file} "    <Script>installscript.qs</Script>${sak_endl}")
file(APPEND ${package_file} "    <ReleaseDate>${PACKING_DATE}</ReleaseDate>${sak_endl}")
file(APPEND ${package_file} "    <Default>true</Default>${sak_endl}")
file(APPEND ${package_file} "    <ForcedInstallation>true</ForcedInstallation>${sak_endl}")
file(APPEND ${package_file} "</Package>${sak_endl}")

# installscript.qs
set(script_file ${ROOT_DIR}/packages/${TARGET}/meta/installscript.qs)
file(WRITE ${script_file} "function Component() {}${sak_endl}")
file(APPEND ${script_file} "Component.prototype.createOperations = function() {${sak_endl}")
file(APPEND ${script_file} "component.createOperations();${sak_endl}")
file(APPEND ${script_file} "    if (systemInfo.productType === \"windows\") {${sak_endl}")
file(APPEND ${script_file} "        component.addOperation(\"CreateShortcut\",${sak_endl}")
file(APPEND ${script_file} "            \"@TargetDir@/${TARGET}.exe\",${sak_endl}")
file(APPEND ${script_file} "            \"@StartMenuDir@/${TARGET}.lnk\",${sak_endl}")
file(APPEND ${script_file} "            \"workingDirectory=@TargetDir@\",${sak_endl}")
file(APPEND ${script_file} "            \"iconPath=@TargetDir@/icon.ico\",${sak_endl}")
file(APPEND ${script_file} "            \"description=Open ${TARGET} Program\");${sak_endl}")
file(APPEND ${script_file} "        component.addOperation(\"CreateShortcut\",${sak_endl}")
file(APPEND ${script_file} "            \"@TargetDir@/${TARGET}.exe\",${sak_endl}")
file(APPEND ${script_file} "            \"@DesktopDir@/${TARGET}.lnk\",${sak_endl}")
file(APPEND ${script_file} "            \"workingDirectory=@TargetDir@\",${sak_endl}")
file(APPEND ${script_file} "            \"iconPath=@TargetDir@/icon.ico\",${sak_endl}")
file(APPEND ${script_file} "            \"description=Open ${TARGET} Program\");${sak_endl}")
file(APPEND ${script_file} "        component.addOperation(\"CreateShortcut\",${sak_endl}")
file(APPEND ${script_file} "            \"@TargetDir@/maintenancetool.exe\",${sak_endl}")
file(APPEND ${script_file} "            \"@StartMenuDir@/${TARGET} Uninstaller.lnk\",${sak_endl}")
file(APPEND ${script_file} "            \"workingDirectory=@TargetDir@\",${sak_endl}")
file(APPEND ${script_file} "            \"iconPath=%SystemRoot%/system32/SHELL32.dll\",${sak_endl}")
file(APPEND ${script_file} "            \"iconId=101\",${sak_endl}")
file(APPEND ${script_file} "            \"description=Uninstall ${TARGET} Program\",${sak_endl}")
file(APPEND ${script_file} "            \"--start-uninstaller\");${sak_endl}")
file(APPEND ${script_file} "    }${sak_endl}")
file(APPEND ${script_file} "}${sak_endl}")
