#!/usr/bin/env bash
set -e

current_path=$(pwd)
current_script=$(realpath "$0")
script_folder=$(dirname "$current_script")
project_folder=("$script_folder/..")

num_ghosties=$(grep "Local identity" $project_folder/service/ghosties | wc -l)
echo "let endowed_accounts = vec!["
for num in $(seq 2 $num_ghosties); do
    account_id=$(grep -m $num "wallet" $project_folder/service/ghosties | tail -n 1 | awk '{ print $6 }')
    public_key=$(ghost key inspect $account_id --public | grep "SS58 Address" | awk '{ print $3 }')
    echo -e "\t// $public_key"
    echo -e "\thex![\"${account_id:2}\"].into(),"
done
echo "];"
echo -e "\n"

print_session_key() {
    echo -e "\t\t// $1"
    echo -e "\t\thex![\"${2:2}\"].$3(),"
}

echo "let initial_authorities: Vec<("
echo -e "\tAccountId,"
echo -e "\tAccountId,"
echo -e "\tBabeId,"
echo -e "\tGrandpaId,"
echo -e "\tAuthorityDiscoveryId,"
echo -e "\tSlowClapId,"
echo ")> = vec!["
for num in $(seq 2 $num_ghosties); do
    echo -e "\t("
    for key_word in $(echo stash stash babe gran audi slow); do
        account_id=$(grep -m $num $key_word $project_folder/service/ghosties | tail -n 1 | awk '{ print $7 }')
        public_key=$(ghost key inspect $account_id --public | grep "SS58 Address" | awk '{ print $3 }')
        postfix="unchecked_into"
        if [ $key_word = "stash" ]; then
            postfix="into"
        fi
        print_session_key $public_key $account_id $postfix
    done
    echo -e "\t),"
done
echo "];"
