#!/bin/sh
# tools/post-receive — codehub's git hook: copy it to <repository>.git/hooks/post-receive and make it executable.
# After each push it updates the files `git clone` reads over plain HTTP, then exports the repository to the site.
# Each setting comes from the environment, else from the repository's config (git config codehub.<key>), else a default:
#   CODEHUB_SLUG         codehub.slug         the repository's name on the site, [a-z0-9-]    (the folder's name)
#   CODEHUB_DATA         codehub.data         the site's data folder (SOLSTICE_DATA_DIR)      (/srv/site/target/data)
#   CODEHUB_DOWNLOADS    codehub.downloads    the folder served at the addon's downloads_base (/srv/site/downloads)
#   CODEHUB_TITLE        codehub.title        (the slug)
#   CODEHUB_DESCRIPTION  codehub.description  (empty)
#   CODEHUB_BRANCH       codehub.branch       (main)
set -eu
repo=$(pwd)
setting() { git config --get "codehub.$1" 2>/dev/null || printf '%s' "$2"; }
slug=${CODEHUB_SLUG:-$(setting slug "$(basename "$repo" .git)")}
git update-server-info
exec codehub-export --repo "$repo" --slug "$slug" \
  --data "${CODEHUB_DATA:-$(setting data /srv/site/target/data)}" \
  --downloads "${CODEHUB_DOWNLOADS:-$(setting downloads /srv/site/downloads)}" \
  --title "${CODEHUB_TITLE:-$(setting title "$slug")}" \
  --description "${CODEHUB_DESCRIPTION:-$(setting description "")}" \
  --branch "${CODEHUB_BRANCH:-$(setting branch main)}"
