#!/usr/bin/env bash

set -x --

THIS_SCRIPT="$(realpath $0)"
HERE="$THIS_SCRIPT"

while [ "${HERE##*/}" != "bridgedb" ] ; do HERE="${HERE%/*}" ; done

THERE="$HERE"
echo "Found top-level repo directory: $THERE"
cd $THERE

mkdir -p run/from-authority
mkdir -p run/from-bifroest
cp -R -t run bridgedb.conf captchas
# Add '127.0.0.1' to EMAIL_DOMAINS in bridgedb.conf. This should ONLY be
# done on testing servers, never on production servers.
sed -r -i -e "s/(EMAIL_DOMAINS)(.*)(])/\1\2\, '127.0.0.1']/" run/bridgedb.conf
# Change EMAIL_SMTP_PORT to 2525:
sed -r -i -e "s/(EMAIL_SMTP_PORT = )([1-9]{2,5})/\12525/" run/bridgedb.conf
# Enable plaintext HTTP distribution, otherwise the mechanize-based tests in
# test_https.py will fail with CERTIFICATE_VERIFY_FAILED because urllib2
# won't recognize our self-signed certificate:
sed -r -i -e "s/(HTTP_UNENCRYPTED_BIND_IP = )(None)/\1'127.0.0.1'/" run/bridgedb.conf
sed -r -i -e "s/(HTTP_UNENCRYPTED_PORT = )(None)/\16788/" run/bridgedb.conf
sed -r -i -e "s/(SERVER_PUBLIC_FQDN = )(.*)/\1'127.0.0.1:6788'/" run/bridgedb.conf
# Enable plaintext HTTP distribution for the moat server as well
sed -r -i -e "s/(MOAT_HTTP_IP = )(None)/\1'127.0.0.1'/" run/bridgedb.conf
sed -r -i -e "s/(MOAT_HTTP_PORT = )(None)/\16790/" run/bridgedb.conf
# Create descriptors
./scripts/create_descriptors 200 --num-resistant-descs 100
cp -t run/from-authority networkstatus-bridges cached-extrainfo* bridge-descriptors
cp -t run/from-bifroest networkstatus-bridges cached-extrainfo* bridge-descriptors
# Create TLS certificates
./scripts/make-ssl-cert
cp -t run privkey.pem cert
cp privkey.pem run/moat-tls.pem
cp cert run/moat-tls.crt
