///|
/// The place of an app that is installed for one instance, below the base folder.
/// `mission-control desktop install` installs an app there, and `mission-control desktop`
/// finds it there.
#derive(Eq, Debug)
struct InstallPlace {
  instance : @app.InstanceName
  folder : @core.FolderPath
  app : @core.FolderPath
  program : @core.FolderPath
  binary : @core.FolderPath
  instance_file : @core.FolderPath?
  record_file : @core.FolderPath
}

///|
/// The place of the app of an instance.
pub fn InstallPlace::InstallPlace(
  instance : @app.InstanceName,
  base : @core.BaseFolder,
  platform : @core.Platform,
) -> InstallPlace {
  let folder = base.desktop(instance)
  let program = @core.desktop_program(folder)
  // On Linux the program is a script, because Proton finds the engine with variables.
  let binary = if platform is Linux {
    from(from(folder, "bin"), program.name())
  } else {
    program
  }
  // A file in a bundle changes its signature. The app opens the instance of its channel
  // without the file.
  let is_own = instance ==
    @app.InstanceName::of_channel(@contracts.Channel::current())
  {
    instance,
    folder,
    // NAME.app/Contents/MacOS/program
    app: if platform is MacOs {
      above(above(above(program)))
    } else {
      folder
    },
    program,
    binary,
    instance_file: if platform is MacOs && is_own {
      None
    } else {
      Some(installed_instance_path(binary, platform))
    },
    record_file: from(folder, app_record_file),
  }
}

///|
/// The instance that the app opens.
pub fn InstallPlace::instance(self : InstallPlace) -> @app.InstanceName {
  self.instance
}

///|
/// The folder that has the app, and nothing else.
pub fn InstallPlace::folder(self : InstallPlace) -> @core.FolderPath {
  self.folder
}

///|
/// The app itself. On macOS it is the bundle in the folder, which can be a link to the
/// bundle in `~/Applications`. On Windows and Linux it is the folder.
pub fn InstallPlace::app(self : InstallPlace) -> @core.FolderPath {
  self.app
}

///|
/// The path of the program in the folder that `mission-control desktop` starts.
pub fn InstallPlace::program(self : InstallPlace) -> @core.FolderPath {
  self.program
}

///|
/// The path of the program that runs. On Linux `program` is a script that gives this
/// program its engine. On Windows and macOS it is the same file as `program`.
pub fn InstallPlace::binary(self : InstallPlace) -> @core.FolderPath {
  self.binary
}

///|
/// The path of the file with the name of the instance. An app on macOS for the instance of
/// its channel has no such file.
pub fn InstallPlace::instance_file(self : InstallPlace) -> @core.FolderPath? {
  self.instance_file
}

///|
/// The path of the file with the version of the app and its engine, in the folder.
pub fn InstallPlace::record_file(self : InstallPlace) -> @core.FolderPath {
  self.record_file
}

///|
pub extend InstallPlace with Eq::{not_equal, equal}

///|
pub extend InstallPlace with @debug.Debug::{to_repr}