#!/data/data/com.termux/files/usr/bin/bash # Install or upgrade Hermit on Android Termux. Safe to rerun: an existing # checkout is fast-forwarded, rebuilt and reinstalled. # # bootstrap_termux.sh [DIR] [--no-pull] # hermit-update (installed by this script; same as rerunning it) set -euo pipefail main() { local repo_url="${HERMIT_REPO:-${CST_AGENT_REPO:-https://git.whcself.xyz/WHC/Hermit.git}}" local dir="" pull=1 changed=0 arg for arg in "$@"; do case "$arg" in --no-pull) pull=0 ;; --rebuilt) changed=1 ;; # internal: re-exec after a pull -h | --help) sed -n '2,6p' "$0" return 0 ;; *) dir="$arg" ;; esac done if [[ -z "$dir" ]]; then if [[ -n "${HERMIT_DIR:-${CST_AGENT_DIR:-}}" ]]; then dir="${HERMIT_DIR:-$CST_AGENT_DIR}" elif [[ -f Makefile && -f src/main.c && -d .git ]]; then dir="$(pwd)" elif [[ -d "$HOME/Hermit/.git" ]]; then dir="$HOME/Hermit" else dir="$HOME/CST-agent" fi fi echo "==> Termux packages" local need=() p for p in git clang make pkg-config binutils libcurl ncurses; do dpkg -s "$p" >/dev/null 2>&1 || need+=("$p") done if ((${#need[@]})); then pkg install -y "${need[@]}" else echo " all present" fi local old="" new="" if [[ ! -f "$dir/Makefile" ]]; then echo "==> git clone $repo_url -> $dir" git clone "$repo_url" "$dir" changed=1 elif ((pull)); then old="$(git -C "$dir" rev-parse HEAD)" if git -C "$dir" diff --quiet && git -C "$dir" diff --cached --quiet; then echo "==> git pull ($dir)" git -C "$dir" pull --ff-only else echo "warning: $dir has local changes; skipping git pull" >&2 fi new="$(git -C "$dir" rev-parse HEAD)" if [[ "$old" != "$new" ]]; then echo " $(git -C "$dir" log --oneline "$old..$new" | wc -l) new commit(s)" # The script itself may have changed: continue with the new version. exec bash "$dir/scripts/bootstrap_termux.sh" "$dir" --no-pull --rebuilt fi fi cd "$dir" dir="$(pwd)" echo "==> build" local jobs jobs="$(nproc 2>/dev/null || echo 4)" if ((changed)); then make clean >/dev/null 2>&1 || true fi make -j"$jobs" make check >/dev/null 2>&1 || echo "warning: some tests failed (run 'make check')" echo "==> install to $PREFIX/bin" make install cat >"$PREFIX/bin/hermit-update" </dev/null || \ ln -sf hermit-update "$PREFIX/bin/cst-agent-update" echo echo "OK: $("$PREFIX/bin/hermit" --version 2>/dev/null || "$PREFIX/bin/cst-agent" --version)" echo "Run 'hermit' inside a project directory; upgrade later with 'hermit-update'." echo "The first start asks for the provider and its API key and saves them." } main "$@"