mihomo-kit/install.sh

54 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
# One-command setup of mihomo-kit on OpenWrt (nikki), over ssh on the router:
# wget -qO- https://git.sam-hub.net/sam/mihomo-kit/raw/branch/main/install.sh | sh
# or after tools/push.sh from a computer:
# sh /etc/nikki/kit/install.sh
# Installs nikki (if needed), the core, the fixes and the kit with its LuCI page;
# the settings are then made in LuCI -> Services -> mihomo-kit.
set -eu
KIT_URL=${KIT_URL:-https://git.sam-hub.net/sam/mihomo-kit/archive/main.tar.gz}
KIT_DIR=/etc/nikki/kit
here=$(cd "$(dirname "$0")" 2>/dev/null && pwd || echo /nonexistent)
mkdir -p "$KIT_DIR"
if [ -f "$here/router/mihomo-kit.sh" ]; then
# whole directories, as below: a template renamed in the kit must not stay behind
if [ "$here" != "$KIT_DIR" ]; then
for d in templates presets router luci; do
rm -rf "${KIT_DIR:?}/$d"
cp -r "$here/$d" "$KIT_DIR/"
done
fi
else
tmp=/tmp/mihomo-kit.$$
mkdir -p "$tmp"
echo "downloading $KIT_URL"
wget -qO "$tmp/kit.tgz" "$KIT_URL"
tar xzf "$tmp/kit.tgz" -C "$tmp"
src=$(find "$tmp" -path '*/router/mihomo-kit.sh' | head -n 1)
[ -n "$src" ] || { echo "error: no router/mihomo-kit.sh in $KIT_URL" >&2; exit 1; }
src=${src%/router/mihomo-kit.sh}
for d in templates presets router luci; do
rm -rf "${KIT_DIR:?}/$d"
cp -r "$src/$d" "$KIT_DIR/"
done
# commit id from the git archive header (same as kit_version in mihomo-kit.sh)
sha=$(gunzip -c "$tmp/kit.tgz" | head -c 1536 | tr -c 'a-z0-9=\n' '\n' |
grep '^comment=[0-9a-f]\{40\}$' | head -n 1 | cut -c9-15) || true
echo "${sha:-?} $(date '+%Y-%m-%d %H:%M')" > "$KIT_DIR/VERSION"
rm -rf "$tmp"
fi
# stdin may be the piped script: the wizard (setup --cli) reads answers from /dev/tty.
# The output is also kept in RAM: the flash may be full by the time something fails.
log=/tmp/mihomo-kit-install.log
rc=/tmp/mihomo-kit-install.rc
{
code=0
sh "$KIT_DIR/router/mihomo-kit.sh" setup "$@" 2>&1 || code=$?
echo "$code" > "$rc"
} | tee "$log"
code=$(cat "$rc" 2>/dev/null || echo 1)
rm -f "$rc"
echo "install log: $log"
exit "$code"