#!/bin/sh set -e HTTP_BASE="https://init.autogenic.dev" GIT_HOST="autogenic.dev" MODE="full" KEY="$HOME/.ssh/autogenic" say() { printf '%s\n' "$*" >&2; } jval() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p"; } command -v git >/dev/null 2>&1 || { say "git is required"; exit 1; } command -v curl >/dev/null 2>&1 || { say "curl is required"; exit 1; } # 1) ensure an SSH key if [ ! -f "$KEY" ]; then say "→ generating an autogenic SSH key at $KEY" ssh-keygen -t ed25519 -N "" -f "$KEY" -C "autogenic" >/dev/null 2>&1 fi PUB=$(cat "$KEY.pub") # make future git to autogenic.dev seamless on this machine (idempotent). # PREPEND the block so it wins over any earlier "Host *" IdentityFile — SSH # accumulates IdentityFiles in file order and offers them in that order, and # our server accepts the first key offered, so ours must come first. CFG="$HOME/.ssh/config" if [ ! -f "$CFG" ] || ! grep -q "^Host $GIT_HOST\$" "$CFG" 2>/dev/null; then say "→ adding $GIT_HOST to ~/.ssh/config" TMP=$(mktemp) printf '# autogenic (managed)\nHost %s\n IdentityFile %s\n IdentitiesOnly yes\n StrictHostKeyChecking accept-new\n\n' "$GIT_HOST" "$KEY" > "$TMP" [ -f "$CFG" ] && cat "$CFG" >> "$TMP" cat "$TMP" > "$CFG" && rm -f "$TMP" chmod 600 "$CFG" 2>/dev/null || true fi # 2) link this machine if needed say "→ checking link status" START=$(curl -fsS -X POST "$HTTP_BASE/api/link/start" -H 'content-type: application/json' --data "{\"pubkey\":\"$PUB\"}") if printf '%s' "$START" | grep -q '"already_linked":true'; then say "✓ this machine is already linked" else CODE=$(printf '%s' "$START" | jval code) URL=$(printf '%s' "$START" | jval link_url) [ -n "$URL" ] || { say "could not start link: $START"; exit 1; } say "" say " Authorize this machine — sign in with Google:" say " $URL" say "" (open "$URL" >/dev/null 2>&1 || xdg-open "$URL" >/dev/null 2>&1 || true) say "→ waiting for sign-in (Ctrl-C to cancel)…" S="" i=0 while [ "$i" -lt 150 ]; do sleep 2 S=$(curl -fsS "$HTTP_BASE/api/link/status?code=$CODE" 2>/dev/null || true) if printf '%s' "$S" | grep -q '"linked":true'; then break; fi i=$((i + 1)) done printf '%s' "$S" | grep -q '"linked":true' || { say "timed out waiting for sign-in"; exit 1; } say "✓ linked" fi # login mode: machine is linked + ssh config is set — stop here (no repo). if [ "$MODE" = "login" ]; then say "" say "✓ this machine is linked. You can now clone or push, e.g.:" say " git clone ssh://$GIT_HOST/" exit 0 fi # 3) init repo + remote + push REPO=$(basename "$(pwd)") if [ ! -d .git ]; then say "→ git init ($REPO)" git init -q git symbolic-ref HEAD refs/heads/main 2>/dev/null || true fi git config core.sshCommand "ssh -i $KEY -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" git remote remove autogenic >/dev/null 2>&1 || true git remote add autogenic "ssh://$GIT_HOST/$REPO" if ! git rev-parse HEAD >/dev/null 2>&1; then [ -n "$(git status --porcelain 2>/dev/null)" ] || printf '# %s\n' "$REPO" > README.md git add -A N=$(git config user.name 2>/dev/null || echo autogenic) E=$(git config user.email 2>/dev/null || echo you@autogenic.dev) git -c user.name="$N" -c user.email="$E" commit -q -m "initial commit" || true fi BR=$(git symbolic-ref --short HEAD 2>/dev/null || echo main) say "→ pushing to ssh://$GIT_HOST/$REPO" git push -u autogenic "$BR" say "" say "✓ done — your repo is on autogenic: ssh://$GIT_HOST/$REPO" say " clone it elsewhere: git clone ssh://$GIT_HOST/$REPO"