#!/bin/bash

# cd into the directory containing this script (from the bash faq 028)
if [[ $BASH_SOURCE = */* ]]; then
  cd -- "${BASH_SOURCE%/*}/" || exit
fi

cd ..

fetch_repo() {
    repo_url="$1"
    commit_id="$2"
    dir_name="$3"

    if [ -d "$dir_name" ]; then
        ( cd "$dir_name" && git fetch origin --tags && \
            git checkout "$commit_id" )
    else
        git clone "$repo_url" "$dir_name" && \
        ( cd "$dir_name" && git checkout "$commit_id" && \
            if [ -e "../patches/${dir_name}.diff" ]; then
                patch -p1 < ../patches/${dir_name}.diff || exit 1
            fi )
    fi
}

fetch_repos() {
    fetch_repo https://github.com/arkworks-rs/spongefish v0.5.1 spongefish && \
    fetch_repo https://github.com/mmaker/sigma-rs v0.3.0 sigma-proofs && \
    fetch_repo https://git-crysp.uwaterloo.ca/SigmaProtocol/sigma-compiler 0.2.2 sigma-compiler && \
    fetch_repo https://git-crysp.uwaterloo.ca/SigmaProtocol/cmz 0.2.1 cmz && \
    fetch_repo https://gitlab.torproject.org/onyinyang/lox.git/ lox-artifact application-lox && \
    fetch_repo https://gitlab.torproject.org/onyinyang/lox.git/ lox-artifact-zkp application-lox-zkp && \
    fetch_repo https://github.com/ooni/userauth artifact-v0.2 application-ooni
}

fetch_repos
