set -e

AVAILIABLE_NETWORKS=(anvil-localnet sepolia-testnet hoodi-testnet mordor-testnet)
NETWORK="${AVAILIABLE_NETWORKS[0]}"
START=1
END=14
VERIFY_NEEDED="--verify"
LEGACY_FLAGS=""

usage() {
    echo "Correct deployer script usage:"
    echo -e "\t-s, --start - from which contract deployment should start."
    echo -e "\t-e, --end - last contract deployment to be done."
    echo -e "\t-n, --network - network name to be deployed on."
    echo -e "\t-b, --blockscout - should try to use blockscout as verifier."
    echo -e "\nAvailable network names: ${AVAILIABLE_NETWORKS[@]}"
    echo -e "\nContract enumeration:"
    echo "0) Reserve"
    echo "1) Authority"
    echo "2) Fatso"
    echo "3) Stinky"
    echo "4) Ghost"
    echo "5) Staking"
    echo "6) Treasury"
    echo "7) BondDepository"
    echo "8) StakingDistributor"
    echo "9) BondingCalculator"
    echo "10) Gatekeeper"
    echo "11) Governor"
    echo "12) AfterPartyFirst"
    echo "13) AfterPartySecond"
    echo "14) AfterPartyThird"
    exit 1
}

while [[ $# -gt 0 ]];
do
    case "${1}" in
        -s|--start)
        START="$2"
        shift
        shift
        ;;
        -e|--end)
        END="$2"
        shift
        shift
        ;;
        -n|--network)
        NETWORK="$2"
        shift
        shift
        ;;
        -b|--blockscout)
        VERIFY_NEEDED+=" --verifier blockscout"
        shift
        ;;
        -h|--help)
        usage
        ;;
        *)
        echo -e "\nERROR: Unknown option provided\n"
        usage
        ;;
    esac
done

if [ "$NETWORK" == "anvil-localnet" ]; then
    VERIFY_NEEDED=""
fi

found=false
for i in "${AVAILIABLE_NETWORKS[@]}"
do
    if [ "$i" == "$NETWORK" ] ; then
        found=true
        if [ "$NETWORK" == "mordor-testnet" ]; then
            LEGACY_FLAGS="--legacy"
        fi
    fi
done

if [ $found == false ] ; then
    echo "ERROR: '${NETWORK}' is incorrect network for deployment, valid networks are:"
    for i in "${AVAILIABLE_NETWORKS[@]}"
    do
        echo -e "\t${i}"
    done
    exit
fi

for i in $(seq $START $END); do
    if [ "0" == "$i" ]; then
        CONTRACT="Reserve"
    elif [ "1" == "$i" ] ; then
        CONTRACT="Authority"
    elif [ "2" == "$i" ]; then
        CONTRACT="Fatso"
    elif [ "3" == "$i" ]; then
        CONTRACT="Stinky"
    elif [ "4" == "$i" ]; then
        CONTRACT="Ghost"
    elif [ "5" == "$i" ]; then
        CONTRACT="Staking"
    elif [ "6" == "$i" ]; then
        CONTRACT="Treasury"
    elif [ "7" == "$i" ]; then
        CONTRACT="BondDepository"
    elif [ "8" == "$i" ]; then
        CONTRACT="StakingDistributor"
    elif [ "9" == "$i" ]; then
        CONTRACT="BondingCalculator"
    elif [ "10" == "$i" ]; then
        CONTRACT="Gatekeeper"
    elif [ "11" == "$i" ]; then
        CONTRACT="Governor"
    elif [ "12" == "$i" ]; then
        CONTRACT="AfterPartyFirst"
    elif [ "13" == "$i" ]; then
        CONTRACT="AfterPartySecond"
    elif [ "14" == "$i" ]; then
        CONTRACT="AfterPartyThird"
    else
        echo "No contract found for index $i"
        exit
    fi

    forge script script/${CONTRACT}.s.sol:Ghost${CONTRACT}Script ${LEGACY_FLAGS} --rpc-url $NETWORK
    if [ "$NETWORK" == "anvil-localnet" ]; then
        answer="y"
    else
        read -p "Do you want to broadcast '${CONTRACT}' to ${NETWORK} (y/N)? " answer
    fi

    if [ "$answer" != "${answer#[Yy]}" ] ; then
        forge script script/${CONTRACT}.s.sol:Ghost${CONTRACT}Script \
            --rpc-url $NETWORK \
            --broadcast \
            ${VERIFY_NEEDED} \
            ${LEGACY_FLAGS} \
            -vvv
    else
        exit
    fi
done
