#! /usr/bin/env bash

# Stage (or queue) build2 packages and distributions.
#
# -t
#    Toolchain only, no brep.
# -p
#    packages only, no baseutils/mingw.
# -q
#    Put packages into the queue instead of staging (distributions still go to
#    staging).
#
usage="usage: etc/stage"

rsync_ops="--progress"

owd=`pwd`
trap "{ cd $owd; exit 1; }" ERR
set -o errtrace # Trap in functions.

function info () { echo "$*" 1>&2; }
function error () { info "$*"; exit 1; }

v="$(cat build2-toolchain/version)"
vf="$(echo $v | sed -e 's/^\(.*\)\.\(.*\)\.\([^-]*\).*$/\1.\2.\3/')" # Final.

if [ -z "$v" -o -z "$vf" ]; then
  error "unable to extract version from `cat build2-toolchain/version`"
fi

toolchain="libbutl build2 libbpkg bpkg"
extras="brep"
base="y"

repo_name="STAGE.BUILD2.ORG"
repo_dir="staging/repository/1"
repo_host1="stage.build2.org:/var/bpkg/1"
repo_host2=

while [ $# -gt 0 ]; do
  case $1 in
    -t)
      extras=
      shift
      ;;
    -p)
      base=n
      shift
      ;;
    -q)
      repo_name="CPPGET.ORG/QUEUE"
      repo_dir="cppget.org/repository/1/queue"
      repo_host1="cppget.org:/var/bpkg/1/queue"
      repo_host2="queue.cppget.org:/var/bpkg/1/queue"
      shift
      ;;
    *)
      error "unexpected $1"
      ;;
  esac
done

tools="$toolchain $extras"

mkdir -p "staging/0/$v"

# Generate baseutils if requested.
#
if [ "$base" = "y" ]; then
  cd private/baseutils

  echo "$v" >version

  ./pkdist -n baseutils -d "$vf" -p "$vf/list-i686" \
-c control -s ./pkpost \
-z "/tmp/build2-baseutils-$v-i686-windows"

  ./pkdist -n baseutils -d "$vf" -p "$vf/list-x86_64" \
-c control -s ./pkpost \
-z "/tmp/build2-baseutils-$v-x86_64-windows"

  ./pkdist -n mingw -d "mingw-$vf" -p "mingw-$vf/list-i686" \
-c ./control-mingw-i686 -s ./pkpost-mingw -u http://repo.msys2.org/mingw \
-x "/tmp/build2-mingw-$v-i686-windows"

  ./pkdist -n mingw -d "mingw-$vf" -p "mingw-$vf/list-x86_64" \
-c ./control-mingw-x86_64 -s ./pkpost-mingw -u http://repo.msys2.org/mingw \
-x "/tmp/build2-mingw-$v-x86_64-windows"

  cd ../..

  mv "/tmp/build2-baseutils-$v-i686-windows.zip" "staging/0/$v/"
  mv "/tmp/build2-baseutils-$v-i686-windows.zip.sha256" "staging/0/$v/"

  mv "/tmp/build2-baseutils-$v-x86_64-windows.zip" "staging/0/$v/"
  mv "/tmp/build2-baseutils-$v-x86_64-windows.zip.sha256" "staging/0/$v/"

  mv "/tmp/build2-mingw-$v-i686-windows.tar.xz" "staging/0/$v/"
  mv "/tmp/build2-mingw-$v-i686-windows.tar.xz.sha256" "staging/0/$v/"

  mv "/tmp/build2-mingw-$v-x86_64-windows.tar.xz" "staging/0/$v/"
  mv "/tmp/build2-mingw-$v-x86_64-windows.tar.xz.sha256" "staging/0/$v/"
fi

# Dist build2-toolchain.
#
# @@ .sha256 sums: make sure they don't contain full paths!

b "dist(build2-toolchain-default/)" \
  "config.dist.archives=$owd/staging/0/$v/tar.xz $owd/staging/0/$v/tar.gz"

cd "$owd/staging/0/$v/"
sha256sum -b "build2-toolchain-$v.tar.gz" >"build2-toolchain-$v.tar.gz.sha256"
sha256sum -b "build2-toolchain-$v.tar.xz" >"build2-toolchain-$v.tar.xz.sha256"
cd "$owd"

# Dist individual packages into the repository.
#
function dist() # <dir> <category>
{
  local b="$(basename $1)"
  local o="$repo_dir/$2"

  # Clean up old packages.
  #
  mkdir -p "$o"
  rm -f "$o/$b"-*

  b "dist($1-default/)" "config.dist.archives=$owd/$o/tar.gz"
}

dist packaging/apr/libapr1 apr
dist packaging/postgresql/libpq postgresql

dist packaging/sqlite/libsqlite3 sqlite
dist packaging/sqlite/sqlite3 sqlite

dist ../studxml/libstudxml studxml

dist ../odb/builds/default/libodb odb
dist ../odb/builds/default/libodb-sqlite odb
dist ../odb/builds/default/libodb-pgsql odb

dist libbutl build2
dist build2 build2
dist libbpkg build2
dist bpkg build2
dist brep build2

# Regenerate the repository.
#
info "Insert $repo_name signing key and press Enter"
read
etc/rep-update "$repo_dir/" \
 --openssl openssl-1.0.2 \
 --openssl-option -engine --openssl-option pkcs11 \
 --openssl-option -keyform --openssl-option engine \
 --key "label_SIGN key"

# Sync repository and distribution.
#
etc/rep-publish "$repo_dir/" "$repo_host1/" $rsync_ops

if [ -n "$repo_host2" ]; then
  etc/rep-publish "$repo_dir/" "$repo_host2/" $rsync_ops
fi

rsync -v -rlpt -c --copy-unsafe-links --prune-empty-dirs --delete-after \
  $rsync_ops staging/0/ stage.build2.org:/var/www/stage.build2.org/public/0/