#!/bin/bash

set -e

CONTRIB_SDIST="$(dirname "$(readlink -e "$0")")"
CONTRIB="$CONTRIB_SDIST"/../..
ROOT_FOLDER="$CONTRIB"/..
PACKAGES="$ROOT_FOLDER"/packages/
LOCALE="$ROOT_FOLDER"/electrum/locale/

if [ ! -d "$PACKAGES" ]; then
  echo "Run make_packages first!"
  exit 1
fi

git submodule update --init

(
    cd "$CONTRIB/deterministic-build/electrum-locale/"
    if ! which msgfmt > /dev/null 2>&1; then
        echo "Please install gettext"
        exit 1
    fi
    # We include both source (.po) and compiled (.mo) locale files in the source dist.
    # Maybe we should exclude the compiled locale files? see https://askubuntu.com/a/144139
    # (also see MANIFEST.in)
    rm -rf "$LOCALE"
    for i in ./locale/*; do
        dir="$ROOT_FOLDER/electrum/$i/LC_MESSAGES"
        mkdir -p "$dir"
        msgfmt --output-file="$dir/electrum.mo" "$i/electrum.po" || true
        cp $i/electrum.po "$ROOT_FOLDER/electrum/$i/electrum.po"
    done
)

(
    cd "$ROOT_FOLDER"

    find -exec touch -h -d '2000-11-11T11:11:11+00:00' {} +

    # note: .zip sdists would not be reproducible due to https://bugs.python.org/issue40963
    TZ=UTC faketime -f '2000-11-11 11:11:11' python3 setup.py --quiet sdist --format=gztar
)
