const { spawn } = require("child_process");

const app = process.argv[2];
const command = process.argv[3];

const files = {
  all: ["zammad", "postgresql", "bridge", "opensearch", "leafcutter", "link", "signal-cli-rest-api"],
  linkDev: ["zammad", "postgresql", "opensearch"],
  link: ["zammad", "postgresql", "opensearch", "link"],
  leafcutterDev: ["opensearch"],
  leafcutter: ["opensearch", "leafcutter"],
  opensearch: ["opensearch"],
  bridgeDev: ["zammad", "postgresql", "signal-cli-rest-api"],
  bridge: ["zammad", "postgresql", "bridge", "signal-cli-rest-api"],
  zammad: ["zammad", "postgresql", "opensearch"],
};

const finalFiles = files[app]
  .map((file) => ['-f', `docker/compose/${file}.yml`]).flat();
const finalCommand = command === "up" ? ["up", "-d"] : [command];

const dockerCompose = spawn('docker', ['compose', '--env-file', '.env', ...finalFiles, ...finalCommand]);

dockerCompose.stdout.on('data', (data) => {
  console.log(`${data}`);
});

dockerCompose.stderr.on('data', (data) => {
  console.log(`${data}`);
});
