<!doctype html>
<html>
<head>
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta charset="utf-8">
  <title>LatticeSearch Setup</title>
  <hta:application id="LatticeSetup" applicationname="LatticeSearch Setup" border="thin" caption="yes" maximizebutton="no" minimizebutton="yes" scroll="no" singleinstance="yes" sysmenu="yes" windowstate="normal" />
  <style>
    html, body { margin:0; width:760px; height:560px; background:#101418; color:#e7edf2; font-family:"Segoe UI", Arial, sans-serif; }
    body { border-top:4px solid #35d399; }
    .shell { display:flex; height:556px; }
    .rail { width:210px; background:#151b21; border-right:1px solid #26313a; padding:28px 22px; box-sizing:border-box; }
    .brand { font-size:24px; font-weight:700; letter-spacing:0; margin-bottom:6px; }
    .tag { color:#91a1ad; font-size:13px; line-height:1.4; margin-bottom:30px; }
    .steps div { padding:9px 0; color:#7f8d98; font-size:13px; }
    .steps div.on { color:#35d399; font-weight:600; }
    .main { flex:1; padding:30px 34px 24px; box-sizing:border-box; position:relative; }
    h1 { margin:0 0 14px; font-size:26px; font-weight:650; }
    p { color:#aebbc4; line-height:1.5; font-size:14px; }
    label { display:block; margin:14px 0 6px; color:#d8e1e8; font-size:13px; font-weight:600; }
    input[type=text] { width:100%; box-sizing:border-box; padding:10px 12px; color:#e7edf2; background:#0d1115; border:1px solid #33414c; border-radius:6px; }
    button { border:0; border-radius:6px; padding:9px 14px; background:#27323b; color:#e7edf2; font-weight:600; cursor:pointer; }
    button.primary { background:#22b978; color:#07100c; }
    button:disabled { color:#65737e; background:#202830; cursor:default; }
    .row { display:flex; gap:10px; align-items:center; }
    .row input[type=text] { flex:1; }
    .check, .radio { display:flex; gap:10px; align-items:flex-start; margin:14px 0; color:#d8e1e8; font-size:14px; }
    .check input, .radio input { margin-top:3px; }
    .sub { color:#91a1ad; font-size:12px; margin-top:2px; }
    .panel { border:1px solid #2b3741; border-radius:8px; padding:14px; background:#121820; margin-top:14px; }
    .folders { height:104px; overflow:auto; background:#0d1115; border:1px solid #33414c; border-radius:6px; padding:8px; font-size:12px; color:#c8d3db; }
    .folders div { padding:4px 2px; border-bottom:1px solid #172029; }
    .status { white-space:pre-wrap; height:185px; overflow:auto; background:#0d1115; border:1px solid #33414c; border-radius:6px; padding:10px; color:#b7c5ce; font-family:Consolas, monospace; font-size:12px; }
    .footer { position:absolute; left:34px; right:34px; bottom:24px; display:flex; justify-content:space-between; align-items:center; }
    .muted { color:#7f8d98; font-size:12px; }
    .hidden { display:none; }
  </style>
  <script language="VBScript">
    Function IsAdmin()
      Dim sh, rc
      Set sh = CreateObject("WScript.Shell")
      rc = sh.Run("cmd /c net session >nul 2>nul", 0, True)
      IsAdmin = (rc = 0)
    End Function
  </script>
  <script language="javascript">
    var step = 1;
    var admin = false;
    var folders = [];
    function byId(id) { return document.getElementById(id); }
    function sourceRoot() {
      var p = unescape(location.pathname);
      if (p.charAt(0) == "/") p = p.substring(1);
      p = p.replace(/\//g, "\\");
      return p.replace(/\\installer\\LatticeSearch-Setup\.hta$/i, "");
    }
    function defaultDir() {
      var sh = new ActiveXObject("WScript.Shell");
      return admin ? "C:\\Program Files\\LatticeSearch" : sh.ExpandEnvironmentStrings("%LOCALAPPDATA%") + "\\LatticeSearch";
    }
    function init() {
      window.resizeTo(780, 620);
      admin = IsAdmin();
      byId("adminState").innerText = admin ? "Admin: yes" : "Admin: no";
      byId("installDir").value = defaultDir();
      showStep(1);
    }
    function showStep(n) {
      step = n;
      for (var i=1; i<=5; i++) {
        byId("page" + i).className = (i == n) ? "" : "hidden";
        byId("s" + i).className = (i == n) ? "on" : "";
      }
      byId("back").disabled = (n == 1);
      byId("next").className = (n == 5) ? "hidden" : "primary";
      byId("install").className = (n == 5) ? "primary" : "hidden";
      byId("finish").className = "hidden";
      byId("progress").innerText = "";
      syncWork();
    }
    function nextStep() { if (step < 5) showStep(step + 1); }
    function backStep() { if (step > 1) showStep(step - 1); }
    function browseInstall() {
      var shell = new ActiveXObject("Shell.Application");
      var f = shell.BrowseForFolder(0, "Choose the LatticeSearch install folder", 0, 0);
      if (f) byId("installDir").value = f.Self.Path;
    }
    function addFolder() {
      var shell = new ActiveXObject("Shell.Application");
      var f = shell.BrowseForFolder(0, "Choose a green-tape folder LatticeSearch may scan", 0, 0);
      if (f) { folders.push(f.Self.Path); renderFolders(); }
    }
    function clearFolders() { folders = []; renderFolders(); }
    function renderFolders() {
      var box = byId("folderList");
      box.innerHTML = "";
      for (var i=0; i<folders.length; i++) {
        var d = document.createElement("div");
        d.innerText = folders[i];
        box.appendChild(d);
      }
      if (!folders.length) box.innerHTML = "<span class='muted'>No folders selected.</span>";
    }
    function syncWork() {
      var work = byId("modeWork").checked;
      byId("workPanel").className = work ? "panel" : "panel hidden";
    }
    function q(s) { return '"' + String(s).replace(/"/g, '""') + '"'; }
    function installNow() {
      byId("back").disabled = true;
      byId("install").disabled = true;
      var sh = new ActiveXObject("WScript.Shell");
      var temp = sh.ExpandEnvironmentStrings("%TEMP%") + "\\latticesearch-install-" + (new Date()).getTime() + ".cmd";
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var script = [];
      var mode = byId("modeWork").checked ? "work" : "personal";
      var allow = folders.join("|");
      var bat = sourceRoot() + "\\installer\\" + (admin ? "install_admin.bat" : "install_user.bat");
      script.push("@echo off");
      script.push("set \"LS_INSTALL_DIR=" + byId("installDir").value + "\"");
      script.push("set \"LS_RIGHTCLICK=" + (byId("rightClick").checked ? "1" : "0") + "\"");
      script.push("set \"LS_AUTOSTART=" + (byId("autoStart").checked ? "1" : "0") + "\"");
      script.push("set \"LS_DESKTOP=" + (byId("desktopShortcut").checked ? "1" : "0") + "\"");
      script.push("set \"LS_ACCESS_MODE=" + mode + "\"");
      script.push("set \"LS_ALLOW_FOLDERS=" + allow + "\"");
      script.push("call " + q(bat));
      script.push("exit /b %ERRORLEVEL%");
      var file = fso.CreateTextFile(temp, true);
      file.Write(script.join("\r\n"));
      file.Close();
      byId("progress").innerText = "Running installer...\n" + bat + "\n";
      var rc = sh.Run("cmd /c " + q(temp), 1, true);
      byId("progress").innerText += "\nInstaller exited with code " + rc + ".";
      byId("finish").className = "primary";
      byId("install").className = "hidden";
    }
    function finishNow() {
      if (byId("launchNow").checked) {
        var sh = new ActiveXObject("WScript.Shell");
        sh.Run(q(byId("installDir").value + "\\LatticeSearch-Launch.bat"), 1, false);
      }
      window.close();
    }
  </script>
</head>
<body onload="init()">
  <div class="shell">
    <div class="rail">
      <div class="brand">LatticeSearch</div>
      <div class="tag">Local search, private by default, installed for the way this Windows account is allowed to run.</div>
      <div class="steps">
        <div id="s1">1. Welcome</div>
        <div id="s2">2. Location</div>
        <div id="s3">3. Options</div>
        <div id="s4">4. Privacy</div>
        <div id="s5">5. Install</div>
      </div>
    </div>
    <div class="main">
      <div id="page1">
        <h1>Install LatticeSearch</h1>
        <p>This wizard installs the local web app and creates the Windows entries you choose. It can run without elevation; administrator rights only change the default location and machine-wide shortcut/startup behavior.</p>
        <div class="panel">
          <strong id="adminState">Admin: checking...</strong>
          <p class="sub">Detection uses the Windows net session elevation check through VBScript. A failed check means the wizard will use the no-admin installer.</p>
        </div>
      </div>
      <div id="page2" class="hidden">
        <h1>Install Location</h1>
        <p>Choose where the app files will be copied. The launcher stores mutable data in a data folder for user installs, or in ProgramData for administrator installs.</p>
        <label for="installDir">Destination folder</label>
        <div class="row"><input id="installDir" type="text"><button onclick="browseInstall()">Browse</button></div>
      </div>
      <div id="page3" class="hidden">
        <h1>Options</h1>
        <label class="check"><input id="rightClick" type="checkbox" checked> <span>Right-click menu integration<div class="sub">Adds per-user Explorer actions, including Search this folder.</div></span></label>
        <label class="check"><input id="autoStart" type="checkbox"> <span>Start LatticeSearch when I log in<div class="sub">Adds a Windows startup entry.</div></span></label>
        <label class="check"><input id="desktopShortcut" type="checkbox" checked> <span>Desktop shortcut<div class="sub">Creates a shortcut beside the Start Menu entry.</div></span></label>
      </div>
      <div id="page4" class="hidden">
        <h1>Privacy Mode</h1>
        <label class="radio"><input id="modePersonal" name="mode" type="radio" checked onclick="syncWork()"> <span>Personal<div class="sub">Full access, except red-tape folders that are explicitly denied by policy or future settings.</div></span></label>
        <label class="radio"><input id="modeWork" name="mode" type="radio" onclick="syncWork()"> <span>Work<div class="sub">Green-tape only: LatticeSearch scans only folders you allow. Red-tape deny rules always win.</div></span></label>
        <div id="workPanel" class="panel hidden">
          <div class="row"><button onclick="addFolder()">Add Folder</button><button onclick="clearFolders()">Clear</button></div>
          <div id="folderList" class="folders"><span class="muted">No folders selected.</span></div>
        </div>
      </div>
      <div id="page5" class="hidden">
        <h1>Install</h1>
        <p>Review your choices, then install. The installer window shows file copy, shortcut, privacy, and integration progress.</p>
        <div id="progress" class="status"></div>
        <label class="check"><input id="launchNow" type="checkbox" checked> <span>Launch now</span></label>
      </div>
      <div class="footer">
        <span class="muted">LatticeSearch local setup</span>
        <span>
          <button id="back" onclick="backStep()">Back</button>
          <button id="next" class="primary" onclick="nextStep()">Next</button>
          <button id="install" class="hidden" onclick="installNow()">Install</button>
          <button id="finish" class="hidden" onclick="finishNow()">Finish</button>
        </span>
      </div>
    </div>
  </div>
</body>
</html>
