install.sh: --uninstall takes off what it installed, --purge the settings too

pota on Discord: the Linux install script only installs. Now
curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --uninstall
takes off what the script put there, and nothing of the user's:

- Linux: $bin/magpie, the menu entry (while it starts this magpie, or
  one that is gone; another magpie's stays), its icon, the
  x-scheme-handler/magpie=magpie.desktop line xdg-mime wrote (that line
  only), and the menu's mimeinfo.cache when it lists nothing else. The
  service and login entry go too while they start this magpie, since
  left they would start a program that is gone at each login
  (`magpie daemon uninstall`, systemctl as a fallback).
- macOS: magpie.app in /Applications or ~/Applications and the
  ~/.local/bin link into it. Termux: $PREFIX/bin/magpie.
- ~/.config/magpie stays and the script says so. A magpie still
  running is named with its pid; it ends when the user quits it.
- --purge runs `magpie uninstall --yes` first (agents disconnected and
  put back, magpie's folders deleted), then the above. With no magpie
  to ask it refuses, and if magpie uninstall fails nothing of the
  install is taken, so it can be run again.
- Nothing is downloaded; the uninstall runs before the CPU check.

Docs: an FAQ entry "How do I uninstall magpie?" in docs/start (en, zh,
ja), and a line in both READMEs' install note.

Run at 41357f393: node --test site/install.test.mjs worker.test.mjs
stable.test.mjs on mote (53 pass) and install.test.mjs in WSL
magpie-desktop (dash as sh, GNU sed; 14 pass); dash -n ok.

Real run in WSL magpie-desktop (Ubuntu 24.04, WebKitGTK 4.1), sandbox
HOME, env -i, the real 0.1.2345 release from usemagpie.ai:
- install put .local/bin/magpie, applications/magpie.desktop,
  mimeinfo.cache, icons/hicolor/256x256/apps/magpie.png and the
  mimeapps.list line.
- --uninstall, with a settings.json and a text/html line of the user's
  added: removed all of those; left .config/magpie/settings.json, the
  user's mimeapps line and the empty .config, .local/bin and
  .local/share folders.
- install again, then --purge: the real magpie uninstall ran (service,
  login, shortcut, magpie:// links, .config/.cache/.local/share magpie
  deleted), then the binary and the mimeapps line went; left only the
  empty folders and the user's mimeapps.list.
- --purge with no magpie: exit 1, nothing changed.
An earlier run caught that magpie uninstall removes the menu entry
first, which had left the icon and mimeapps line behind; fixed and the
test now covers it.

Broken by hand: origin/main's install.sh turns the four new tests red;
the mimeapps rewrite, the login entry removal and the magpie uninstall
call, each taken out, turn their tests red.

Not tried: macOS and Termux for real (the script's /Applications can't
be sandboxed; the unit test covers Termux's path), a real systemd
user service (a sandbox HOME's unit isn't the user manager's), and a
desktop entry with a quoted Exec path containing a quote.
This commit is contained in:
yetone
2026-10-31 16:09:55 +08:00
parent 41357f3935
commit 305c624eda
7 changed files with 253 additions and 19 deletions
+1 -1
View File
@@ -52,7 +52,7 @@ Runs on macOS, Windows and Linux (menu bar app, window, TUI, web UI and CLI), in
curl -fsSL https://usemagpie.ai/install.sh | sh
```
<sub>Mac builds are signed and notarised, and every build updates itself. Behind a firewall, use `--proxy` or `--mirror`. Or `go install github.com/yetone/magpie@latest`, or the [Docker image](https://usemagpie.ai/docs/docker).</sub>
<sub>Mac builds are signed and notarised, and every build updates itself. Behind a firewall, use `--proxy` or `--mirror`. Or `go install github.com/yetone/magpie@latest`, or the [Docker image](https://usemagpie.ai/docs/docker). Remove what the script installed with `curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --uninstall`; your settings in `~/.config/magpie` stay unless you add `--purge`, which disconnects your agents and deletes them too.</sub>
**2 · Add a provider.** Open magpie and go to **Providers → Add provider**. Pick a preset and paste a key, or sign in with a subscription.
+1 -1
View File
@@ -52,7 +52,7 @@
curl -fsSL https://usemagpie.ai/install.sh | sh
```
<sub>Mac 版已签名并公证,每个版本都会自动更新。网络受限时可用 `--proxy` 或 `--mirror`。也可以 `go install github.com/yetone/magpie@latest`,或者用 [Docker 镜像](https://usemagpie.ai/docs/zh/docker)。</sub>
<sub>Mac 版已签名并公证,每个版本都会自动更新。网络受限时可用 `--proxy` 或 `--mirror`。也可以 `go install github.com/yetone/magpie@latest`,或者用 [Docker 镜像](https://usemagpie.ai/docs/zh/docker)。 用 `curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --uninstall` 卸载脚本装上的东西;`~/.config/magpie` 里的设置会保留,加 `--purge` 才会断开 Agent 并一起删除。</sub>
**2 · 添加 provider**。打开 magpie,进入 **Providers → 添加 provider**。选一个预设、粘贴 key,或者登录一个订阅。
+122 -3
View File
@@ -33,6 +33,13 @@ function install(t, { termux = true, arch = "aarch64", override = false, prefixO
const requests = join(dir, "requests");
writeFileSync(join(tools, "uname"), `#!/bin/sh\ncase "$1" in -m) echo '${arch}';; -s) echo Linux;; esac\n`, { mode: 0o755 });
writeFileSync(join(tools, "ldconfig"), "#!/bin/sh\necho libwebkit2gtk-4.1.so.0\n", { mode: 0o755 });
// xdg-mime as xdg-utils writes the default: one line in mimeapps.list
writeFileSync(join(tools, "xdg-mime"), `#!/bin/sh
[ "$1" = default ] || exit 0
mkdir -p "$XDG_CONFIG_HOME"
printf '[Default Applications]\\n' >> "$XDG_CONFIG_HOME/mimeapps.list"
printf '%s=%s\\n' "$3" "$2" >> "$XDG_CONFIG_HOME/mimeapps.list"
`, { mode: 0o755 });
writeFileSync(join(tools, "curl"), `#!${process.execPath}
const fs = require("node:fs");
const args = process.argv.slice(2);
@@ -62,10 +69,15 @@ else fs.writeFileSync(args[args.indexOf("-o") + 1], tamper && url.startsWith(tam
MAGPIE_MIRROR: "",
...extra,
};
const result = spawnSync("sh", [installer, ...args], { env, encoding: "utf8" });
const read = (p) => (existsSync(p) ? readFileSync(p, "utf8") : "");
const calls = read(env.INSTALL_ARGS).trim().split("\n").filter(Boolean).map((l) => JSON.parse(l));
return { ...result, bin, file, payload, share: env.XDG_DATA_HOME, requests: read(requests), calls };
const run = (args) => {
rmSync(requests, { force: true });
rmSync(env.INSTALL_ARGS, { force: true });
const result = spawnSync("sh", [installer, ...args], { env, encoding: "utf8" });
const calls = read(env.INSTALL_ARGS).trim().split("\n").filter(Boolean).map((l) => JSON.parse(l));
return { ...result, requests: read(requests), calls };
};
return { ...run(args), run, bin, file, payload, home, env, share: env.XDG_DATA_HOME, config: env.XDG_CONFIG_HOME };
}
test("Termux downloads Android arm64 into PREFIX/bin even with WebKit installed", (t) => {
@@ -168,3 +180,110 @@ test("a file the mirror changed is refused and the installed magpie kept", (t) =
assert.notEqual(bad.status, 0);
assert.match(bad.stderr, /--mirror is an http\(s\) address/);
});
// pota on Discord: the script installed magpie and had no way to take it
// off. --uninstall takes off what it put there and keeps the user's
// settings; --purge has magpie disconnect the agents and delete them first.
function installedDesktop(t, opts = {}) {
const r = install(t, { termux: false, desktop: true, ...opts });
assert.equal(r.status, 0, r.stderr);
const exe = join(r.bin, "magpie");
// magpie itself, as far as the uninstall asks it anything
writeFileSync(exe, `#!/bin/sh
echo "$*" >> "$HOME/magpie-calls"
[ "$1 $2" = "daemon uninstall" ] && rm -f "$XDG_CONFIG_HOME/systemd/user/magpie.service"
[ "$1 $2" = "uninstall --yes" ] && { [ -z "$MAGPIE_FAIL" ] || exit 1; rm -rf "$XDG_CONFIG_HOME/magpie" "$XDG_DATA_HOME/applications/magpie.desktop"; }
exit 0
`, { mode: 0o755 });
const settings = join(r.config, "magpie", "settings.json");
mkdirSync(join(r.config, "magpie"), { recursive: true });
writeFileSync(settings, "{}");
const calls = () => (existsSync(join(r.home, "magpie-calls")) ? readFileSync(join(r.home, "magpie-calls"), "utf8") : "");
return { ...r, exe, settings, calls, entry: join(r.share, "applications/magpie.desktop"), icon: join(r.share, "icons/hicolor/256x256/apps/magpie.png"), mime: join(r.config, "mimeapps.list") };
}
test("--uninstall takes off the command, menu entry, icon and magpie:// default, and keeps the settings", (t) => {
const r = installedDesktop(t);
for (const p of [r.exe, r.entry, r.icon]) assert.ok(existsSync(p), p);
writeFileSync(r.mime, readFileSync(r.mime, "utf8") + "text/html=firefox.desktop\n");
assert.match(readFileSync(r.mime, "utf8"), /^x-scheme-handler\/magpie=magpie\.desktop$/m);
const u = r.run(["--uninstall"]);
assert.equal(u.status, 0, u.stderr);
for (const p of [r.exe, r.entry, r.icon]) assert.equal(existsSync(p), false, p);
assert.equal(readFileSync(r.mime, "utf8"), "[Default Applications]\ntext/html=firefox.desktop\n");
assert.ok(existsSync(r.settings), "the settings stay");
assert.equal(r.calls(), "", "magpie isn't asked to uninstall");
assert.equal(u.calls.length, 0, "nothing is downloaded");
assert.match(u.stdout, /kept your settings.*--purge/);
// again: nothing there, nothing wrong
const again = r.run(["--uninstall"]);
assert.equal(again.status, 0, again.stderr);
assert.match(again.stdout, /no magpie installed by this script/);
});
test("--uninstall takes the service and login entry that start this magpie, never another's", (t) => {
const r = installedDesktop(t);
const unit = join(r.config, "systemd/user/magpie.service");
const login = join(r.config, "autostart/magpie.desktop");
mkdirSync(join(r.config, "systemd/user"), { recursive: true });
mkdirSync(join(r.config, "autostart"), { recursive: true });
writeFileSync(unit, `[Service]\nExecStart="${r.exe}" serve\n`);
writeFileSync(login, `[Desktop Entry]\nExec="${r.exe}" --autostart\n`);
const u = r.run(["--uninstall"]);
assert.equal(u.status, 0, u.stderr);
assert.equal(existsSync(unit), false);
assert.equal(existsSync(login), false);
assert.match(r.calls(), /^daemon uninstall$/m);
// another magpie's (one still there) are left alone
const o = installedDesktop(t);
const other = join(o.home, "elsewhere/magpie");
mkdirSync(join(o.home, "elsewhere"), { recursive: true });
writeFileSync(other, "", { mode: 0o755 });
mkdirSync(join(o.config, "systemd/user"), { recursive: true });
mkdirSync(join(o.config, "autostart"), { recursive: true });
const ounit = join(o.config, "systemd/user/magpie.service");
const ologin = join(o.config, "autostart/magpie.desktop");
writeFileSync(ounit, `[Service]\nExecStart="${other}" serve\n`);
writeFileSync(ologin, `[Desktop Entry]\nExec="${other}" --autostart\n`);
writeFileSync(o.entry, `[Desktop Entry]\nExec=${other} %u\n`);
const ou = o.run(["--uninstall"]);
assert.equal(ou.status, 0, ou.stderr);
for (const p of [ounit, ologin, o.entry]) assert.ok(existsSync(p), p);
assert.equal(existsSync(o.exe), false);
});
test("--purge has magpie uninstall itself first, then takes off the install", (t) => {
const r = installedDesktop(t);
const u = r.run(["--purge"]);
assert.equal(u.status, 0, u.stderr);
assert.match(r.calls(), /^uninstall --yes$/m);
for (const p of [r.exe, r.entry, r.icon, r.settings]) assert.equal(existsSync(p), false, p);
// magpie took the menu entry itself; its icon and magpie:// default go too
assert.doesNotMatch(readFileSync(r.mime, "utf8"), /magpie/);
assert.doesNotMatch(u.stdout, /kept your settings/);
// magpie failing to: the command stays, to run it again
const f = installedDesktop(t, { env: { MAGPIE_FAIL: "1" } });
const fu = f.run(["--uninstall", "--purge"]);
assert.notEqual(fu.status, 0);
assert.match(fu.stderr, /magpie uninstall didn't finish/);
for (const p of [f.exe, f.entry, f.settings]) assert.ok(existsSync(p), p);
// no magpie to ask: refused, the settings kept
const n = installedDesktop(t);
rmSync(n.exe);
const nu = n.run(["--purge"]);
assert.notEqual(nu.status, 0);
assert.match(nu.stderr, /no magpie at .* --uninstall alone/);
assert.ok(existsSync(n.settings));
});
test("--uninstall in Termux takes off PREFIX/bin/magpie", (t) => {
const r = install(t, {});
assert.equal(r.status, 0, r.stderr);
const u = r.run(["--uninstall"]);
assert.equal(u.status, 0, u.stderr);
assert.equal(existsSync(join(r.bin, "magpie")), false);
assert.match(u.stdout, /removed .*com\.termux\/files\/usr\/bin\/magpie/);
});
+3
View File
@@ -285,6 +285,9 @@ magpie claude group/opus-anywhere <span class="c"># これを使う</span></p
<h3>GitHub に届かない環境で、インストールや更新をするには?</h3>
<p>インストーラと <code>magpie update</code> にプロキシを渡します:<code>curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --proxy http://127.0.0.1:7890</code>、または <code>magpie update --proxy socks5://127.0.0.1:1080</code>(http・https・socks5・socks5h に対応。<code>HTTPS_PROXY</code> と <code>ALL_PROXY</code> も使われます)。<code>--mirror &lt;prefix&gt;</code> を付けると、指定した GitHub ダウンロードミラーから <code>&lt;prefix&gt;https://github.com/…</code> としてリリースファイルを取得します。<code>magpie update mirror &lt;prefix&gt;</code> でアプリ自身の更新を含め毎回使い、<code>magpie update mirror off</code> で解除します。指定しない限りミラーは使われず、ファイルの SHA-256 はミラーではなく常に usemagpie.ai から取得するので、ミラーが書き換えたファイルはインストールされません。アプリ自身の更新は <span class="ui">Settings</span> → <span class="ui">Proxy</span> を通ります。</p>
<h3 id="uninstall"><a class="anchor" href="#uninstall">magpie をアンインストールするには?</a></h3>
<p>スクリプトでインストールした場合:<code>curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --uninstall</code> で、スクリプトが置いたものを取り除きます。<code>magpie</code> コマンド、macOS ではアプリ、Linux ではメニュー項目・アイコン・<code>magpie://</code> のハンドラ、それに Linux ではもう無い magpie を起動してしまうサービスとログイン時の起動です。<code>~/.config/magpie</code> の設定・プロバイダ・使用量は残るので、再インストールすれば戻ります。接続したエージェントはそれまで magpie のアドレスを指したままです。<code>--purge</code> を付けると先に <code>magpie uninstall --yes</code> を実行し、すべてのエージェントの接続を外して元の設定に戻してから、magpie の設定・プロバイダ・使用量を削除します。ほかの方法でインストールした場合は、<code>magpie uninstall</code> または <span class="ui">Settings</span> → <span class="ui">magpie をアンインストール</span> が <code>--purge</code> と同じことを行い、プログラム本体は手動で削除します。</p>
<h3 id="log">magpie のログはどこにありますか?</h3>
<p>アプリ(ウィンドウ、メニューバーやトレイのアイコン、パネル。どう開いたかによらず)は <code>app.log</code> を書きます。macOS では <code>~/Library/Logs/magpie/app.log</code>、Windows では <code>%LOCALAPPDATA%\magpie\logs\app.log</code>、Linux では <code>~/.local/state/magpie/app.log</code>(<code>$XDG_STATE_HOME</code> が設定されていれば <code>$XDG_STATE_HOME/magpie</code>)、ポータブル版ではデータフォルダーの <code>logs</code> です。<span class="ui">設定</span> → <span class="ui">情報</span> → <span class="ui">ログ</span>にパスがあり、<span class="ui">開く</span>でそのフォルダーを表示します。<code>magpie log</code> はパスを表示し、<code>magpie log -f</code> は追いかけます。5 MiB に達すると隣の <code>app.old.log</code> になり、残るのは最大 2 ファイルです。記録されるのは magpie がしたことと出会ったエラー(プロバイダーがリクエストを断った、再試行した、プラグインが起動しなかった)で、キー、プロンプト、返答は含みません。キーやトークンに見えるものも伏せられます。サービスとして動く <code>magpie serve</code> のログは別の場所です。macOS では <code>~/Library/Logs/magpie/serve.log</code>、Linux では <code>journalctl --user -u magpie</code>。<code>magpie log</code> はそれも示します。</p>
+3
View File
@@ -285,6 +285,9 @@ magpie claude group/opus-anywhere <span class="c"># use it</span></pre>
<h3>Installing or updating behind a firewall, or without access to GitHub?</h3>
<p>Give the installer and <code>magpie update</code> a proxy: <code>curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --proxy http://127.0.0.1:7890</code>, or <code>magpie update --proxy socks5://127.0.0.1:1080</code> (http, https, socks5 and socks5h; <code>HTTPS_PROXY</code> and <code>ALL_PROXY</code> are honoured too). <code>--mirror &lt;prefix&gt;</code> fetches the release file from a GitHub download mirror you choose, as <code>&lt;prefix&gt;https://github.com/…</code>; <code>magpie update mirror &lt;prefix&gt;</code> keeps it for every update, the app’s too, and <code>magpie update mirror off</code> removes it. No mirror is used unless you give one, and the file’s SHA-256 still comes from usemagpie.ai, never from the mirror: a file the mirror changed is refused. The app’s own updates go through <span class="ui">Settings</span> → <span class="ui">Proxy</span>.</p>
<h3 id="uninstall"><a class="anchor" href="#uninstall">How do I uninstall magpie?</a></h3>
<p>Installed with the script: <code>curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --uninstall</code> takes off what it put there: the <code>magpie</code> command, and the app on macOS or its menu entry, icon and <code>magpie://</code> handler on Linux, with magpie’s service and opening at login there, which would start a magpie that is gone. Your settings, providers and usage in <code>~/.config/magpie</code> stay, for when you install again; agents you connected keep magpie’s address until then. Add <code>--purge</code> to have <code>magpie uninstall --yes</code> run first: it disconnects every agent and puts back what each had, then deletes magpie’s settings, providers and usage. Installed another way, <code>magpie uninstall</code> or <span class="ui">Settings</span> → <span class="ui">Uninstall magpie</span> does what <code>--purge</code> does and leaves the program itself for you to delete.</p>
<h3 id="log">Where is magpie’s log?</h3>
<p>The app (its window, the menu bar or tray icon and the panel, however it was opened) writes <code>app.log</code>: <code>~/Library/Logs/magpie/app.log</code> on macOS, <code>%LOCALAPPDATA%\magpie\logs\app.log</code> on Windows, <code>~/.local/state/magpie/app.log</code> on Linux (<code>$XDG_STATE_HOME/magpie</code> when that is set), and <code>logs</code> in a portable magpie’s data folder. <span class="ui">Settings</span> → <span class="ui">About</span> → <span class="ui">Log</span> has its path and an <span class="ui">Open</span> that shows its folder; <code>magpie log</code> prints it, and <code>magpie log -f</code> follows it. At 5 MiB it becomes <code>app.old.log</code> beside it, so two files at most are kept. It holds what magpie did and the errors it met (a provider refusing a request, a retry, a plugin that failed to start), not your keys, prompts or replies; what looks like a key or token is masked anyway. <code>magpie serve</code> run as a service logs elsewhere: <code>~/Library/Logs/magpie/serve.log</code> on macOS, <code>journalctl --user -u magpie</code> on Linux; <code>magpie log</code> names it too.</p>
+3
View File
@@ -285,6 +285,9 @@ magpie claude group/opus-anywhere <span class="c"># 使用它</span></pre>
<h3>没有梯子,安装或更新连不上 GitHub 怎么办?</h3>
<p>给安装脚本和 <code>magpie update</code> 加代理:<code>curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --proxy http://127.0.0.1:7890</code>,或 <code>magpie update --proxy socks5://127.0.0.1:1080</code>(支持 http、https、socks5、socks5h;环境变量 <code>HTTPS_PROXY</code>、<code>ALL_PROXY</code> 同样生效)。<code>--mirror &lt;前缀&gt;</code> 从你指定的 GitHub 下载镜像取发布文件,即 <code>&lt;前缀&gt;https://github.com/…</code>;<code>magpie update mirror &lt;前缀&gt;</code> 让之后每次更新(包括应用自己的更新)都用它,<code>magpie update mirror off</code> 取消。不指定就不用任何镜像;文件的 SHA-256 始终来自 usemagpie.ai,而不是镜像,镜像改过的文件会被拒绝安装。应用自己的更新走 <span class="ui">设置</span> → <span class="ui">代理</span>。</p>
<h3 id="uninstall"><a class="anchor" href="#uninstall">怎么卸载 magpie?</a></h3>
<p>用安装脚本装的:<code>curl -fsSL https://usemagpie.ai/install.sh | sh -s -- --uninstall</code> 会拿掉它装上的东西:<code>magpie</code> 命令,macOS 上的应用,Linux 上的菜单项、图标和 <code>magpie://</code> 链接处理,以及 Linux 上会去启动一个已不存在的 magpie 的后台服务和开机启动。<code>~/.config/magpie</code> 里的设置、provider 和用量都保留,重新安装就回来;已连接的 Agent 在那之前仍指向 magpie 的地址。加上 <code>--purge</code> 会先运行 <code>magpie uninstall --yes</code>:断开每个 Agent 并恢复它原来的配置,再删除 magpie 的设置、provider 和用量。用其他方式装的,<code>magpie uninstall</code> 或 <span class="ui">设置</span> → <span class="ui">卸载 magpie</span> 做的就是 <code>--purge</code> 那些事,程序本身留给你删除。</p>
<h3 id="log">magpie 的日志在哪里?</h3>
<p>应用(窗口、菜单栏或托盘图标、快捷面板,不论怎样打开)写入 <code>app.log</code>:macOS 上是 <code>~/Library/Logs/magpie/app.log</code>,Windows 上是 <code>%LOCALAPPDATA%\magpie\logs\app.log</code>,Linux 上是 <code>~/.local/state/magpie/app.log</code>(设置了 <code>$XDG_STATE_HOME</code> 时是 <code>$XDG_STATE_HOME/magpie</code>),便携版则在数据文件夹的 <code>logs</code> 里。<span class="ui">设置</span> → <span class="ui">关于</span> → <span class="ui">日志</span>里有它的路径,<span class="ui">打开</span>会显示它所在的文件夹;<code>magpie log</code> 打印路径,<code>magpie log -f</code> 持续跟随。满 5 MiB 后它变成旁边的 <code>app.old.log</code>,最多保留两个文件。它记的是 magpie 做了什么、遇到了哪些错误(服务商拒绝请求、重试、插件没能启动),不含你的密钥、提示词或回复;看起来像密钥或令牌的内容也会被遮掉。作为服务运行的 <code>magpie serve</code> 日志在别处:macOS 上是 <code>~/Library/Logs/magpie/serve.log</code>,Linux 上是 <code>journalctl --user -u magpie</code>;<code>magpie log</code> 也会列出它。</p>
+120 -14
View File
@@ -20,12 +20,22 @@
# list and its SHA-256 still come from usemagpie.ai,
# never from the mirror, so a file the mirror changed is
# refused.
# --uninstall takes off what this script put there: the magpie
# command, and the app (macOS) or its menu entry, icon
# and magpie:// handler (Linux), and the service and
# opening at login that would start a magpie no longer
# there. Your settings, providers and usage in
# ~/.config/magpie stay, for when you install again.
# --purge --uninstall, and before it `magpie uninstall --yes`:
# every agent disconnected and put back as it was, and
# magpie's settings, providers and usage deleted.
set -eu
site=https://usemagpie.ai
bin="${MAGPIE_BIN_DIR:-$HOME/.local/bin}"
proxy="${MAGPIE_PROXY:-}"
mirror="${MAGPIE_MIRROR:-}"
uninstall=
say() { printf ' %s\n' "$*"; }
die() { printf 'magpie: %s\n' "$*" >&2; exit 1; }
@@ -36,13 +46,18 @@ while [ $# -gt 0 ]; do
--proxy=*) proxy=${1#--proxy=}; shift ;;
--mirror) [ $# -ge 2 ] || die "--mirror needs a value"; mirror=$2; shift 2 ;;
--mirror=*) mirror=${1#--mirror=}; shift ;;
--uninstall) [ "$uninstall" = purge ] || uninstall=1; shift ;;
--purge) uninstall=purge; shift ;;
-h|--help)
printf '%s\n' "usage: curl -fsSL $site/install.sh | sh -s -- [--proxy <url>] [--mirror <prefix>]" \
" curl -fsSL $site/install.sh | sh -s -- --uninstall [--purge]" \
" --proxy <url> http://, https://, socks5:// or socks5h:// proxy for the downloads (or MAGPIE_PROXY=)" \
" --mirror <prefix> GitHub download mirror put before the release's github.com URL (or MAGPIE_MIRROR=);" \
" the SHA-256 is still checked against $site's"
" the SHA-256 is still checked against $site's" \
" --uninstall take off what this script installed; your settings in ~/.config/magpie stay" \
" --purge with --uninstall: also disconnect every agent and delete magpie's settings (magpie uninstall)"
exit 0 ;;
*) die "unknown option $1 (--proxy <url>, --mirror <prefix>)" ;;
*) die "unknown option $1 (--proxy <url>, --mirror <prefix>, --uninstall, --purge)" ;;
esac
done
case "$proxy" in
@@ -60,28 +75,120 @@ get() {
if [ -n "$proxy" ]; then curl -fsSL --proxy "$proxy" "$@"; else curl -fsSL "$@"; fi
}
case "$(uname -m)" in
arm64|aarch64) arch=arm64 ;;
x86_64|amd64) arch=amd64 ;;
*) die "unsupported CPU: $(uname -m)" ;;
esac
case "$(uname -s)" in
Darwin) os=darwin; file="magpie-darwin-$arch.zip" ;;
Darwin) os=darwin ;;
Linux)
termux="${TERMUX_VERSION:-}"
case "${PREFIX:-}" in */com.termux/files/usr) termux=1 ;; esac
if [ -n "$termux" ]; then
[ -n "${PREFIX:-}" ] || die "Termux requires PREFIX to locate its bin directory"
os=android; file="magpie-cli-android-$arch"
os=android
bin="${MAGPIE_BIN_DIR:-$PREFIX/bin}"
else
os=linux; file="magpie-cli-linux-$arch"
if [ -z "${MAGPIE_CLI:-}" ] && { ldconfig -p 2>/dev/null | grep -q 'libwebkit2gtk-4\.1\.so\.0'; }; then
file="magpie-linux-$arch"
fi
os=linux
fi ;;
*) die "unsupported system: $(uname -s); see $site" ;;
esac
share="${XDG_DATA_HOME:-$HOME/.local/share}"
config="${XDG_CONFIG_HOME:-$HOME/.config}"
# runs <file> <key>: the program an entry's Exec= or a unit's ExecStart=
# starts, its first word, quoted or not
runs() {
sed -n "s/^$2=//p" "$1" 2>/dev/null | head -n 1 | sed -e 's/^"\([^"]*\)".*/\1/' -e t -e 's/ .*//'
}
# --uninstall: what the install below puts there, and nothing of the
# user's. The service and login entry go too while they start this
# magpie: left, they would start a program that is gone at each login.
if [ -n "$uninstall" ]; then
exe="$bin/magpie"
if [ "$uninstall" = purge ]; then
# magpie disconnects the agents it connected and deletes its own
# folders; deleting them here by hand would leave the agents pointed
# at a gateway that is gone
[ -x "$exe" ] || die "no magpie at $exe to disconnect your agents and delete its settings: install it again and run this again, or run --uninstall alone to keep them"
"$exe" uninstall --yes || die "magpie uninstall didn't finish (see above), so the magpie command and app are left: run this again once it does"
fi
removed=
gone() { rm -rf "$1"; removed=1; say "removed $1"; }
if [ "$os" = darwin ]; then
for apps in /Applications "$HOME/Applications"; do
[ ! -d "$apps/magpie.app" ] || gone "$apps/magpie.app"
done
case "$(readlink "$exe" 2>/dev/null)" in
*/magpie.app/Contents/MacOS/magpie) gone "$exe" ;;
esac
else
if [ "$os" = linux ]; then
unit="$config/systemd/user/magpie.service"
if [ "$(runs "$unit" ExecStart)" = "$exe" ]; then
if ! "$exe" daemon uninstall >/dev/null 2>&1; then
command -v systemctl >/dev/null && systemctl --user disable --now magpie.service >/dev/null 2>&1 || true
rm -f "$unit"
fi
removed=1; say "removed magpie's service ($unit)"
fi
login="$config/autostart/magpie.desktop"
[ "$(runs "$login" Exec)" != "$exe" ] || gone "$login"
fi
[ ! -f "$exe" ] || gone "$exe"
if [ "$os" = linux ]; then
apps="$share/applications"
# the menu entry and its icon while they start this magpie, or one
# that is gone; another magpie's stays
run=$(runs "$apps/magpie.desktop" Exec)
if [ -f "$apps/magpie.desktop" ] && { [ "$run" = "$exe" ] || [ ! -e "$run" ]; }; then
gone "$apps/magpie.desktop"
fi
# with no menu entry (this one, or the one magpie uninstall took),
# what came with it: the icon, the magpie:// default xdg-mime set
# (that line only) and the menu's cache when it lists nothing else
if [ ! -e "$apps/magpie.desktop" ]; then
icons="$share/icons/hicolor/256x256/apps"
[ ! -f "$icons/magpie.png" ] || gone "$icons/magpie.png"
rmdir "$icons" "$share/icons/hicolor/256x256" "$share/icons/hicolor" "$share/icons" 2>/dev/null || true
list="$config/mimeapps.list"
if [ -f "$list" ] && grep -qx 'x-scheme-handler/magpie=magpie\.desktop;\{0,1\}' "$list"; then
grep -vx 'x-scheme-handler/magpie=magpie\.desktop;\{0,1\}' "$list" > "$list.magpie-new" || true
mv "$list.magpie-new" "$list"
removed=1; say "removed magpie:// from $list"
fi
if [ -d "$apps" ] && ! ls "$apps" | grep -q '\.desktop$'; then
rm -f "$apps/mimeinfo.cache"
rmdir "$apps" 2>/dev/null || true
elif [ -d "$apps" ]; then
command -v update-desktop-database >/dev/null && update-desktop-database "$apps" 2>/dev/null || true
fi
fi
fi
fi
[ "$removed" ] || say "no magpie installed by this script at $exe"
if [ "$uninstall" != purge ]; then
say "kept your settings, providers and usage in $config/magpie: install again to have them back, or run with --purge to delete them"
if pid=$(pgrep -f "^$exe( |\$)" 2>/dev/null) && [ -n "$pid" ]; then
say "magpie is still running (pid $(printf '%s' "$pid" | tr '\n' ' ' | sed 's/ $//')) and ends when you quit it; agents connected to it reach its gateway until then"
fi
fi
# magpie uninstall said so already
[ "$uninstall" = purge ] || say "magpie is uninstalled"
exit 0
fi
case "$(uname -m)" in
arm64|aarch64) arch=arm64 ;;
x86_64|amd64) arch=amd64 ;;
*) die "unsupported CPU: $(uname -m)" ;;
esac
case "$os" in
darwin) file="magpie-darwin-$arch.zip" ;;
android) file="magpie-cli-android-$arch" ;;
*)
file="magpie-cli-linux-$arch"
if [ -z "${MAGPIE_CLI:-}" ] && { ldconfig -p 2>/dev/null | grep -q 'libwebkit2gtk-4\.1\.so\.0'; }; then
file="magpie-linux-$arch"
fi ;;
esac
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
@@ -128,7 +235,6 @@ if [ "$os" = darwin ]; then
else
install -m 755 "$tmp/$file" "$bin/magpie"
if [ "$file" = "magpie-linux-$arch" ]; then
share="${XDG_DATA_HOME:-$HOME/.local/share}"
mkdir -p "$share/applications" "$share/icons/hicolor/256x256/apps"
get -o "$share/icons/hicolor/256x256/apps/magpie.png" "$site/img/icon-256.png" || true
cat > "$share/applications/magpie.desktop" <<EOF