Files
linux-bootstrap/install.sh
2026-06-14 13:13:45 +02:00

73 lines
2.4 KiB
Bash

#!/usr/bin/env bash
#
# install.sh - tiny entrypoint for the curl|bash oneliner.
# Ensures git, clones the repo, hands off to bootstrap.sh.
#
# Configurable via env:
# REPO_URL git url (default: this public repo)
# REF branch/tag/commit (default: remote's HEAD branch, e.g. main/master)
# DEST clone destination (default: /opt/linux-bootstrap)
# Any extra args are forwarded to bootstrap.sh, e.g.:
# curl -fsSL .../install.sh | bash -s -- --skip hardening
#
set -Eeuo pipefail
export DEBIAN_FRONTEND=noninteractive
REPO_URL="${REPO_URL:-https://gitea.big-chungus.me/moritz/linux-bootstrap.git}"
REF="${REF:-}" # empty -> auto-detect remote default branch
DEST="${DEST:-/opt/linux-bootstrap}"
say() { printf '\e[34m[install]\e[0m %s\n' "$*"; }
if [[ $EUID -ne 0 ]]; then
command -v sudo >/dev/null 2>&1 || { echo "need root or sudo"; exit 1; }
SUDO="sudo"
else
SUDO=""
fi
# 1. ensure git
if ! command -v git >/dev/null 2>&1; then
say "installing git"
if command -v apt-get >/dev/null 2>&1; then
$SUDO apt-get update -qq
$SUDO apt-get install -y -qq git
elif command -v dnf >/dev/null 2>&1; then
$SUDO dnf install -y git
elif command -v pacman >/dev/null 2>&1; then
$SUDO pacman -Sy --noconfirm git
else
echo "no supported package manager to install git"; exit 1
fi
fi
# 2. resolve ref: explicit env wins; otherwise ask the remote what its default
# branch is (handles main vs master without guessing).
if [[ -z "$REF" ]]; then
REF="$(git ls-remote --symref "$REPO_URL" HEAD 2>/dev/null \
| awk '/^ref:/ {sub("refs/heads/","",$2); print $2; exit}')"
if [[ -z "$REF" ]]; then
say "could not detect default branch, falling back to 'main'"
REF="main"
else
say "remote default branch: $REF"
fi
fi
# 3. clone or update
if [[ -d "$DEST/.git" ]]; then
say "updating existing clone at $DEST ($REF)"
$SUDO git -C "$DEST" remote set-url origin "$REPO_URL"
$SUDO git -C "$DEST" fetch --depth=1 origin "$REF"
$SUDO git -C "$DEST" checkout -B "$REF" "origin/$REF"
$SUDO git -C "$DEST" reset --hard "origin/$REF"
else
say "cloning $REPO_URL ($REF) -> $DEST"
$SUDO git clone --depth=1 --branch "$REF" "$REPO_URL" "$DEST"
fi
# 4. run bootstrap, forwarding any extra args
say "running bootstrap.sh $*"
$SUDO chmod +x "$DEST/bootstrap.sh" "$DEST/config/motd/01-banner.sh"
exec "$DEST/bootstrap.sh" "$@"