# enlaunch

Build a thin launcher binary with a program's release channel and pinned signing keys compiled in. A system package manager (Homebrew, AUR, apt) owns the launcher at `/usr/bin/<program>`, while the full executable is stored in a user-writable home directory where the program can update itself.

```sh
go get neuroplast.io/engram/enlaunch
```

`neuroplast.io/engram/enlaunch` is a standalone Go module that requires only `neuroplast.io/engram`.

## Directory layout

```text
/usr/bin/acme              thin launcher owned by the system package
~/.local/acme/
  builds/<commit>/         verified artifacts for one build
  bin -> builds/<commit>   symlink to the active build
```

## Launcher and self-update integration

A launcher program calls `enlaunch.Main` with the pinned channel configuration:

```go
// cmd/acme-launcher: installed as /usr/bin/acme by a system package
package main

import "neuroplast.io/engram/enlaunch"

func main() {
	enlaunch.Main(enlaunch.Config{
		Base:    "https://pkg.example.org",
		Project: "acme",
		Channel: "stable",
		Signers: signers, // allowed_signers lines pinned in the binary
	})
}
```

When `~/.local/<project>/bin` already exists, the launcher executes the target binary immediately via `execve` on Unix (preserving PID, arguments, and controlling terminal; on non-Unix platforms it runs the target as a child process) without network requests or version checks. When no build exists in the home or seed directory, it fetches the channel's newest build, verifies the manifest signature against `Signers` and every artifact's size and SHA-256, updates the `bin` symlink atomically, and executes it.

Updates remain under the program's control. The launcher exports `ENLAUNCH_HOME` to the started program (and `enlaunch` removes it from the environment at init so child processes do not inherit it). The program's update command checks `enlaunch.Launched()` and calls `enlaunch.Install`:

```go
if home, ok := enlaunch.Launched(); ok {
	cfg.Home = home
	_, err = enlaunch.Install(ctx, cfg, m.Build.Commit)
} else {
	// Not started via a launcher: update the executable directly
}
```

`enlaunch.Install` fetches the specified commit (or the channel's newest when `commit` is empty), verifies signatures and hashes, writes each raw executable artifact directly with mode `0755` (it does not unpack `.tar.gz` archives), updates `bin` atomically, and retains the active build plus the previously linked build while pruning older directories. Programs that do not ship a launcher are unaffected by `enlaunch`.

## Preloading builds in packages

System packages can bundle an initial verified build in a read-only seed directory (`/usr/lib/<project>` by default) so the first launch works offline:

```sh
# Inside a package build step (for example PKGBUILD package()):
ENLAUNCH_PRELOAD="$pkgdir/usr/lib/acme" ./acme-launcher
```

When the user's home has no installed build yet, the launcher runs the seed build while still setting `ENLAUNCH_HOME` to `~/.local/<project>`. After the program installs its first update into `ENLAUNCH_HOME`, subsequent launches run from the user's home.

## Environment variables

| Variable | Set by | Behavior |
| --- | --- | --- |
| `ENLAUNCH_HOME` | Launcher | Target home directory passed to the started program (`enlaunch.Launched()`) |
| `ENLAUNCH_FETCH` | User | When set to any non-empty value, force fetching the channel's newest build into the home before executing, overriding any broken installed build |
| `ENLAUNCH_PRELOAD=<dir>` | Packager | Fetch the newest verified build into `<dir>` using the home layout and exit without executing the program |

## `Config` fields

| Field | Default | Meaning |
| --- | --- | --- |
| `Base`, `Project`, `Channel` | required | Channel URL `<Base>/<Project>/<Channel>` |
| `Signers` | required | OpenSSH `allowed_signers` lines pinned in the binary |
| `Names` | `[]string{Project}` | Raw executable artifact names to download and install (archives are not unpacked); the first name is executed |
| `Home` | `~/.local/<Project>` | User-writable build directory |
| `Seed` | `/usr/lib/<Project>` | Read-only preloaded build directory checked when `Home` has no build |
