#!/usr/bin/env node
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
  if (typeof require !== "undefined") return require.apply(this, arguments);
  throw Error('Dynamic require of "' + x + '" is not supported');
});

// scripts/postinstall.ts
import { existsSync, mkdirSync, readFileSync, writeFileSync, appendFileSync, copyFileSync, renameSync, unlinkSync } from "fs";
import { homedir, tmpdir } from "os";
import { join } from "path";
var LOG_FILE = join(tmpdir(), "opencode-orchestrator.log");
function log(message, data) {
  try {
    const timestamp = (/* @__PURE__ */ new Date()).toISOString();
    const entry = `[${timestamp}] [postinstall] ${message} ${data ? JSON.stringify(data) : ""}
`;
    appendFileSync(LOG_FILE, entry);
  } catch {
  }
}
function formatError(err, context) {
  if (err instanceof Error) {
    const nodeErr = err;
    if (nodeErr.code === "EACCES" || nodeErr.code === "EPERM") {
      return `Permission denied: Cannot ${context}. Try running as administrator.`;
    }
    if (nodeErr.code === "ENOENT") {
      return `File not found while trying to ${context}.`;
    }
    if (err instanceof SyntaxError) {
      return `JSON syntax error while trying to ${context}: ${err.message}.`;
    }
    if (nodeErr.code === "EIO") {
      return `File lock error: Cannot ${context}. Please close OpenCode and try again.`;
    }
    if (nodeErr.code === "ENOSPC") {
      return `Disk full: Cannot ${context}. Free up disk space and try again.`;
    }
    if (nodeErr.code === "EROFS") {
      return `Read-only filesystem: Cannot ${context}.`;
    }
    return `Failed to ${context}: ${err.message}`;
  }
  return `Failed to ${context}: ${String(err)}`;
}
var PLUGIN_NAME = "opencode-orchestrator";
function getConfigPaths() {
  const paths = [];
  if (process.env.XDG_CONFIG_HOME) {
    paths.push(join(process.env.XDG_CONFIG_HOME, "opencode"));
  }
  if (process.platform === "win32") {
    const appDataPath = process.env.APPDATA || join(homedir(), "AppData", "Roaming");
    paths.push(join(appDataPath, "opencode"));
    const dotConfigPath = join(homedir(), ".config", "opencode");
    if (!paths.includes(dotConfigPath)) {
      paths.push(dotConfigPath);
    }
  } else {
    paths.push(join(homedir(), ".config", "opencode"));
  }
  return paths;
}
function validateConfig(config) {
  try {
    if (typeof config !== "object" || config === null) {
      return false;
    }
    if (config.plugin !== void 0 && !Array.isArray(config.plugin)) {
      return false;
    }
    if (config.plugin) {
      for (const p of config.plugin) {
        if (typeof p !== "string") {
          return false;
        }
      }
    }
    return true;
  } catch {
    return false;
  }
}
function createBackup(configFile) {
  try {
    if (!existsSync(configFile)) {
      return null;
    }
    const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
    const backupFile = `${configFile}.backup.${timestamp}`;
    copyFileSync(configFile, backupFile);
    log("Backup created", { backupFile });
    return backupFile;
  } catch (error) {
    log("Failed to create backup", { error: String(error) });
    return null;
  }
}
function atomicWriteJSON(filePath, data) {
  const tempFile = `${filePath}.tmp.${Date.now()}`;
  try {
    writeFileSync(tempFile, JSON.stringify(data, null, 2) + "\n", { mode: 420 });
    renameSync(tempFile, filePath);
    log("Atomic write successful", { filePath });
  } catch (error) {
    try {
      if (existsSync(tempFile)) {
        unlinkSync(tempFile);
      }
    } catch {
    }
    throw error;
  }
}
function registerInConfig(configDir) {
  const configFile = join(configDir, "opencode.json");
  let backupFile = null;
  try {
    if (!existsSync(configDir)) {
      mkdirSync(configDir, { recursive: true, mode: 493 });
      log("Created config directory", { configDir });
    }
    backupFile = createBackup(configFile);
    let config = {};
    if (existsSync(configFile)) {
      try {
        const content = readFileSync(configFile, "utf-8").trim();
        if (content) {
          config = JSON.parse(content);
          if (!validateConfig(config)) {
            log("Invalid config structure detected, using safe defaults", { config });
            config = { plugin: [] };
          }
        }
      } catch (error) {
        log("Failed to parse existing config, creating new one", { error: String(error) });
        if (backupFile) {
          console.log(`\u26A0\uFE0F  Corrupted config detected. Backup saved: ${backupFile}`);
        }
        config = { plugin: [] };
      }
    }
    if (!config.plugin) {
      config.plugin = [];
    }
    const hasPlugin = config.plugin.some((p) => {
      if (typeof p !== "string") return false;
      return p === PLUGIN_NAME || p.includes(PLUGIN_NAME);
    });
    if (hasPlugin) {
      log("Plugin already registered", { configFile });
      return { success: false, backupFile };
    }
    config.plugin.push(PLUGIN_NAME);
    log("Adding plugin to config", { plugin: PLUGIN_NAME, configFile });
    atomicWriteJSON(configFile, config);
    try {
      const verifyContent = readFileSync(configFile, "utf-8");
      const verifyConfig = JSON.parse(verifyContent);
      if (!verifyConfig.plugin?.includes(PLUGIN_NAME)) {
        throw new Error("Verification failed: plugin not found after write");
      }
    } catch (verifyError) {
      log("Write verification failed, rolling back", { error: String(verifyError) });
      if (backupFile && existsSync(backupFile)) {
        copyFileSync(backupFile, configFile);
        console.log(`\u26A0\uFE0F  Write verification failed. Restored from backup.`);
      }
      throw verifyError;
    }
    log("Plugin registered successfully", { configFile });
    return { success: true, backupFile };
  } catch (error) {
    log("Registration failed", { error: String(error), configFile });
    if (backupFile && existsSync(backupFile)) {
      try {
        copyFileSync(backupFile, configFile);
        log("Rolled back to backup", { backupFile });
        console.log(`\u26A0\uFE0F  Registration failed. Restored from backup: ${backupFile}`);
      } catch (rollbackError) {
        log("Rollback failed", { error: String(rollbackError) });
      }
    }
    return { success: false, backupFile };
  }
}
function cleanupOldBackups(configFile) {
  try {
    const configDir = join(configFile, "..");
    const files = __require("fs").readdirSync(configDir);
    const backupFiles = files.filter((f) => f.startsWith("opencode.json.backup.")).sort().reverse();
    for (let i = 5; i < backupFiles.length; i++) {
      const backupPath = join(configDir, backupFiles[i]);
      try {
        unlinkSync(backupPath);
        log("Deleted old backup", { file: backupFiles[i] });
      } catch {
      }
    }
  } catch {
  }
}
try {
  console.log("\u{1F3AF} OpenCode Orchestrator - Installing...");
  log("Installation started", { platform: process.platform, node: process.version });
  const configPaths = getConfigPaths();
  log("Config paths to check", configPaths);
  let registered = false;
  let alreadyRegistered = false;
  let backupCreated = null;
  for (const configDir of configPaths) {
    const configFile = join(configDir, "opencode.json");
    if (existsSync(configFile)) {
      try {
        const content = readFileSync(configFile, "utf-8").trim();
        if (content) {
          const config = JSON.parse(content);
          if (config.plugin?.some((p) => typeof p === "string" && (p === PLUGIN_NAME || p.includes(PLUGIN_NAME)))) {
            alreadyRegistered = true;
            log("Plugin already registered in this location", { configFile });
            continue;
          }
        }
      } catch (error) {
        log("Error checking existing config", { error: String(error), configFile });
      }
    }
    const result = registerInConfig(configDir);
    if (result.success) {
      console.log(`\u2705 Plugin registered: ${configFile}`);
      if (result.backupFile) {
        console.log(`   Backup created: ${result.backupFile}`);
        backupCreated = result.backupFile;
      }
      registered = true;
      cleanupOldBackups(configFile);
    } else if (result.backupFile) {
      backupCreated = result.backupFile;
    }
  }
  if (!registered && alreadyRegistered) {
    console.log("\u2705 Plugin already registered.");
    log("Plugin was already registered");
  } else if (!registered) {
    console.log("\u26A0\uFE0F  Could not register plugin in any config location.");
    console.log("   This may be due to permissions or file system issues.");
    console.log(`   Check logs: ${LOG_FILE}`);
    log("Failed to register plugin in any location");
  }
  console.log("");
  console.log("\u{1F680} Ready! Restart OpenCode to use.");
  console.log("");
  log("Installation completed", { registered, alreadyRegistered, backupCreated });
} catch (error) {
  log("Installation error", { error: String(error) });
  console.error("\u274C " + formatError(error, "register plugin"));
  console.log(`   Check logs: ${LOG_FILE}`);
  process.exit(0);
}
