#!/bin/sh
# One-time server setup so GitHub Actions can deploy (see .github/workflows/deploy.yml).
# Run once on the server, AFTER install-linux.sh, from the repo folder:
#   sudo sh deploy/linux/setup-ci-deploy.sh git@github-workshare:Dikshant294/kavion-workshare-server.git
#
# Creates:
#   /opt/workshare-src        root-owned clone used for deploys (reads GitHub with its own read-only deploy key)
#   user "deployer"           no password, no shell access: its SSH key may only run "deploy <sha>"
#   /usr/local/sbin/workshare-deploy, workshare-deploy-ssh   root-owned, not editable by deployer
#   /etc/sudoers.d/workshare-deploy                         deployer may run exactly workshare-deploy as root
set -eu
REPO="${1:-}"
[ "$(id -u)" = 0 ] || { echo "run with sudo"; exit 1; }
[ -n "$REPO" ] || { echo "usage: sudo sh deploy/linux/setup-ci-deploy.sh <git ssh url of the repo>"; exit 1; }
[ -f deploy/linux/workshare-deploy ] || { echo "run from the repo folder"; exit 1; }
command -v git >/dev/null || { echo "install git first"; exit 1; }
command -v flock >/dev/null || { echo "flock missing (package util-linux)"; exit 1; }

echo "== scripts"
install -o root -g root -m 755 deploy/linux/workshare-deploy /usr/local/sbin/workshare-deploy
install -o root -g root -m 755 deploy/linux/workshare-deploy-ssh /usr/local/sbin/workshare-deploy-ssh

echo "== root's read-only GitHub key for /opt/workshare-src"
install -d -m 700 /root/.ssh
if [ ! -f /root/.ssh/workshare_src ]; then ssh-keygen -q -t ed25519 -C "workshare-src@$(hostname)" -f /root/.ssh/workshare_src -N ""; fi
REPO_PATH="$(printf '%s' "$REPO" | sed -E 's#^git@[^:]+:##; s#^https://github.com/##')"
if ! grep -q "Host github-workshare-src" /root/.ssh/config 2>/dev/null; then
  cat >> /root/.ssh/config <<CFG
Host github-workshare-src
    HostName github.com
    User git
    IdentityFile /root/.ssh/workshare_src
    IdentitiesOnly yes
CFG
  chmod 600 /root/.ssh/config
fi
ssh-keyscan -t ed25519 github.com 2>/dev/null >> /root/.ssh/known_hosts
echo
echo "Add this as a SECOND deploy key on GitHub (repo > Settings > Deploy keys, read-only), title 'server deploy clone':"
echo
cat /root/.ssh/workshare_src.pub
echo
printf "Press Enter when it is added... "; read _

if [ ! -d /opt/workshare-src/.git ]; then
  git clone --quiet "git@github-workshare-src:$REPO_PATH" /opt/workshare-src
fi
git -C /opt/workshare-src fetch --quiet origin && echo "clone OK"

echo "== user deployer"
id deployer >/dev/null 2>&1 || useradd --create-home --shell /bin/sh deployer
passwd -l deployer >/dev/null 2>&1 || true
install -d -o deployer -g deployer -m 700 /home/deployer/.ssh
KEYDIR="$(mktemp -d)"
ssh-keygen -q -t ed25519 -C "github-actions-deploy" -f "$KEYDIR/ci" -N ""
echo "restrict,command=\"/usr/local/sbin/workshare-deploy-ssh\" $(cat "$KEYDIR/ci.pub")" > /home/deployer/.ssh/authorized_keys
chown deployer:deployer /home/deployer/.ssh/authorized_keys
chmod 600 /home/deployer/.ssh/authorized_keys

echo "deployer ALL=(root) NOPASSWD: /usr/local/sbin/workshare-deploy" > /etc/sudoers.d/workshare-deploy
chmod 440 /etc/sudoers.d/workshare-deploy
visudo -cf /etc/sudoers.d/workshare-deploy >/dev/null

echo
echo "================ GitHub secrets (repo > Settings > Secrets and variables > Actions) ================"
echo "DEPLOY_HOST        = the address GitHub reaches this server at"
echo "DEPLOY_PORT        = 22 (or your SSH port)"
echo "DEPLOY_KNOWN_HOSTS = output of: ssh-keyscan -p 22 <that address>   (run it on your PC)"
echo "DEPLOY_SSH_KEY     = everything between the lines below:"
echo "-----8<-----"
cat "$KEYDIR/ci"
echo "-----8<-----"
rm -rf "$KEYDIR"
echo "The private key above is not stored on this server. Paste it into GitHub now; if lost, run this script again."
