#!/bin/bash

# This script is a wrapper for running the packaging in the context of a container.
#
# It is assuming there will be an 'artifacts' directory that contains all of the
# pre-built versions of safe_authenticator and safe_app for Linux and targetX, and
# hopefully Windows in the future.
#
# With all the artifacts available it will package everything into zip files.

set -e -x

deployment_type=$1
if [[ -z "$deployment_type" ]]; then
    echo "An argument indicating the type of the deployment must be supplied."
    echo "Valid values are 'versioned', 'commit_hash' or 'nightly'."
    exit 1
fi

if [[ ! -d "artifacts" ]]; then
    echo "This script is intended to be used with a docker container and a set of pre-built artifacts."
    echo "Place these artifacts in an 'artifacts' folder at the root of the repository and perform the 'docker run' command again."
    exit 1
fi

export RUST_BACKTRACE=1
[[ ! -d "deploy" ]] && mkdir -p deploy/prod

declare -a targets=("x86_64-unknown-linux-gnu" "x86_64-pc-windows-gnu" "x86_64-apple-darwin" )

function run_packaging() {
    local name=$1
    local deployment_type=$2
    local target=$3
    local build_type=$4

    local package_args="--lib --name $name --dest deploy/$build_type"
    package_args="$package_args --target $target --artifacts artifacts/$build_type"
    [[ "$deployment_type" == "nightly" ]] && package_args="$package_args --nightly"
    [[ "$deployment_type" == "commit_hash" ]] && package_args="$package_args --commit"
    [[ "$build_type" == "dev" ]] && package_args="$package_args --dev"

    package_command=(./scripts/package.rs $package_args)
    "${package_command[@]}"
}

# for target in "${targets[@]}"; do
    # run_packaging "safe_authenticator_ffi" "$deployment_type" "$target" "dev"
    # run_packaging "safe_authenticator_ffi" "$deployment_type" "$target" "prod"
# done
