#!/usr/bin/env bash set -Eeuo pipefail bold= cyan= green= red= reset= if [[ -t 1 && "${TERM:-dumb}" != dumb && ! -v NO_COLOR ]]; then bold=$'\033[1m' cyan=$'\033[36m' green=$'\033[32m' red=$'\033[31m' reset=$'\033[0m' fi step() { current_step="$2" printf '\n%b[%s/4] %s%b\n' "$bold$cyan" "$1" "$2" "$reset" } ok() { printf ' %b[OK]%b %s\n' "$green" "$reset" "$1"; } info() { printf ' %s\n' "$1"; } fail() { printf '\n %b[FAIL]%b %s\n' "$red" "$reset" "$1" >&2; exit 1; } # Keep successful tool output quiet, but show diagnostics when a command fails. run_quiet() { local result=0 "$@" > "$temporary/command.log" 2>&1 || result=$? if ((result != 0)); then cat "$temporary/command.log" >&2 return "$result" fi } current_step='Starting installer' trap 'fail "$current_step failed. See the message above."' ERR main() { [[ "$(uname -s)" == Linux ]] || fail 'Run this installer on Ubuntu.' ((EUID != 0)) || fail 'Run as your normal user; the installer uses sudo when needed.' umask 077 printf '\n%bHomelab installer%b\n' "$bold" "$reset" info 'Set up GitHub access, download Homelab, and install the command.' local temporary temporary_root temporary_root="$(cd -- "${TMPDIR:-/tmp}" && pwd -P)" temporary="$(mktemp -d "$temporary_root/homelab-install.XXXXXX")" [[ "$temporary" == "$temporary_root"/homelab-install.* && -d "$temporary" ]] || fail 'Cannot create the installer working directory.' # Expand the directory now: the local variable is gone when EXIT runs. trap "$(printf 'rm -rf -- %q' "$temporary")" EXIT step 1 'Prerequisites' local packages=() command -v curl >/dev/null 2>&1 || packages+=(curl) command -v git >/dev/null 2>&1 || packages+=(git) if ! command -v ssh >/dev/null 2>&1 || ! command -v ssh-keygen >/dev/null 2>&1; then packages+=(openssh-client) fi if ((${#packages[@]})); then info "Installing: ${packages[*]} (sudo may ask for your password)." run_quiet sudo apt-get update run_quiet sudo apt-get install -y "${packages[@]}" fi if ! command -v croc >/dev/null 2>&1; then info 'Installing croc (sudo may ask for your password).' curl -fsSL https://getcroc.com -o "$temporary/install-croc.sh" run_quiet bash "$temporary/install-croc.sh" hash -r command -v croc >/dev/null 2>&1 || fail 'croc installation did not make the command available.' fi ok 'Git, SSH, curl, and croc are ready.' step 2 'GitHub access' local key_path=~/.ssh/id_ed25519 if [[ ! -e "$key_path" && ! -L "$key_path" ]]; then info 'On your other computer, send the GitHub SSH private key:' printf '\n croc send PATH_TO_id_ed25519\n' info 'Windows example:' printf ' croc send C:\\Users\\Ben\\.ssh\\id_ed25519\n\n' info 'Enter the transfer code below and accept id_ed25519.' mkdir "$temporary/receive" (cd "$temporary/receive" && croc) [[ -f "$temporary/receive/id_ed25519" && ! -L "$temporary/receive/id_ed25519" ]] \ || fail 'The transfer did not include a file named id_ed25519.' chmod 600 "$temporary/receive/id_ed25519" ssh-keygen -y -f "$temporary/receive/id_ed25519" >/dev/null mkdir -p ~/.ssh chmod 700 ~/.ssh [[ ! -e "$key_path" && ! -L "$key_path" ]] \ || fail 'An SSH key appeared during the transfer; leaving it unchanged.' install -m 600 "$temporary/receive/id_ed25519" "$key_path" else [[ -f "$key_path" ]] || fail "$key_path is not a regular key file." info "Using $key_path" chmod 600 "$key_path" fi info 'Checking GitHub access. Confirm GitHub host identity if prompted.' local response ssh_status=0 response="$(ssh -i "$key_path" -o IdentitiesOnly=yes -T git@github.com 2>&1)" || ssh_status=$? # GitHub returns 1 even when authentication succeeds (no shell access). if [[ "$ssh_status" -ne 1 && "$ssh_status" -ne 0 ]] \ || [[ "$response" != *'successfully authenticated'* ]]; then printf '%s\n' "$response" >&2 fail 'GitHub authentication failed. Add the matching public key to your GitHub account.' fi ok 'GitHub authentication succeeded.' step 3 'Homelab repository' local repo_dir=~/homelab local git_ssh_command="ssh -i $(printf '%q' "$key_path") -o IdentitiesOnly=yes" if [[ -e "$repo_dir" || -L "$repo_dir" ]]; then local origin branch changes [[ -d "$repo_dir/.git" ]] || fail "$repo_dir already exists and is not this repository." origin="$(git -C "$repo_dir" remote get-url origin)" case "$origin" in git@github.com:Bennycopter/homelab.git|https://github.com/Bennycopter/homelab.git) info "Updating $repo_dir" ;; *) fail 'The existing checkout has a different origin; leaving it unchanged.' ;; esac changes="$(git -C "$repo_dir" status --porcelain)" [[ -z "$changes" ]] || fail "$repo_dir has local changes. Commit or move them before rerunning." branch="$(git -C "$repo_dir" symbolic-ref --quiet --short HEAD)" \ || fail 'The checkout has no active branch. Check out your Homelab branch before rerunning.' GIT_SSH_COMMAND="$git_ssh_command" run_quiet git -C "$repo_dir" pull --ff-only --no-rebase git@github.com:Bennycopter/homelab.git "$branch" else info "Downloading to $repo_dir" GIT_SSH_COMMAND="$git_ssh_command" run_quiet git clone git@github.com:Bennycopter/homelab.git "$repo_dir" fi ok 'Repository is ready.' step 4 'Install homelab command' [[ -f "$repo_dir/bootstrap.sh" ]] || fail 'bootstrap.sh is missing from the downloaded checkout.' info 'Installing the menu and shortcut (sudo may ask for your password).' run_quiet bash "$repo_dir/bootstrap.sh" --no-menu --bin-dir /usr/local/bin ok 'homelab is available on your PATH.' printf '\n%bSetup complete%b\n\n' "$bold$green" "$reset" info 'Open the menu:' printf ' %bhomelab%b\n\n' "$bold" "$reset" info 'Or run bootstrap directly:' printf ' %bhomelab bootstrap%b\n\n' "$bold" "$reset" } # Bash is reading the script from a pipe. Interactive commands need the terminal. if ! { true < /dev/tty; } 2>/dev/null; then fail 'Run this installer in an interactive terminal or SSH session.' fi main "$@" < /dev/tty